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