View Javadoc

1   /*
2    * $Id: AbstractAsynchronousLoanBrokerTestCase.java 10288 2008-01-11 16:34:21Z aperepel $
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.UMOException;
19  import org.mule.umo.UMOMessage;
20  import org.mule.util.concurrent.Latch;
21  
22  import org.apache.commons.lang.time.StopWatch;
23  
24  /**
25   * Tests the Loan Broker application asynchronously.  Note that a simple thread delay is used to wait for the 
26   * incoming responses to arrive.  This may or may not be sufficient depending on external factors (processor 
27   * speed, logging detail, etc.).  To make the tests reliable, a more accurate mechanism should be employed 
28   * (notifications, thread-safe counter, etc.)
29   */
30  public abstract class AbstractAsynchronousLoanBrokerTestCase extends AbstractLoanBrokerTestCase
31  {
32      // @Override
33      protected int getNumberOfRequests()
34      {
35          return 100;
36      }
37      
38      /**
39       * Milliseconds to wait after sending each message in order for the thread to "catch up" with the test.
40       */
41      protected int getDelay()
42      {
43          return 30000;
44      }
45  
46      protected int getWarmUpMessages()
47      {
48          return 20;
49      }
50      
51      public void testSingleLoanRequest() throws Exception
52      {
53          MuleClient client = new MuleClient();
54          Customer c = new Customer("Ross Mason", 1234);
55          CustomerQuoteRequest request = new CustomerQuoteRequest(c, 100000, 48);
56          // Send asynchronous request
57          client.dispatch("CustomerRequests", request, null);
58          
59          // Wait for asynchronous response
60          UMOMessage result = client.receive("CustomerResponses", getDelay());
61          assertNotNull("Result is null", result);
62          assertFalse("Result is null", result.getPayload() instanceof NullPayload);
63          assertTrue("Result should be LoanQuote but is " + result.getPayload().getClass().getName(), 
64                      result.getPayload() instanceof LoanQuote);
65          LoanQuote quote = (LoanQuote)result.getPayload();
66          assertTrue(quote.getInterestRate() > 0);
67      }
68  
69      public void testLotsOfLoanRequests() throws Exception
70      {
71          final MuleClient client = new MuleClient();
72          Customer c = new Customer("Ross Mason", 1234);
73          CustomerQuoteRequest[] requests = new CustomerQuoteRequest[3];
74          requests[0] = new CustomerQuoteRequest(c, 100000, 48);
75          requests[1] = new CustomerQuoteRequest(c, 1000, 12);
76          requests[2] = new CustomerQuoteRequest(c, 10, 24);
77  
78          final StopWatch stopWatch = new StopWatch();
79          int i = 0;
80          final int numRequests = getNumberOfRequests() + getWarmUpMessages();
81          final Latch latch = new Latch();
82          Thread thread = new Thread(new Runnable()
83              {
84  
85                  public void run()
86                  {
87                      try
88                      {
89                          UMOMessage result = null;
90                          for (int i = 0; i < numRequests; i++)
91                          {
92                              try
93                              {
94                                  result = client.receive("CustomerResponses", getDelay());
95                              }
96                              catch (UMOException e)
97                              {
98                                  fail(e.getMessage());
99                              }
100                             //System.out.println("Received: " + i);
101                             assertNotNull("Result is null", result);
102                             assertFalse("Result is null", result.getPayload() instanceof NullPayload);
103                             assertTrue("Result should be LoanQuote but is " + result.getPayload().getClass().getName(),
104                                         result.getPayload() instanceof LoanQuote);
105                             LoanQuote quote = (LoanQuote)result.getPayload();
106                             assertTrue(quote.getInterestRate() > 0);
107                         }
108                     }
109                     finally
110                     {
111                         latch.countDown();
112                     }
113 
114                 }
115             });
116             thread.start();
117         try
118         {
119             for (i = 0; i < numRequests; i++)
120             {
121                 if(i==getWarmUpMessages())
122                 {
123                     stopWatch.start();
124                 }
125                 client.dispatch("CustomerRequests", requests[i % 3], null);
126             }
127         }
128         finally
129         {
130             latch.await();
131             stopWatch.stop();
132             System.out.println("Total running time was: " + stopWatch.getTime() + "ms");
133             System.out.println("Requests processed was: " + i);
134             int mps = (int)(numRequests/((double)stopWatch.getTime()/(double)1000));
135             System.out.println("Msg/sec: " + mps + " (warm up msgs= " + getWarmUpMessages() + ")");
136         }
137     }
138 }