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.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
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 final MuleClient client = muleContext.getClient();
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 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
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
176 System.out.println(StringMessageUtils.getBoilerPlate("Processed Messages=" + i));
177 if(e instanceof Error)
178 {
179
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 }