Commit | Line | Data |
---|---|---|
d18dd09b ASL |
1 | /******************************************************************************* |
2 | * Copyright (c) 2009, 2010 Ericsson | |
3 | * | |
4 | * All rights reserved. This program and the accompanying materials are | |
5 | * made available under the terms of the Eclipse Public License v1.0 which | |
6 | * accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | * Contributors: | |
10 | * Francois Chouinard - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.linuxtools.tmf.tests.request; | |
14 | ||
15 | import java.io.File; | |
16 | import java.io.IOException; | |
17 | import java.net.URISyntaxException; | |
18 | import java.net.URL; | |
19 | import java.util.Vector; | |
20 | ||
21 | import junit.framework.TestCase; | |
22 | ||
23 | import org.eclipse.core.runtime.FileLocator; | |
24 | import org.eclipse.core.runtime.Path; | |
25 | import org.eclipse.linuxtools.tmf.component.ITmfDataProvider; | |
26 | import org.eclipse.linuxtools.tmf.component.TmfProviderManager; | |
27 | import org.eclipse.linuxtools.tmf.event.TmfEvent; | |
2fb2eb37 FC |
28 | import org.eclipse.linuxtools.tmf.event.TmfEventReference; |
29 | import org.eclipse.linuxtools.tmf.event.TmfEventSource; | |
30 | import org.eclipse.linuxtools.tmf.event.TmfEventType; | |
d18dd09b ASL |
31 | import org.eclipse.linuxtools.tmf.event.TmfTimeRange; |
32 | import org.eclipse.linuxtools.tmf.event.TmfTimestamp; | |
2fb2eb37 | 33 | import org.eclipse.linuxtools.tmf.request.TmfCoalescedDataRequest; |
d18dd09b ASL |
34 | import org.eclipse.linuxtools.tmf.request.TmfCoalescedEventRequest; |
35 | import org.eclipse.linuxtools.tmf.request.TmfDataRequest; | |
2fb2eb37 | 36 | import org.eclipse.linuxtools.tmf.request.TmfDataRequestStub; |
d18dd09b ASL |
37 | import org.eclipse.linuxtools.tmf.request.TmfEventRequest; |
38 | import org.eclipse.linuxtools.tmf.request.TmfEventRequestStub; | |
39 | import org.eclipse.linuxtools.tmf.signal.TmfSignal; | |
40 | import org.eclipse.linuxtools.tmf.signal.TmfSignalHandler; | |
41 | import org.eclipse.linuxtools.tmf.signal.TmfSignalManager; | |
42 | import org.eclipse.linuxtools.tmf.tests.TmfCoreTestPlugin; | |
43 | import org.eclipse.linuxtools.tmf.trace.TmfTraceStub; | |
44 | ||
45 | /** | |
46 | * <b><u>TmfCoalescedEventRequestTest</u></b> | |
47 | * <p> | |
2fb2eb37 | 48 | * Test suite for the TmfCoalescedEventRequest class. |
d18dd09b ASL |
49 | */ |
50 | public class TmfCoalescedEventRequestTest extends TestCase { | |
51 | ||
2fb2eb37 FC |
52 | // ------------------------------------------------------------------------ |
53 | // Variables | |
54 | // ------------------------------------------------------------------------ | |
d18dd09b | 55 | |
2fb2eb37 FC |
56 | private static TmfTimeRange range1 = new TmfTimeRange(TmfTimeRange.Eternity); |
57 | private static TmfTimeRange range2 = new TmfTimeRange(new TmfTimestamp(), TmfTimestamp.BigCrunch); | |
58 | ||
59 | private static TmfCoalescedEventRequest<TmfEvent> fRequest1; | |
60 | private static TmfCoalescedEventRequest<TmfEvent> fRequest2; | |
61 | private static TmfCoalescedEventRequest<TmfEvent> fRequest3; | |
62 | private static TmfCoalescedEventRequest<TmfEvent> fRequest4; | |
d18dd09b | 63 | |
2fb2eb37 FC |
64 | private static TmfCoalescedEventRequest<TmfEvent> fRequest1b; |
65 | private static TmfCoalescedEventRequest<TmfEvent> fRequest1c; | |
d18dd09b | 66 | |
2fb2eb37 FC |
67 | private static int fRequestCount; |
68 | ||
69 | // ------------------------------------------------------------------------ | |
d18dd09b ASL |
70 | // Housekeeping |
71 | // ------------------------------------------------------------------------ | |
72 | ||
73 | public TmfCoalescedEventRequestTest(String name) { | |
74 | super(name); | |
75 | } | |
76 | ||
77 | @Override | |
78 | public void setUp() throws Exception { | |
79 | super.setUp(); | |
2fb2eb37 FC |
80 | TmfEventRequest.reset(); |
81 | fRequest1 = new TmfCoalescedEventRequest<TmfEvent>(TmfEvent.class, range1, 100, 200); | |
82 | fRequest2 = new TmfCoalescedEventRequest<TmfEvent>(TmfEvent.class, range2, 100, 200); | |
83 | fRequest3 = new TmfCoalescedEventRequest<TmfEvent>(TmfEvent.class, range2, 200, 200); | |
84 | fRequest4 = new TmfCoalescedEventRequest<TmfEvent>(TmfEvent.class, range2, 200, 300); | |
85 | ||
86 | fRequest1b = new TmfCoalescedEventRequest<TmfEvent>(TmfEvent.class, range1, 100, 200); | |
87 | fRequest1c = new TmfCoalescedEventRequest<TmfEvent>(TmfEvent.class, range1, 100, 200); | |
88 | ||
89 | fRequestCount = fRequest1c.getRequestId() + 1; | |
d18dd09b ASL |
90 | } |
91 | ||
92 | @Override | |
93 | public void tearDown() throws Exception { | |
94 | super.tearDown(); | |
95 | } | |
96 | ||
2fb2eb37 FC |
97 | private TmfCoalescedEventRequest<TmfEvent> setupTestRequest(final boolean[] flags) { |
98 | ||
99 | TmfCoalescedEventRequest<TmfEvent> request = new TmfCoalescedEventRequest<TmfEvent>(TmfEvent.class, range1, 100, 200) { | |
100 | @Override | |
101 | public void handleCompleted() { | |
102 | super.handleCompleted(); | |
103 | flags[0] = true; | |
104 | } | |
105 | @Override | |
106 | public void handleSuccess() { | |
107 | super.handleSuccess(); | |
108 | flags[1] = true; | |
109 | } | |
110 | @Override | |
111 | public void handleFailure() { | |
112 | super.handleFailure(); | |
113 | flags[2] = true; | |
114 | } | |
115 | @Override | |
116 | public void handleCancel() { | |
117 | super.handleCancel(); | |
118 | flags[3] = true; | |
119 | } | |
120 | }; | |
121 | return request; | |
122 | } | |
123 | ||
d18dd09b ASL |
124 | // ------------------------------------------------------------------------ |
125 | // Constructors | |
126 | // ------------------------------------------------------------------------ | |
127 | ||
128 | public void testTmfCoalescedEventRequest() { | |
2fb2eb37 | 129 | TmfCoalescedEventRequest<TmfEvent> request = new TmfCoalescedEventRequest<TmfEvent>(TmfEvent.class); |
d18dd09b | 130 | |
2fb2eb37 FC |
131 | assertEquals("getRequestId", fRequestCount++, request.getRequestId()); |
132 | assertEquals("getDataType", TmfEvent.class, request.getDataType()); | |
d18dd09b | 133 | |
2fb2eb37 FC |
134 | assertEquals("getRange", range1, request.getRange()); |
135 | assertEquals("getNbRequestedEvents", TmfEventRequest.ALL_DATA, request.getNbRequested()); | |
136 | assertEquals("getBlockize", TmfEventRequest.DEFAULT_BLOCK_SIZE, request.getBlockize()); | |
137 | ||
138 | assertFalse("isCompleted", request.isCompleted()); | |
139 | assertFalse("isFailed", request.isFailed()); | |
140 | assertFalse("isCancelled", request.isCancelled()); | |
141 | ||
142 | assertEquals("getNbRead", 0, request.getNbRead()); | |
d18dd09b ASL |
143 | } |
144 | ||
2fb2eb37 FC |
145 | public void testTmfCoalescedEventRequestIndex() { |
146 | TmfCoalescedEventRequest<TmfEvent> request = new TmfCoalescedEventRequest<TmfEvent>(TmfEvent.class, range1); | |
147 | ||
148 | assertEquals("getRequestId", fRequestCount++, request.getRequestId()); | |
149 | assertEquals("getDataType", TmfEvent.class, request.getDataType()); | |
150 | ||
151 | assertEquals("getRange", range1, request.getRange()); | |
152 | assertEquals("getNbRequestedEvents", TmfEventRequest.ALL_DATA, request.getNbRequested()); | |
153 | assertEquals("getBlockize", TmfEventRequest.DEFAULT_BLOCK_SIZE, request.getBlockize()); | |
d18dd09b | 154 | |
2fb2eb37 FC |
155 | assertFalse("isCompleted", request.isCompleted()); |
156 | assertFalse("isFailed", request.isFailed()); | |
157 | assertFalse("isCancelled", request.isCancelled()); | |
d18dd09b | 158 | |
2fb2eb37 | 159 | assertEquals("getNbRead", 0, request.getNbRead()); |
d18dd09b ASL |
160 | } |
161 | ||
2fb2eb37 FC |
162 | public void testTmfCoalescedEventRequestIndexNbRequested() { |
163 | TmfCoalescedEventRequest<TmfEvent> request = new TmfCoalescedEventRequest<TmfEvent>(TmfEvent.class, range1, 100); | |
d18dd09b | 164 | |
2fb2eb37 FC |
165 | assertEquals("getRequestId", fRequestCount++, request.getRequestId()); |
166 | assertEquals("getDataType", TmfEvent.class, request.getDataType()); | |
d18dd09b | 167 | |
2fb2eb37 | 168 | assertEquals("getRange", range1, request.getRange()); |
d18dd09b | 169 | assertEquals("getNbRequestedEvents", 100, request.getNbRequested()); |
2fb2eb37 FC |
170 | assertEquals("getBlockize", TmfEventRequest.DEFAULT_BLOCK_SIZE, request.getBlockize()); |
171 | ||
172 | assertFalse("isCompleted", request.isCompleted()); | |
173 | assertFalse("isFailed", request.isFailed()); | |
174 | assertFalse("isCancelled", request.isCancelled()); | |
175 | ||
176 | assertEquals("getNbRead", 0, request.getNbRead()); | |
d18dd09b ASL |
177 | } |
178 | ||
2fb2eb37 FC |
179 | public void testTmfCoalescedEventRequestIndexNbEventsBlocksize() { |
180 | TmfCoalescedEventRequest<TmfEvent> request = new TmfCoalescedEventRequest<TmfEvent>(TmfEvent.class, range1, 100, 200); | |
d18dd09b | 181 | |
2fb2eb37 FC |
182 | assertEquals("getRequestId", fRequestCount++, request.getRequestId()); |
183 | assertEquals("getDataType", TmfEvent.class, request.getDataType()); | |
d18dd09b | 184 | |
2fb2eb37 | 185 | assertEquals("getRange", range1, request.getRange()); |
d18dd09b | 186 | assertEquals("getNbRequestedEvents", 100, request.getNbRequested()); |
2fb2eb37 FC |
187 | assertEquals("getBlockize", 200, request.getBlockize()); |
188 | ||
189 | assertFalse("isCompleted", request.isCompleted()); | |
190 | assertFalse("isFailed", request.isFailed()); | |
191 | assertFalse("isCancelled", request.isCancelled()); | |
192 | ||
193 | assertEquals("getNbRead", 0, request.getNbRead()); | |
194 | } | |
195 | ||
196 | // ------------------------------------------------------------------------ | |
197 | // equals | |
198 | // ------------------------------------------------------------------------ | |
199 | ||
200 | public void testEqualsReflexivity() throws Exception { | |
201 | assertTrue("equals", fRequest1.equals(fRequest1)); | |
202 | assertTrue("equals", fRequest2.equals(fRequest2)); | |
203 | ||
204 | assertFalse("equals", fRequest1.equals(fRequest2)); | |
205 | assertFalse("equals", fRequest2.equals(fRequest1)); | |
206 | } | |
207 | ||
208 | public void testEqualsSymmetry() throws Exception { | |
209 | assertTrue("equals", fRequest1.equals(fRequest1b)); | |
210 | assertTrue("equals", fRequest1b.equals(fRequest1)); | |
211 | ||
212 | assertFalse("equals", fRequest1.equals(fRequest3)); | |
213 | assertFalse("equals", fRequest2.equals(fRequest3)); | |
214 | assertFalse("equals", fRequest3.equals(fRequest1)); | |
215 | assertFalse("equals", fRequest3.equals(fRequest2)); | |
216 | } | |
217 | ||
218 | public void testEqualsTransivity() throws Exception { | |
219 | assertTrue("equals", fRequest1.equals(fRequest1b)); | |
220 | assertTrue("equals", fRequest1b.equals(fRequest1c)); | |
221 | assertTrue("equals", fRequest1.equals(fRequest1c)); | |
222 | } | |
223 | ||
2fb2eb37 FC |
224 | public void testEqualsNull() throws Exception { |
225 | assertFalse("equals", fRequest1.equals(null)); | |
226 | assertFalse("equals", fRequest2.equals(null)); | |
227 | } | |
228 | ||
229 | public void testEqualsSuper() throws Exception { | |
230 | TmfCoalescedDataRequest<TmfEvent> dataRequest1 = new TmfCoalescedDataRequest<TmfEvent>( | |
231 | fRequest1.getDataType(), fRequest1.getIndex(), fRequest1.getNbRequested(), fRequest1.getBlockize()); | |
232 | TmfCoalescedDataRequest<TmfEvent> dataRequest2 = new TmfCoalescedDataRequest<TmfEvent>( | |
233 | fRequest1.getDataType(), fRequest1.getIndex(), fRequest1.getNbRequested(), fRequest1.getBlockize()); | |
234 | TmfCoalescedDataRequest<TmfEvent> dataRequest3 = new TmfCoalescedDataRequest<TmfEvent>( | |
235 | fRequest3.getDataType(), fRequest3.getIndex(), fRequest3.getNbRequested(), fRequest3.getBlockize()); | |
236 | ||
237 | assertTrue("equals", fRequest1.equals(dataRequest2)); | |
238 | assertTrue("equals", fRequest2.equals(dataRequest1)); | |
239 | assertFalse("equals", fRequest1.equals(dataRequest3)); | |
240 | assertFalse("equals", fRequest3.equals(dataRequest1)); | |
241 | } | |
242 | ||
243 | // ------------------------------------------------------------------------ | |
244 | // hashCode | |
245 | // ------------------------------------------------------------------------ | |
246 | ||
247 | public void testHashCode() throws Exception { | |
248 | assertTrue("hashCode", fRequest1.hashCode() == fRequest1.hashCode()); | |
249 | assertTrue("hashCode", fRequest2.hashCode() == fRequest2.hashCode()); | |
250 | assertTrue("hashCode", fRequest1.hashCode() != fRequest2.hashCode()); | |
251 | } | |
252 | ||
253 | // ------------------------------------------------------------------------ | |
254 | // toString | |
255 | // ------------------------------------------------------------------------ | |
256 | ||
257 | public void testToString() { | |
258 | String expected1 = "[TmfCoalescedEventRequest(0,TmfEvent," + range1 + ",100,200)]"; | |
259 | String expected2 = "[TmfCoalescedEventRequest(1,TmfEvent," + range2 + ",100,200)]"; | |
260 | String expected3 = "[TmfCoalescedEventRequest(2,TmfEvent," + range2 + ",200,200)]"; | |
261 | String expected4 = "[TmfCoalescedEventRequest(3,TmfEvent," + range2 + ",200,300)]"; | |
262 | ||
263 | assertEquals("toString", expected1, fRequest1.toString()); | |
264 | assertEquals("toString", expected2, fRequest2.toString()); | |
265 | assertEquals("toString", expected3, fRequest3.toString()); | |
266 | assertEquals("toString", expected4, fRequest4.toString()); | |
d18dd09b ASL |
267 | } |
268 | ||
269 | // ------------------------------------------------------------------------ | |
270 | // isCompatible | |
271 | // ------------------------------------------------------------------------ | |
272 | ||
273 | public void testIsCompatible() { | |
2fb2eb37 FC |
274 | TmfCoalescedEventRequest<TmfEvent> coalescedRequest = new TmfCoalescedEventRequest<TmfEvent>(TmfEvent.class, range1, 100, 200); |
275 | TmfEventRequest<TmfEvent> request1 = new TmfEventRequestStub<TmfEvent>(TmfEvent.class, range1, 100, 200); | |
276 | TmfEventRequest<TmfEvent> request2 = new TmfEventRequestStub<TmfEvent>(TmfEvent.class, range2, 100, 200); | |
277 | TmfEventRequest<TmfEvent> request3 = new TmfEventRequestStub<TmfEvent>(TmfEvent.class, range1, 101, 200); | |
278 | TmfEventRequest<TmfEvent> request4 = new TmfEventRequestStub<TmfEvent>(TmfEvent.class, range1, 100, 201); | |
279 | TmfDataRequest<TmfEvent> request5 = new TmfDataRequestStub<TmfEvent> (TmfEvent.class, 10, 100, 201); | |
d18dd09b ASL |
280 | |
281 | assertTrue ("isCompatible", coalescedRequest.isCompatible(request1)); | |
282 | assertFalse("isCompatible", coalescedRequest.isCompatible(request2)); | |
283 | assertFalse("isCompatible", coalescedRequest.isCompatible(request3)); | |
2fb2eb37 FC |
284 | assertFalse("isCompatible", coalescedRequest.isCompatible(request4)); |
285 | assertFalse("isCompatible", coalescedRequest.isCompatible(request5)); | |
d18dd09b ASL |
286 | } |
287 | ||
2fb2eb37 FC |
288 | // ------------------------------------------------------------------------ |
289 | // setData/getData | |
290 | // ------------------------------------------------------------------------ | |
291 | ||
292 | public void testSetData() { | |
293 | ||
294 | TmfCoalescedEventRequest<TmfEvent> coalescedRequest = new TmfCoalescedEventRequest<TmfEvent>(TmfEvent.class, range1, 100, 200); | |
295 | TmfEventRequest<TmfEvent> request1 = new TmfEventRequestStub<TmfEvent>(TmfEvent.class, range1, 100, 200); | |
296 | TmfEventRequest<TmfEvent> request2 = new TmfEventRequestStub<TmfEvent>(TmfEvent.class, range1, 100, 200); | |
297 | coalescedRequest.addRequest(request1); | |
298 | coalescedRequest.addRequest(request2); | |
299 | ||
300 | // Initialize the data | |
301 | int nbEvents = 10; | |
302 | TmfEvent[] events = new TmfEvent[nbEvents]; | |
303 | for (int i = 0; i < nbEvents; i++) { | |
304 | events[i] = new TmfEvent(new TmfTimestamp(i), new TmfEventSource(), | |
305 | new TmfEventType(), new TmfEventReference()); | |
306 | } | |
307 | ||
308 | coalescedRequest.setData(events); | |
309 | coalescedRequest.handleData(); | |
310 | ||
311 | // Validate the coalescing request | |
312 | assertEquals("setData", nbEvents, coalescedRequest.getNbRead()); | |
313 | TmfEvent[] eventsRead1 = coalescedRequest.getData(); | |
314 | assertEquals("getData", nbEvents, eventsRead1.length); | |
315 | for (int i = 0; i < nbEvents; i++) { | |
316 | assertEquals("getData", i, eventsRead1[i].getTimestamp().getValue()); | |
317 | } | |
318 | ||
319 | // Validate the first coalesced request | |
320 | assertEquals("setData", nbEvents, request1.getNbRead()); | |
321 | TmfEvent[] eventsRead2 = request1.getData(); | |
322 | assertEquals("getData", nbEvents, eventsRead2.length); | |
323 | for (int i = 0; i < nbEvents; i++) { | |
324 | assertEquals("getData", i, eventsRead2[i].getTimestamp().getValue()); | |
325 | } | |
326 | ||
327 | // Validate the second coalesced request | |
328 | assertEquals("setData", nbEvents, request2.getNbRead()); | |
329 | TmfEvent[] eventsRead3 = request2.getData(); | |
330 | assertEquals("getData", nbEvents, eventsRead3.length); | |
331 | for (int i = 0; i < nbEvents; i++) { | |
332 | assertEquals("getData", i, eventsRead3[i].getTimestamp().getValue()); | |
333 | } | |
334 | } | |
335 | ||
336 | // ------------------------------------------------------------------------ | |
337 | // done | |
338 | // ------------------------------------------------------------------------ | |
339 | ||
340 | public void testDone() { | |
341 | ||
342 | // Test request | |
343 | final boolean[] crFlags = new boolean[4]; | |
344 | TmfCoalescedEventRequest<TmfEvent> request = setupTestRequest(crFlags); | |
345 | TmfEventRequest<TmfEvent> subRequest1 = new TmfEventRequestStub<TmfEvent>(TmfEvent.class, range1, 100, 200); | |
346 | TmfEventRequest<TmfEvent> subRequest2 = new TmfEventRequestStub<TmfEvent>(TmfEvent.class, range1, 100, 200); | |
347 | request.addRequest(subRequest1); | |
348 | request.addRequest(subRequest2); | |
349 | ||
350 | request.done(); | |
351 | ||
352 | // Validate the coalescing request | |
353 | assertTrue ("isCompleted", request.isCompleted()); | |
354 | assertFalse("isFailed", request.isFailed()); | |
355 | assertFalse("isCancelled", request.isCancelled()); | |
356 | ||
357 | assertTrue ("handleCompleted", crFlags[0]); | |
358 | assertTrue ("handleSuccess", crFlags[1]); | |
359 | assertFalse("handleFailure", crFlags[2]); | |
360 | assertFalse("handleCancel", crFlags[3]); | |
361 | ||
362 | // Validate the first coalesced request | |
363 | assertTrue ("isCompleted", subRequest1.isCompleted()); | |
364 | assertFalse("isFailed", subRequest1.isFailed()); | |
365 | assertFalse("isCancelled", subRequest1.isCancelled()); | |
366 | ||
367 | // Validate the second coalesced request | |
368 | assertTrue ("isCompleted", subRequest2.isCompleted()); | |
369 | assertFalse("isFailed", subRequest2.isFailed()); | |
370 | assertFalse("isCancelled", subRequest2.isCancelled()); | |
371 | } | |
372 | ||
373 | // ------------------------------------------------------------------------ | |
374 | // fail | |
375 | // ------------------------------------------------------------------------ | |
376 | ||
377 | public void testFail() { | |
378 | ||
379 | final boolean[] crFlags = new boolean[4]; | |
380 | TmfCoalescedEventRequest<TmfEvent> request = setupTestRequest(crFlags); | |
381 | TmfEventRequest<TmfEvent> subRequest1 = new TmfEventRequestStub<TmfEvent>(TmfEvent.class, range1, 100, 200); | |
382 | TmfEventRequest<TmfEvent> subRequest2 = new TmfEventRequestStub<TmfEvent>(TmfEvent.class, range1, 100, 200); | |
383 | request.addRequest(subRequest1); | |
384 | request.addRequest(subRequest2); | |
385 | ||
386 | request.fail(); | |
387 | ||
388 | // Validate the coalescing request | |
389 | assertTrue ("isCompleted", request.isCompleted()); | |
390 | assertTrue ("isFailed", request.isFailed()); | |
391 | assertFalse("isCancelled", request.isCancelled()); | |
392 | ||
393 | assertTrue ("handleCompleted", crFlags[0]); | |
394 | assertFalse("handleSuccess", crFlags[1]); | |
395 | assertTrue ("handleFailure", crFlags[2]); | |
396 | assertFalse("handleCancel", crFlags[3]); | |
397 | ||
398 | // Validate the first coalesced request | |
399 | assertTrue ("isCompleted", subRequest1.isCompleted()); | |
400 | assertTrue ("isFailed", subRequest1.isFailed()); | |
401 | assertFalse("isCancelled", subRequest1.isCancelled()); | |
402 | ||
403 | // Validate the second coalesced request | |
404 | assertTrue ("isCompleted", subRequest2.isCompleted()); | |
405 | assertTrue ("isFailed", subRequest2.isFailed()); | |
406 | assertFalse("isCancelled", subRequest2.isCancelled()); | |
407 | } | |
408 | ||
409 | // ------------------------------------------------------------------------ | |
410 | // cancel | |
411 | // ------------------------------------------------------------------------ | |
412 | ||
413 | public void testCancel() { | |
414 | ||
415 | final boolean[] crFlags = new boolean[4]; | |
416 | TmfCoalescedEventRequest<TmfEvent> request = setupTestRequest(crFlags); | |
417 | TmfEventRequest<TmfEvent> subRequest1 = new TmfEventRequestStub<TmfEvent>(TmfEvent.class, range1, 100, 200); | |
418 | TmfEventRequest<TmfEvent> subRequest2 = new TmfEventRequestStub<TmfEvent>(TmfEvent.class, range1, 100, 200); | |
419 | request.addRequest(subRequest1); | |
420 | request.addRequest(subRequest2); | |
421 | ||
422 | request.cancel(); | |
423 | ||
424 | // Validate the coalescing request | |
425 | assertTrue ("isCompleted", request.isCompleted()); | |
426 | assertFalse("isFailed", request.isFailed()); | |
427 | assertTrue ("isCancelled", request.isCancelled()); | |
428 | ||
429 | assertTrue ("handleCompleted", crFlags[0]); | |
430 | assertFalse("handleSuccess", crFlags[1]); | |
431 | assertFalse("handleFailure", crFlags[2]); | |
432 | assertTrue ("handleCancel", crFlags[3]); | |
433 | ||
434 | // Validate the first coalesced request | |
435 | assertTrue ("isCompleted", subRequest1.isCompleted()); | |
436 | assertFalse("isFailed", subRequest1.isFailed()); | |
437 | assertTrue ("isCancelled", subRequest1.isCancelled()); | |
438 | ||
439 | // Validate the second coalesced request | |
440 | assertTrue ("isCompleted", subRequest2.isCompleted()); | |
441 | assertFalse("isFailed", subRequest2.isFailed()); | |
442 | assertTrue ("isCancelled", subRequest2.isCancelled()); | |
443 | } | |
444 | ||
445 | // ------------------------------------------------------------------------ | |
446 | // waitForCompletion | |
447 | // ------------------------------------------------------------------------ | |
448 | ||
d18dd09b ASL |
449 | // ------------------------------------------------------------------------ |
450 | // Coalescing | |
451 | // ------------------------------------------------------------------------ | |
452 | ||
2fb2eb37 FC |
453 | private static final String DIRECTORY = "testfiles"; |
454 | private static final String TEST_STREAM = "A-Test-10K"; | |
455 | private static final int NB_EVENTS = 10000; | |
456 | private static final int BLOCK_SIZE = 100; | |
457 | ||
458 | // Initialize the test trace | |
459 | private static TmfTraceStub fTrace = null; | |
ff4ed569 | 460 | private synchronized TmfTraceStub setupTrace(String path) { |
2fb2eb37 FC |
461 | if (fTrace == null) { |
462 | try { | |
463 | URL location = FileLocator.find(TmfCoreTestPlugin.getDefault().getBundle(), new Path(path), null); | |
464 | File test = new File(FileLocator.toFileURL(location).toURI()); | |
ff4ed569 | 465 | fTrace = new TmfTraceStub(test.getPath(), 500); |
2fb2eb37 FC |
466 | } catch (URISyntaxException e) { |
467 | e.printStackTrace(); | |
468 | } catch (IOException e) { | |
469 | e.printStackTrace(); | |
470 | } | |
471 | } | |
472 | return fTrace; | |
473 | } | |
474 | ||
d18dd09b ASL |
475 | Vector<TmfEvent> requestedEvents1; |
476 | Vector<TmfEvent> requestedEvents2; | |
477 | Vector<TmfEvent> requestedEvents3; | |
478 | ||
479 | TmfEventRequest<TmfEvent> request1; | |
480 | TmfEventRequest<TmfEvent> request2; | |
481 | TmfEventRequest<TmfEvent> request3; | |
482 | ||
2fb2eb37 FC |
483 | ITmfDataProvider<TmfEvent>[] providers; |
484 | ||
d18dd09b ASL |
485 | private class TmfTestTriggerSignal extends TmfSignal { |
486 | public final boolean forceCancel; | |
487 | public TmfTestTriggerSignal(Object source, boolean cancel) { | |
488 | super(source); | |
489 | forceCancel = cancel; | |
2fb2eb37 | 490 | |
d18dd09b ASL |
491 | } |
492 | } | |
493 | ||
494 | @SuppressWarnings("unchecked") | |
495 | @TmfSignalHandler | |
496 | public void trigger(final TmfTestTriggerSignal signal) { | |
497 | ||
498 | TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BigBang, TmfTimestamp.BigCrunch); | |
499 | ||
500 | requestedEvents1 = new Vector<TmfEvent>(); | |
501 | request1 = new TmfEventRequest<TmfEvent>(TmfEvent.class, range, NB_EVENTS, BLOCK_SIZE) { | |
502 | @Override | |
503 | public void handleData() { | |
504 | if (!isCompleted()) { | |
505 | TmfEvent[] events = getData(); | |
506 | for (TmfEvent e : events) { | |
507 | requestedEvents1.add(e); | |
508 | } | |
509 | if (signal.forceCancel) | |
510 | cancel(); | |
511 | } | |
512 | } | |
513 | }; | |
514 | ||
515 | requestedEvents2 = new Vector<TmfEvent>(); | |
516 | request2 = new TmfEventRequest<TmfEvent>(TmfEvent.class, range, NB_EVENTS, BLOCK_SIZE) { | |
517 | @Override | |
518 | public void handleData() { | |
519 | if (!isCompleted()) { | |
520 | TmfEvent[] events = getData(); | |
521 | for (TmfEvent e : events) { | |
522 | requestedEvents2.add(e); | |
523 | } | |
524 | } | |
525 | } | |
526 | }; | |
527 | ||
528 | requestedEvents3 = new Vector<TmfEvent>(); | |
529 | request3 = new TmfEventRequest<TmfEvent>(TmfEvent.class, range, NB_EVENTS, BLOCK_SIZE) { | |
530 | @Override | |
531 | public void handleData() { | |
532 | if (!isCompleted()) { | |
533 | TmfEvent[] events = getData(); | |
534 | for (TmfEvent e : events) { | |
535 | requestedEvents3.add(e); | |
536 | } | |
537 | } | |
538 | } | |
539 | }; | |
540 | ||
2fb2eb37 | 541 | providers = (ITmfDataProvider<TmfEvent>[]) TmfProviderManager.getProviders(TmfEvent.class, TmfTraceStub.class); |
d18dd09b ASL |
542 | providers[0].sendRequest(request1); |
543 | providers[0].sendRequest(request2); | |
544 | providers[0].sendRequest(request3); | |
545 | } | |
546 | ||
547 | public void testCoalescedRequest() throws Exception { | |
548 | ||
2fb2eb37 | 549 | fTrace = setupTrace(DIRECTORY + File.separator + TEST_STREAM); |
d18dd09b ASL |
550 | |
551 | TmfSignalManager.register(this); | |
552 | TmfTestTriggerSignal signal = new TmfTestTriggerSignal(this, false); | |
553 | TmfSignalManager.dispatchSignal(signal); | |
554 | ||
555 | request1.waitForCompletion(); | |
556 | request2.waitForCompletion(); | |
557 | request3.waitForCompletion(); | |
558 | ||
559 | assertEquals("Request1: nbEvents", NB_EVENTS, requestedEvents1.size()); | |
560 | assertTrue ("Request1: isCompleted", request1.isCompleted()); | |
561 | assertFalse ("Request1: isCancelled", request1.isCancelled()); | |
562 | ||
563 | assertEquals("Request2: nbEvents", NB_EVENTS, requestedEvents2.size()); | |
564 | assertTrue ("Request2: isCompleted", request2.isCompleted()); | |
565 | assertFalse ("Request2: isCancelled", request2.isCancelled()); | |
566 | ||
567 | assertEquals("Request3: nbEvents", NB_EVENTS, requestedEvents3.size()); | |
568 | assertTrue ("Request3: isCompleted", request3.isCompleted()); | |
569 | assertFalse ("Request3: isCancelled", request3.isCancelled()); | |
570 | ||
571 | // Ensure that we have distinct events. | |
572 | // Don't go overboard: we are not validating the stub! | |
573 | for (int i = 0; i < NB_EVENTS; i++) { | |
574 | assertEquals("Distinct events", i+1, requestedEvents1.get(i).getTimestamp().getValue()); | |
575 | assertEquals("Distinct events", i+1, requestedEvents2.get(i).getTimestamp().getValue()); | |
576 | assertEquals("Distinct events", i+1, requestedEvents3.get(i).getTimestamp().getValue()); | |
577 | } | |
578 | ||
579 | TmfSignalManager.deregister(this); | |
2fb2eb37 FC |
580 | fTrace.dispose(); |
581 | fTrace = null; | |
d18dd09b ASL |
582 | } |
583 | ||
584 | public void testCancelCoalescedRequest() throws Exception { | |
2fb2eb37 FC |
585 | |
586 | fTrace = setupTrace(DIRECTORY + File.separator + TEST_STREAM); | |
d18dd09b ASL |
587 | |
588 | TmfSignalManager.register(this); | |
589 | TmfTestTriggerSignal signal = new TmfTestTriggerSignal(this, true); | |
590 | TmfSignalManager.dispatchSignal(signal); | |
591 | ||
592 | request1.waitForCompletion(); | |
593 | request2.waitForCompletion(); | |
594 | request3.waitForCompletion(); | |
595 | ||
596 | assertEquals("Request1: nbEvents", BLOCK_SIZE, requestedEvents1.size()); | |
597 | assertTrue ("Request1: isCompleted", request1.isCompleted()); | |
598 | assertTrue ("Request1: isCancelled", request1.isCancelled()); | |
599 | ||
600 | assertEquals("Request2: nbEvents", NB_EVENTS, requestedEvents2.size()); | |
601 | assertTrue ("Request2: isCompleted", request2.isCompleted()); | |
602 | assertFalse ("Request2: isCancelled", request2.isCancelled()); | |
603 | ||
604 | assertEquals("Request3: nbEvents", NB_EVENTS, requestedEvents3.size()); | |
605 | assertTrue ("Request3: isCompleted", request3.isCompleted()); | |
606 | assertFalse ("Request3: isCancelled", request3.isCancelled()); | |
607 | ||
608 | // Ensure that we have distinct events. | |
609 | // Don't go overboard: we are not validating the stub! | |
610 | for (int i = 0; i < NB_EVENTS; i++) { | |
611 | assertEquals("Distinct events", i+1, requestedEvents2.get(i).getTimestamp().getValue()); | |
612 | assertEquals("Distinct events", i+1, requestedEvents3.get(i).getTimestamp().getValue()); | |
613 | } | |
614 | ||
615 | TmfSignalManager.deregister(this); | |
2fb2eb37 FC |
616 | fTrace.dispose(); |
617 | fTrace = null; | |
d18dd09b ASL |
618 | } |
619 | ||
620 | } |