View Javadoc

1   /*
2    * Copyright (c) 2004, RV Test Team
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions are met:
7    *
8    * Redistributions of source code must retain the above copyright notice, this
9    * list of conditions and the following disclaimer.
10   *
11   * Redistributions in binary form must reproduce the above copyright notice,
12   * this list of conditions and the following disclaimer in the documentation
13   * and/or other materials provided with the distribution.
14   *
15   * Neither the name of the "RV Test Team" nor the names of its contributors may
16   * be used to endorse or promote products derived from this software without
17   * specific prior written permission.
18   *
19   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
29   * THE POSSIBILITY OF SUCH DAMAGE.
30   *
31   */
32  //Created on Sep 30, 2004
33  package com.reuters.msgtest.load;
34  
35  import EDU.oswego.cs.dl.util.concurrent.CountDown;
36  import com.reuters.msgtest.RecorderEntry;
37  import com.reuters.msgtest.RvTestException;
38  import com.reuters.msgtest.fixture.Response;
39  import com.reuters.msgtest.load.config.LoadResponse;
40  import java.io.File;
41  import java.util.Iterator;
42  import java.util.List;
43  
44  
45  /***
46   * @author <a href="mailto:Kurman.Karabukaev@thoughtworks.com">Kurman </a>
47   *
48   */
49  abstract public class CustomStimuliLoadTestCase extends LoadTest
50      implements CustomStimuliLoadTest {
51      private long TIMEOUT_TIME = 20000;
52  
53      public CustomStimuliLoadTestCase(String name, File file) {
54          super(name, file);
55      }
56  
57      public void processStimuli() throws RvTestException {
58          if (!getLoadTestFixture().getLoadTestConfig().getLoadStimulus()
59                       .isCustomLoadStimulus()) {
60              throw new RvTestException("Test does not have custom stimulus");
61          }
62  
63          // TODO provide strategy for various stimulus(Fixture/Custom) instead of
64          // nesting it.
65          CustomLoadStimulus loadStimulus = getLoadTestFixture()
66                                                .getLoadTestConfig()
67                                                .getLoadStimulus()
68                                                .getCustomLoadStimulus();
69          int iterations = loadStimulus.getNumberOfMsgs();
70  
71          // initlialize
72          Runner[] runners = new Runner[iterations];
73          CountDown countDown = new CountDown(iterations);
74  
75          for (int i = 0; i < iterations; i++) {
76              runners[i] = new Runner(nextExecutionUnit(), countDown,
77                      getLoadTestResults());
78          }
79  
80          // run
81          try {
82              getLoadTestResults().setTestStartTime(System.currentTimeMillis());
83  
84              for (int i = 0; i < runners.length; i++) {
85                  runners[i].run();
86              }
87  
88              delay(loadStimulus.getTestDelay());
89          } finally {
90              try {
91                  countDown.attempt(TIMEOUT_TIME);
92              } catch (InterruptedException e) {
93              }
94          }
95      }
96  
97      public void getResponses() throws RvTestException {
98          List loadResponses = getLoadTestFixture().getLoadTestConfig()
99                                   .getLoadResponses().getLoadResponseList();
100 
101         for (Iterator iter = loadResponses.iterator(); iter.hasNext();) {
102             LoadResponse loadResponse = (LoadResponse) iter.next();
103             String loadResponseMessageName = loadResponse.getMessageName();
104             Response rvFixtureResponse = getLoadTestFixture().getResponses()
105                                              .getResponse(loadResponseMessageName);
106 
107             String receiveSubject = rvFixtureResponse.getTibrvMsg()
108                                                      .getSendSubject();
109             List receivedRecords = getRecorder().getRecorderEntries(receiveSubject);
110 
111             assertEquals("Number of expected messages is not the same as it was received.",
112                 loadResponse.getNumExpected(), receivedRecords.size());
113             assertEquals("Number of sent stimuli the same as it was received.",
114                 getLoadTestResults().size(), receivedRecords.size());
115 
116             int loop = 0;
117 
118             for (Iterator iterator = receivedRecords.iterator();
119                     iterator.hasNext(); loop++) {
120                 RecorderEntry recordedEntry = (RecorderEntry) iterator.next();
121                 LoadTestResult result = getLoadTestResults().get(loop);
122 
123                 if (result == LoadTestResult.NONE) {
124                     continue;
125                 }
126 
127                 result.setReceiveTime(recordedEntry.getTime());
128             }
129         }
130     }
131 
132     class Runner implements Runnable {
133         private LoadTestResults testResults;
134         private CountDown work;
135         private ExecutionUnit executionUnit;
136 
137         public Runner(ExecutionUnit executionUnit, CountDown work,
138             LoadTestResults testResults) {
139             this.executionUnit = executionUnit;
140             this.work = work;
141             this.testResults = testResults;
142         }
143 
144         public void run() {
145             Throwable error = null;
146             LoadTestResult result = new LoadTestResult(System.currentTimeMillis());
147 
148             try {
149                 executionUnit.execute();
150             } catch (Throwable e) {
151                 error = e;
152             } finally {
153                 testResults.addLoadTestResult(result);
154                 work.release();
155 
156                 if (error != null) {
157                     result.setError(error);
158                 }
159             }
160         }
161     }
162 }