1
2
3
4
5
6
7
8
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 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
35
36
37
38
39 public abstract class AbstractAsynchronousLoanBrokerTestCase extends AbstractLoanBrokerTestCase
40 {
41
42 @Override
43 protected int getNumberOfRequests()
44 {
45 return 100;
46 }
47
48
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 MuleClient client = new MuleClient(muleContext);
63 Customer c = new Customer("Ross Mason", 1234);
64 CustomerQuoteRequest request = new CustomerQuoteRequest(c, 10000, 48);
65
66 client.dispatch("CustomerRequests", request, null);
67
68
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 assertTrue(quote.getInterestRate() > 0);
76 }
77
78 @Override
79 public void testLotsOfLoanRequests() throws Exception
80 {
81 final MuleClient client = new MuleClient(muleContext);
82 Customer c = new Customer("Ross Mason", 1234);
83 CustomerQuoteRequest[] requests = new CustomerQuoteRequest[3];
84 requests[0] = new CustomerQuoteRequest(c, 100000, 48);
85 requests[1] = new CustomerQuoteRequest(c, 1000, 12);
86 requests[2] = new CustomerQuoteRequest(c, 10, 24);
87
88 final StopWatch stopWatch = new StopWatch();
89
90 final int numRequests = getNumberOfRequests() + getWarmUpMessages();
91 int i = 0;
92
93 int numberOfThreads = 1;
94
95 CountDownLatch latch = new CountDownLatch(numberOfThreads);
96 ExceptionHolder exceptionHolder = new ExceptionHolder();
97 try
98 {
99 for (int x = 0; x < numberOfThreads; x++)
100 {
101 Thread thread = new Thread(new ClientReceiver(latch, numRequests / numberOfThreads, exceptionHolder));
102 thread.start();
103 }
104
105
106 for (i = 0; i < numRequests; i++)
107 {
108 if (i == getWarmUpMessages())
109 {
110 stopWatch.start();
111 }
112 client.dispatch("CustomerRequests", requests[i % 3], null);
113 }
114 }
115 finally
116 {
117 latch.await();
118 stopWatch.stop();
119 System.out.println("Total running time was: " + stopWatch.getTime() + "ms");
120 System.out.println("Requests processed was: " + i);
121 int mps = (int) (numRequests / ((double) stopWatch.getTime() / (double) 1000));
122 System.out.println("Msg/sec: " + mps + " (warm up msgs = " + getWarmUpMessages() + ")");
123 if(exceptionHolder.isExceptionThrown())
124 {
125 exceptionHolder.print();
126 fail("Exceptions thrown during async processing");
127 }
128 }
129 }
130
131 private class ClientReceiver implements Runnable
132 {
133
134 private CountDownLatch latch;
135 private int numberOfRequests;
136 private ExceptionListener exListener;
137
138 public ClientReceiver(CountDownLatch latch, int numberOfRequests, ExceptionListener exListener)
139 {
140 this.latch = latch;
141 this.numberOfRequests = numberOfRequests;
142 this.exListener = exListener;
143 }
144
145 public void run()
146 {
147 int i = 0;
148 try
149 {
150 MuleClient client = new MuleClient(muleContext);
151 MuleMessage result = null;
152 for (i = 0; i < numberOfRequests; i++)
153 {
154 try
155 {
156 result = client.request("CustomerResponses", getDelay());
157 }
158 catch (MuleException e)
159 {
160 exListener.exceptionThrown(e);
161 break;
162 }
163
164 assertNotNull("Result is null", result);
165 assertFalse("Result is null", result.getPayload() instanceof NullPayload);
166 assertTrue("Result should be LoanQuote but is " + result.getPayload().getClass().getName(),
167 result.getPayload() instanceof LoanQuote);
168 LoanQuote quote = (LoanQuote) result.getPayload();
169 assertTrue(quote.getInterestRate() > 0);
170 }
171 }
172 catch (Throwable e)
173 {
174
175 System.out.println(StringMessageUtils.getBoilerPlate("Processed Messages=" + i));
176 if(e instanceof Error)
177 {
178
179 exListener.exceptionThrown(new Exception(e));
180 }
181 else
182 {
183 exListener.exceptionThrown((Exception)e);
184 }
185 }
186 finally
187 {
188 latch.countDown();
189 }
190 }
191 }
192 }