001: /*
002: * $Id: AbstractAsynchronousLoanBrokerTestCase.java 11393 2008-03-17 15:16:00Z dirk.olmes $
003: * --------------------------------------------------------------------------------------
004: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
005: *
006: * The software in this package is published under the terms of the CPAL v1.0
007: * license, a copy of which has been included with this distribution in the
008: * LICENSE.txt file.
009: */
010:
011: package org.mule.example.loanbroker.tests;
012:
013: import org.mule.api.MuleException;
014: import org.mule.api.MuleMessage;
015: import org.mule.example.loanbroker.messages.Customer;
016: import org.mule.example.loanbroker.messages.CustomerQuoteRequest;
017: import org.mule.example.loanbroker.messages.LoanQuote;
018: import org.mule.module.client.MuleClient;
019: import org.mule.transport.NullPayload;
020: import org.mule.util.ExceptionHolder;
021: import org.mule.util.StringMessageUtils;
022:
023: import java.beans.ExceptionListener;
024:
025: import edu.emory.mathcs.backport.java.util.concurrent.CountDownLatch;
026:
027: import org.apache.commons.lang.time.StopWatch;
028:
029: /**
030: * Tests the Loan Broker application asynchronously. Note that a simple thread delay is used to wait for the
031: * incoming responses to arrive. This may or may not be sufficient depending on external factors (processor
032: * speed, logging detail, etc.). To make the tests reliable, a more accurate mechanism should be employed
033: * (notifications, thread-safe counter, etc.)
034: */
035: public abstract class AbstractAsynchronousLoanBrokerTestCase extends
036: AbstractLoanBrokerTestCase {
037: @Override
038: protected int getNumberOfRequests() {
039: return 100;
040: }
041:
042: /** Milliseconds to wait after sending each message in order for the thread to "catch up" with the test. */
043: protected int getDelay() {
044: return 10000;
045: }
046:
047: protected int getWarmUpMessages() {
048: return 50;
049: }
050:
051: @Override
052: public void testSingleLoanRequest() throws Exception {
053: MuleClient client = new MuleClient();
054: Customer c = new Customer("Ross Mason", 1234);
055: CustomerQuoteRequest request = new CustomerQuoteRequest(c,
056: 100000, 48);
057: // Send asynchronous request
058: client.dispatch("CustomerRequests", request, null);
059:
060: // Wait for asynchronous response
061: MuleMessage result = client.request("CustomerResponses",
062: getDelay());
063: assertNotNull("Result is null", result);
064: assertFalse("Result is null",
065: result.getPayload() instanceof NullPayload);
066: assertTrue("Result should be LoanQuote but is "
067: + result.getPayload().getClass().getName(), result
068: .getPayload() instanceof LoanQuote);
069: LoanQuote quote = (LoanQuote) result.getPayload();
070: assertTrue(quote.getInterestRate() > 0);
071: }
072:
073: @Override
074: public void testLotsOfLoanRequests() throws Exception {
075: final MuleClient client = new MuleClient();
076: Customer c = new Customer("Ross Mason", 1234);
077: CustomerQuoteRequest[] requests = new CustomerQuoteRequest[3];
078: requests[0] = new CustomerQuoteRequest(c, 100000, 48);
079: requests[1] = new CustomerQuoteRequest(c, 1000, 12);
080: requests[2] = new CustomerQuoteRequest(c, 10, 24);
081:
082: final StopWatch stopWatch = new StopWatch();
083:
084: final int numRequests = getNumberOfRequests()
085: + getWarmUpMessages();
086: int i = 0;
087:
088: int numberOfThreads = 1;
089:
090: CountDownLatch latch = new CountDownLatch(numberOfThreads);
091: ExceptionHolder exceptionHolder = new ExceptionHolder();
092: try {
093: for (int x = 0; x < numberOfThreads; x++) {
094: Thread thread = new Thread(new ClientReceiver(latch,
095: numRequests / numberOfThreads, exceptionHolder));
096: thread.start();
097: }
098:
099: for (i = 0; i < numRequests; i++) {
100: if (i == getWarmUpMessages()) {
101: stopWatch.start();
102: }
103: client.dispatch("CustomerRequests", requests[i % 3],
104: null);
105: }
106: } finally {
107: latch.await();
108: stopWatch.stop();
109: System.out.println("Total running time was: "
110: + stopWatch.getTime() + "ms");
111: System.out.println("Requests processed was: " + i);
112: int mps = (int) (numRequests / ((double) stopWatch
113: .getTime() / (double) 1000));
114: System.out.println("Msg/sec: " + mps + " (warm up msgs = "
115: + getWarmUpMessages() + ")");
116: if (exceptionHolder.isExceptionThrown()) {
117: exceptionHolder.print();
118: fail("Exceptions thrown during async processing");
119: }
120: }
121: }
122:
123: private class ClientReceiver implements Runnable {
124:
125: private CountDownLatch latch;
126: private int numberOfRequests;
127: private ExceptionListener exListener;
128:
129: public ClientReceiver(CountDownLatch latch,
130: int numberOfRequests, ExceptionListener exListener) {
131: this .latch = latch;
132: this .numberOfRequests = numberOfRequests;
133: this .exListener = exListener;
134: }
135:
136: public void run() {
137: int i = 0;
138: try {
139: MuleClient client = new MuleClient();
140: MuleMessage result = null;
141: for (i = 0; i < numberOfRequests; i++) {
142: try {
143: result = client.request("CustomerResponses",
144: getDelay());
145: } catch (MuleException e) {
146: exListener.exceptionThrown(e);
147: break;
148: }
149: //System.out.println("Received: " + i);
150: assertNotNull("Result is null", result);
151: assertFalse("Result is null",
152: result.getPayload() instanceof NullPayload);
153: assertTrue("Result should be LoanQuote but is "
154: + result.getPayload().getClass().getName(),
155: result.getPayload() instanceof LoanQuote);
156: LoanQuote quote = (LoanQuote) result.getPayload();
157: assertTrue(quote.getInterestRate() > 0);
158: }
159: } catch (Throwable e) {
160: //e.printStackTrace();
161: System.out.println(StringMessageUtils
162: .getBoilerPlate("Processed Messages=" + i));
163: if (e instanceof Error) {
164: //throw (Error)e;
165: exListener.exceptionThrown(new Exception(e));
166: } else {
167: exListener.exceptionThrown((Exception) e);
168: }
169: } finally {
170: latch.countDown();
171: }
172: }
173: }
174: }
|