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 | |
|
25 | |
import edu.emory.mathcs.backport.java.util.concurrent.CountDownLatch; |
26 | |
|
27 | |
import org.apache.commons.lang.time.StopWatch; |
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | 0 | public abstract class AbstractAsynchronousLoanBrokerTestCase extends AbstractLoanBrokerTestCase |
36 | |
{ |
37 | |
@Override |
38 | |
protected int getNumberOfRequests() |
39 | |
{ |
40 | 0 | return 100; |
41 | |
} |
42 | |
|
43 | |
|
44 | |
protected int getDelay() |
45 | |
{ |
46 | 0 | return 10000; |
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(); |
58 | 0 | Customer c = new Customer("Ross Mason", 1234); |
59 | 0 | CustomerQuoteRequest request = new CustomerQuoteRequest(c, 100000, 48); |
60 | |
|
61 | 0 | client.dispatch("CustomerRequests", request, null); |
62 | |
|
63 | |
|
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(); |
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(); |
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 | |
|
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 | |
|
170 | 0 | System.out.println(StringMessageUtils.getBoilerPlate("Processed Messages=" + i)); |
171 | 0 | if(e instanceof Error) |
172 | |
{ |
173 | |
|
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 | |
} |