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