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