1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
64
65 CustomLoadStimulus loadStimulus = getLoadTestFixture()
66 .getLoadTestConfig()
67 .getLoadStimulus()
68 .getCustomLoadStimulus();
69 int iterations = loadStimulus.getNumberOfMsgs();
70
71
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
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 }