Coverage Report - org.mule.examples.loanbroker.tests.AbstractAsynchronousLoanBrokerTestCase
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractAsynchronousLoanBrokerTestCase
0%
0/39
0%
0/6
1.667
AbstractAsynchronousLoanBrokerTestCase$1
0%
0/15
0%
0/4
1.667
 
 1  
 /*
 2  
  * $Id: AbstractAsynchronousLoanBrokerTestCase.java 10288 2008-01-11 16:34:21Z aperepel $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
 5  
  *
 6  
  * The software in this package is published under the terms of the CPAL v1.0
 7  
  * license, a copy of which has been included with this distribution in the
 8  
  * LICENSE.txt file.
 9  
  */
 10  
 
 11  
 package org.mule.examples.loanbroker.tests;
 12  
 
 13  
 import org.mule.examples.loanbroker.messages.Customer;
 14  
 import org.mule.examples.loanbroker.messages.CustomerQuoteRequest;
 15  
 import org.mule.examples.loanbroker.messages.LoanQuote;
 16  
 import org.mule.extras.client.MuleClient;
 17  
 import org.mule.providers.NullPayload;
 18  
 import org.mule.umo.UMOException;
 19  
 import org.mule.umo.UMOMessage;
 20  
 import org.mule.util.concurrent.Latch;
 21  
 
 22  
 import org.apache.commons.lang.time.StopWatch;
 23  
 
 24  
 /**
 25  
  * Tests the Loan Broker application asynchronously.  Note that a simple thread delay is used to wait for the 
 26  
  * incoming responses to arrive.  This may or may not be sufficient depending on external factors (processor 
 27  
  * speed, logging detail, etc.).  To make the tests reliable, a more accurate mechanism should be employed 
 28  
  * (notifications, thread-safe counter, etc.)
 29  
  */
 30  0
 public abstract class AbstractAsynchronousLoanBrokerTestCase extends AbstractLoanBrokerTestCase
 31  
 {
 32  
     // @Override
 33  
     protected int getNumberOfRequests()
 34  
     {
 35  0
         return 100;
 36  
     }
 37  
     
 38  
     /**
 39  
      * Milliseconds to wait after sending each message in order for the thread to "catch up" with the test.
 40  
      */
 41  
     protected int getDelay()
 42  
     {
 43  0
         return 30000;
 44  
     }
 45  
 
 46  
     protected int getWarmUpMessages()
 47  
     {
 48  0
         return 20;
 49  
     }
 50  
     
 51  
     public void testSingleLoanRequest() throws Exception
 52  
     {
 53  0
         MuleClient client = new MuleClient();
 54  0
         Customer c = new Customer("Ross Mason", 1234);
 55  0
         CustomerQuoteRequest request = new CustomerQuoteRequest(c, 100000, 48);
 56  
         // Send asynchronous request
 57  0
         client.dispatch("CustomerRequests", request, null);
 58  
         
 59  
         // Wait for asynchronous response
 60  0
         UMOMessage result = client.receive("CustomerResponses", getDelay());
 61  0
         assertNotNull("Result is null", result);
 62  0
         assertFalse("Result is null", result.getPayload() instanceof NullPayload);
 63  0
         assertTrue("Result should be LoanQuote but is " + result.getPayload().getClass().getName(), 
 64  
                     result.getPayload() instanceof LoanQuote);
 65  0
         LoanQuote quote = (LoanQuote)result.getPayload();
 66  0
         assertTrue(quote.getInterestRate() > 0);
 67  0
     }
 68  
 
 69  
     public void testLotsOfLoanRequests() throws Exception
 70  
     {
 71  0
         final MuleClient client = new MuleClient();
 72  0
         Customer c = new Customer("Ross Mason", 1234);
 73  0
         CustomerQuoteRequest[] requests = new CustomerQuoteRequest[3];
 74  0
         requests[0] = new CustomerQuoteRequest(c, 100000, 48);
 75  0
         requests[1] = new CustomerQuoteRequest(c, 1000, 12);
 76  0
         requests[2] = new CustomerQuoteRequest(c, 10, 24);
 77  
 
 78  0
         final StopWatch stopWatch = new StopWatch();
 79  0
         int i = 0;
 80  0
         final int numRequests = getNumberOfRequests() + getWarmUpMessages();
 81  0
         final Latch latch = new Latch();
 82  0
         Thread thread = new Thread(new Runnable()
 83  
             {
 84  
 
 85  0
                 public void run()
 86  
                 {
 87  
                     try
 88  
                     {
 89  0
                         UMOMessage result = null;
 90  0
                         for (int i = 0; i < numRequests; i++)
 91  
                         {
 92  
                             try
 93  
                             {
 94  0
                                 result = client.receive("CustomerResponses", getDelay());
 95  
                             }
 96  0
                             catch (UMOException e)
 97  
                             {
 98  0
                                 fail(e.getMessage());
 99  0
                             }
 100  
                             //System.out.println("Received: " + i);
 101  0
                             assertNotNull("Result is null", result);
 102  0
                             assertFalse("Result is null", result.getPayload() instanceof NullPayload);
 103  0
                             assertTrue("Result should be LoanQuote but is " + result.getPayload().getClass().getName(),
 104  
                                         result.getPayload() instanceof LoanQuote);
 105  0
                             LoanQuote quote = (LoanQuote)result.getPayload();
 106  0
                             assertTrue(quote.getInterestRate() > 0);
 107  
                         }
 108  
                     }
 109  
                     finally
 110  
                     {
 111  0
                         latch.countDown();
 112  0
                     }
 113  
 
 114  0
                 }
 115  
             });
 116  0
             thread.start();
 117  
         try
 118  
         {
 119  0
             for (i = 0; i < numRequests; i++)
 120  
             {
 121  0
                 if(i==getWarmUpMessages())
 122  
                 {
 123  0
                     stopWatch.start();
 124  
                 }
 125  0
                 client.dispatch("CustomerRequests", requests[i % 3], null);
 126  
             }
 127  
         }
 128  
         finally
 129  
         {
 130  0
             latch.await();
 131  0
             stopWatch.stop();
 132  0
             System.out.println("Total running time was: " + stopWatch.getTime() + "ms");
 133  0
             System.out.println("Requests processed was: " + i);
 134  0
             int mps = (int)(numRequests/((double)stopWatch.getTime()/(double)1000));
 135  0
             System.out.println("Msg/sec: " + mps + " (warm up msgs= " + getWarmUpMessages() + ")");
 136  0
         }
 137  0
     }
 138  
 }