View Javadoc

1   /*
2    * $Id: AbstractAsynchronousLoanBrokerTestCase.java 7976 2007-08-21 14:26:13Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.examples.loanbroker.tests;
12  
13  import org.mule.examples.loanbroker.messages.Customer;
14  import org.mule.examples.loanbroker.messages.CustomerQuoteRequest;
15  import org.mule.examples.loanbroker.messages.LoanQuote;
16  import org.mule.extras.client.MuleClient;
17  import org.mule.providers.NullPayload;
18  import org.mule.umo.UMOMessage;
19  
20  /**
21   * Tests the Loan Broker application asynchronously.  Note that a simple thread delay is used to wait for the 
22   * incoming responses to arrive.  This may or may not be sufficient depending on external factors (processor 
23   * speed, logging detail, etc.).  To make the tests reliable, a more accurate mechanism should be employed 
24   * (notifications, thread-safe counter, etc.)
25   */
26  public abstract class AbstractAsynchronousLoanBrokerTestCase extends AbstractLoanBrokerTestCase
27  {
28      // @Override
29      protected int getNumberOfRequests()
30      {
31          return 100;
32      }
33      
34      /**
35       * Milliseconds to wait after sending each message in order for the thread to "catch up" with the test.
36       */
37      protected int getDelay()
38      {
39          return 3000;
40      }
41      
42      public void testSingleLoanRequest() throws Exception
43      {
44          MuleClient client = new MuleClient();
45          Customer c = new Customer("Ross Mason", 1234);
46          CustomerQuoteRequest request = new CustomerQuoteRequest(c, 100000, 48);
47          // Send asynchronous request
48          client.dispatch("CustomerRequests", request, null);
49          
50          // Wait for asynchronous response
51          UMOMessage result = client.receive("CustomerResponses", getDelay());
52          assertNotNull("Result is null", result);
53          assertFalse("Result is null", result.getPayload() instanceof NullPayload);
54          assertTrue("Result should be LoanQuote but is " + result.getPayload().getClass().getName(), 
55                      result.getPayload() instanceof LoanQuote);
56          LoanQuote quote = (LoanQuote)result.getPayload();
57          assertTrue(quote.getInterestRate() > 0);
58      }
59  
60      public void testLotsOfLoanRequests() throws Exception
61      {
62          MuleClient client = new MuleClient();
63          Customer c = new Customer("Ross Mason", 1234);
64          CustomerQuoteRequest[] requests = new CustomerQuoteRequest[3];
65          requests[0] = new CustomerQuoteRequest(c, 100000, 48);
66          requests[1] = new CustomerQuoteRequest(c, 1000, 12);
67          requests[2] = new CustomerQuoteRequest(c, 10, 24);
68  
69          long start = System.currentTimeMillis();
70  
71          int numRequests = getNumberOfRequests();
72          int i = 0;
73          UMOMessage result;
74          try
75          {
76              for (i = 0; i < numRequests; i++)
77              {
78                  client.dispatch("CustomerRequests", requests[i % 3], null);
79              }
80              for (i = 0; i < numRequests; i++)
81              {
82                  result = client.receive("CustomerResponses", getDelay() * numRequests);
83                  assertNotNull("Result is null", result);
84                  assertFalse("Result is null", result.getPayload() instanceof NullPayload);
85                  assertTrue("Result should be LoanQuote but is " + result.getPayload().getClass().getName(), 
86                              result.getPayload() instanceof LoanQuote);
87                  LoanQuote quote = (LoanQuote)result.getPayload();
88                  assertTrue(quote.getInterestRate() > 0);
89              }
90          }
91          finally
92          {
93              long el = System.currentTimeMillis() - start;
94              System.out.println("Total running time was: " + el + "ms");
95              System.out.println("Requests processed was: " + i);
96              int mps = (int)(numRequests/((double)el/(double)1000));
97              System.out.println("Msg/sec: " + mps + " (no warm up)");
98          }
99      }
100 }