View Javadoc

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