Coverage Report - org.mule.example.loanbroker.tests.AbstractAsynchronousLoanBrokerTestCase
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractAsynchronousLoanBrokerTestCase
0%
0/47
0%
0/10
0
AbstractAsynchronousLoanBrokerTestCase$ClientReceiver
0%
0/27
0%
0/6
0
 
 1  
 /*
 2  
  * $Id: AbstractAsynchronousLoanBrokerTestCase.java 20321 2010-11-24 15:21:24Z dfeist $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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.example.loanbroker.tests;
 12  
 
 13  
 import org.mule.api.MuleException;
 14  
 import org.mule.api.MuleMessage;
 15  
 import org.mule.example.loanbroker.messages.Customer;
 16  
 import org.mule.example.loanbroker.messages.CustomerQuoteRequest;
 17  
 import org.mule.example.loanbroker.messages.LoanQuote;
 18  
 import org.mule.module.client.MuleClient;
 19  
 import org.mule.transport.NullPayload;
 20  
 import org.mule.util.ExceptionHolder;
 21  
 import org.mule.util.StringMessageUtils;
 22  
 
 23  
 import java.beans.ExceptionListener;
 24  
 
 25  
 import edu.emory.mathcs.backport.java.util.concurrent.CountDownLatch;
 26  
 
 27  
 import org.apache.commons.lang.time.StopWatch;
 28  
 
 29  
 /**
 30  
  * Tests the Loan Broker application asynchronously.  Note that a simple thread delay is used to wait for the
 31  
  * incoming responses to arrive.  This may or may not be sufficient depending on external factors (processor
 32  
  * speed, logging detail, etc.).  To make the tests reliable, a more accurate mechanism should be employed
 33  
  * (notifications, thread-safe counter, etc.)
 34  
  */
 35  0
 public abstract class AbstractAsynchronousLoanBrokerTestCase extends AbstractLoanBrokerTestCase
 36  
 {
 37  
     @Override
 38  
     protected int getNumberOfRequests()
 39  
     {
 40  0
         return 100;
 41  
     }
 42  
 
 43  
     /** Milliseconds to wait after sending each message in order for the thread to "catch up" with the test. */
 44  
     protected int getDelay()
 45  
     {
 46  0
         return 30000;
 47  
     }
 48  
 
 49  
     protected int getWarmUpMessages()
 50  
     {
 51  0
         return 50;
 52  
     }
 53  
 
 54  
     @Override
 55  
     public void testSingleLoanRequest() throws Exception
 56  
     {
 57  0
         MuleClient client = new MuleClient(muleContext);
 58  0
         Customer c = new Customer("Ross Mason", 1234);
 59  0
         CustomerQuoteRequest request = new CustomerQuoteRequest(c, 10000, 48);
 60  
         // Send asynchronous request
 61  0
         client.dispatch("CustomerRequests", request, null);
 62  
 
 63  
         // Wait for asynchronous response
 64  0
         MuleMessage result = client.request("CustomerResponses", getDelay());
 65  0
         assertNotNull("Result is null", result);
 66  0
         assertFalse("Result is null", result.getPayload() instanceof NullPayload);
 67  0
         assertTrue("Result should be LoanQuote but is " + result.getPayload().getClass().getName(),
 68  
                 result.getPayload() instanceof LoanQuote);
 69  0
         LoanQuote quote = (LoanQuote) result.getPayload();
 70  0
         assertTrue(quote.getInterestRate() > 0);
 71  0
     }
 72  
 
 73  
     @Override
 74  
     public void testLotsOfLoanRequests() throws Exception
 75  
     {
 76  0
         final MuleClient client = new MuleClient(muleContext);
 77  0
         Customer c = new Customer("Ross Mason", 1234);
 78  0
         CustomerQuoteRequest[] requests = new CustomerQuoteRequest[3];
 79  0
         requests[0] = new CustomerQuoteRequest(c, 100000, 48);
 80  0
         requests[1] = new CustomerQuoteRequest(c, 1000, 12);
 81  0
         requests[2] = new CustomerQuoteRequest(c, 10, 24);
 82  
 
 83  0
         final StopWatch stopWatch = new StopWatch();
 84  
 
 85  0
         final int numRequests = getNumberOfRequests() + getWarmUpMessages();
 86  0
         int i = 0;
 87  
 
 88  0
         int numberOfThreads = 1;
 89  
 
 90  0
         CountDownLatch latch = new CountDownLatch(numberOfThreads);
 91  0
         ExceptionHolder exceptionHolder = new ExceptionHolder();
 92  
         try
 93  
         {
 94  0
             for (int x = 0; x < numberOfThreads; x++)
 95  
             {
 96  0
                 Thread thread = new Thread(new ClientReceiver(latch, numRequests / numberOfThreads, exceptionHolder));
 97  0
                 thread.start();
 98  
             }
 99  
 
 100  
 
 101  0
             for (i = 0; i < numRequests; i++)
 102  
             {
 103  0
                 if (i == getWarmUpMessages())
 104  
                 {
 105  0
                     stopWatch.start();
 106  
                 }
 107  0
                 client.dispatch("CustomerRequests", requests[i % 3], null);
 108  
             }
 109  0
         }
 110  
         finally
 111  
         {
 112  0
             latch.await();
 113  0
             stopWatch.stop();
 114  0
             System.out.println("Total running time was: " + stopWatch.getTime() + "ms");
 115  0
             System.out.println("Requests processed was: " + i);
 116  0
             int mps = (int) (numRequests / ((double) stopWatch.getTime() / (double) 1000));
 117  0
             System.out.println("Msg/sec: " + mps + " (warm up msgs = " + getWarmUpMessages() + ")");
 118  0
             if(exceptionHolder.isExceptionThrown())
 119  
             {
 120  0
                 exceptionHolder.print();
 121  0
                 fail("Exceptions thrown during async processing");
 122  
             }
 123  0
         }
 124  0
     }
 125  
 
 126  0
     private class ClientReceiver implements Runnable
 127  
     {
 128  
 
 129  
         private CountDownLatch latch;
 130  
         private int numberOfRequests;
 131  
         private ExceptionListener exListener;
 132  
 
 133  
         public ClientReceiver(CountDownLatch latch, int numberOfRequests, ExceptionListener exListener)
 134  0
         {
 135  0
             this.latch = latch;
 136  0
             this.numberOfRequests = numberOfRequests;
 137  0
             this.exListener = exListener;
 138  0
         }
 139  
 
 140  
         public void run()
 141  
         {
 142  0
             int i = 0;
 143  
             try
 144  
             {
 145  0
                 MuleClient client = new MuleClient(muleContext);
 146  0
                 MuleMessage result = null;
 147  0
                 for (i = 0; i < numberOfRequests; i++)
 148  
                 {
 149  
                     try
 150  
                     {
 151  0
                         result = client.request("CustomerResponses", getDelay());
 152  
                     }
 153  0
                     catch (MuleException e)
 154  
                     {
 155  0
                         exListener.exceptionThrown(e);
 156  0
                         break;
 157  0
                     }
 158  
                     //System.out.println("Received: " + i);
 159  0
                     assertNotNull("Result is null", result);
 160  0
                     assertFalse("Result is null", result.getPayload() instanceof NullPayload);
 161  0
                     assertTrue("Result should be LoanQuote but is " + result.getPayload().getClass().getName(),
 162  
                             result.getPayload() instanceof LoanQuote);
 163  0
                     LoanQuote quote = (LoanQuote) result.getPayload();
 164  0
                     assertTrue(quote.getInterestRate() > 0);
 165  
                 }
 166  
             }
 167  0
             catch (Throwable e)
 168  
             {
 169  
                 //e.printStackTrace();
 170  0
                 System.out.println(StringMessageUtils.getBoilerPlate("Processed Messages=" + i));
 171  0
                 if(e instanceof Error)
 172  
                 {
 173  
                     //throw (Error)e;
 174  0
                     exListener.exceptionThrown(new Exception(e));
 175  
                 }
 176  
                 else
 177  
                 {
 178  0
                     exListener.exceptionThrown((Exception)e);
 179  
                 }
 180  
             }
 181  
             finally
 182  
             {
 183  0
                 latch.countDown();
 184  0
             }
 185  0
         }
 186  
     }
 187  
 }