1 /*******************************************************************************
2 * Copyright (c) 2009, 2010 Ericsson
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
10 * Francois Chouinard - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.tests
.request
;
16 import java
.io
.IOException
;
17 import java
.net
.URISyntaxException
;
19 import java
.util
.Vector
;
21 import junit
.framework
.TestCase
;
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
;
28 import org
.eclipse
.linuxtools
.tmf
.event
.TmfTimeRange
;
29 import org
.eclipse
.linuxtools
.tmf
.event
.TmfTimestamp
;
30 import org
.eclipse
.linuxtools
.tmf
.request
.TmfCoalescedDataRequest
;
31 import org
.eclipse
.linuxtools
.tmf
.request
.TmfCoalescedEventRequest
;
32 import org
.eclipse
.linuxtools
.tmf
.request
.TmfEventRequest
;
33 import org
.eclipse
.linuxtools
.tmf
.request
.TmfEventRequestStub
;
34 import org
.eclipse
.linuxtools
.tmf
.signal
.TmfSignal
;
35 import org
.eclipse
.linuxtools
.tmf
.signal
.TmfSignalHandler
;
36 import org
.eclipse
.linuxtools
.tmf
.signal
.TmfSignalManager
;
37 import org
.eclipse
.linuxtools
.tmf
.tests
.TmfCoreTestPlugin
;
38 import org
.eclipse
.linuxtools
.tmf
.trace
.TmfTraceStub
;
41 * <b><u>TmfCoalescedEventRequestTest</u></b>
43 * Test suite for the TmfCoalescedEventRequest class.
45 @SuppressWarnings("nls")
46 public class TmfCoalescedEventRequestTest
extends TestCase
{
48 // ------------------------------------------------------------------------
50 // ------------------------------------------------------------------------
52 private static TmfTimeRange range1
= new TmfTimeRange(TmfTimeRange
.Eternity
);
53 private static TmfTimeRange range2
= new TmfTimeRange(new TmfTimestamp(), TmfTimestamp
.BigCrunch
);
55 private static TmfCoalescedEventRequest
<TmfEvent
> fRequest1
;
56 private static TmfCoalescedEventRequest
<TmfEvent
> fRequest2
;
57 private static TmfCoalescedEventRequest
<TmfEvent
> fRequest3
;
58 private static TmfCoalescedEventRequest
<TmfEvent
> fRequest4
;
60 private static TmfCoalescedEventRequest
<TmfEvent
> fRequest1b
;
61 private static TmfCoalescedEventRequest
<TmfEvent
> fRequest1c
;
63 private static int fRequestCount
;
65 // ------------------------------------------------------------------------
67 // ------------------------------------------------------------------------
69 public TmfCoalescedEventRequestTest(String name
) {
74 public void setUp() throws Exception
{
76 TmfEventRequest
.reset();
77 fRequest1
= new TmfCoalescedEventRequest
<TmfEvent
>(TmfEvent
.class, range1
, 100, 200);
78 fRequest2
= new TmfCoalescedEventRequest
<TmfEvent
>(TmfEvent
.class, range2
, 100, 200);
79 fRequest3
= new TmfCoalescedEventRequest
<TmfEvent
>(TmfEvent
.class, range2
, 200, 200);
80 fRequest4
= new TmfCoalescedEventRequest
<TmfEvent
>(TmfEvent
.class, range2
, 200, 300);
82 fRequest1b
= new TmfCoalescedEventRequest
<TmfEvent
>(TmfEvent
.class, range1
, 100, 200);
83 fRequest1c
= new TmfCoalescedEventRequest
<TmfEvent
>(TmfEvent
.class, range1
, 100, 200);
85 fRequestCount
= fRequest1c
.getRequestId() + 1;
89 public void tearDown() throws Exception
{
93 private TmfCoalescedEventRequest
<TmfEvent
> setupTestRequest(final boolean[] flags
) {
95 TmfCoalescedEventRequest
<TmfEvent
> request
= new TmfCoalescedEventRequest
<TmfEvent
>(TmfEvent
.class, range1
, 100, 200) {
97 public void handleCompleted() {
98 super.handleCompleted();
102 public void handleSuccess() {
103 super.handleSuccess();
107 public void handleFailure() {
108 super.handleFailure();
112 public void handleCancel() {
113 super.handleCancel();
120 // ------------------------------------------------------------------------
122 // ------------------------------------------------------------------------
124 public void testTmfCoalescedEventRequest() {
125 TmfCoalescedEventRequest
<TmfEvent
> request
= new TmfCoalescedEventRequest
<TmfEvent
>(TmfEvent
.class);
127 assertEquals("getRequestId", fRequestCount
++, request
.getRequestId());
128 assertEquals("getDataType", TmfEvent
.class, request
.getDataType());
130 assertEquals("getRange", range1
, request
.getRange());
131 assertEquals("getNbRequestedEvents", TmfEventRequest
.ALL_DATA
, request
.getNbRequested());
133 assertFalse("isCompleted", request
.isCompleted());
134 assertFalse("isFailed", request
.isFailed());
135 assertFalse("isCancelled", request
.isCancelled());
137 assertEquals("getNbRead", 0, request
.getNbRead());
140 public void testTmfCoalescedEventRequestIndex() {
141 TmfCoalescedEventRequest
<TmfEvent
> request
= new TmfCoalescedEventRequest
<TmfEvent
>(TmfEvent
.class, range1
);
143 assertEquals("getRequestId", fRequestCount
++, request
.getRequestId());
144 assertEquals("getDataType", TmfEvent
.class, request
.getDataType());
146 assertEquals("getRange", range1
, request
.getRange());
147 assertEquals("getNbRequestedEvents", TmfEventRequest
.ALL_DATA
, request
.getNbRequested());
149 assertFalse("isCompleted", request
.isCompleted());
150 assertFalse("isFailed", request
.isFailed());
151 assertFalse("isCancelled", request
.isCancelled());
153 assertEquals("getNbRead", 0, request
.getNbRead());
156 public void testTmfCoalescedEventRequestIndexNbRequested() {
157 TmfCoalescedEventRequest
<TmfEvent
> request
= new TmfCoalescedEventRequest
<TmfEvent
>(TmfEvent
.class, range1
, 100);
159 assertEquals("getRequestId", fRequestCount
++, request
.getRequestId());
160 assertEquals("getDataType", TmfEvent
.class, request
.getDataType());
162 assertEquals("getRange", range1
, request
.getRange());
163 assertEquals("getNbRequestedEvents", 100, request
.getNbRequested());
165 assertFalse("isCompleted", request
.isCompleted());
166 assertFalse("isFailed", request
.isFailed());
167 assertFalse("isCancelled", request
.isCancelled());
169 assertEquals("getNbRead", 0, request
.getNbRead());
172 public void testTmfCoalescedEventRequestIndexNbEventsBlocksize() {
173 TmfCoalescedEventRequest
<TmfEvent
> request
= new TmfCoalescedEventRequest
<TmfEvent
>(TmfEvent
.class, range1
, 100, 200);
175 assertEquals("getRequestId", fRequestCount
++, request
.getRequestId());
176 assertEquals("getDataType", TmfEvent
.class, request
.getDataType());
178 assertEquals("getRange", range1
, request
.getRange());
179 assertEquals("getNbRequestedEvents", 100, request
.getNbRequested());
181 assertFalse("isCompleted", request
.isCompleted());
182 assertFalse("isFailed", request
.isFailed());
183 assertFalse("isCancelled", request
.isCancelled());
185 assertEquals("getNbRead", 0, request
.getNbRead());
188 // ------------------------------------------------------------------------
190 // ------------------------------------------------------------------------
192 public void testEqualsReflexivity() throws Exception
{
193 assertTrue("equals", fRequest1
.equals(fRequest1
));
194 assertTrue("equals", fRequest2
.equals(fRequest2
));
196 assertFalse("equals", fRequest1
.equals(fRequest2
));
197 assertFalse("equals", fRequest2
.equals(fRequest1
));
200 public void testEqualsSymmetry() throws Exception
{
201 assertTrue("equals", fRequest1
.equals(fRequest1b
));
202 assertTrue("equals", fRequest1b
.equals(fRequest1
));
204 assertFalse("equals", fRequest1
.equals(fRequest3
));
205 assertFalse("equals", fRequest2
.equals(fRequest3
));
206 assertFalse("equals", fRequest3
.equals(fRequest1
));
207 assertFalse("equals", fRequest3
.equals(fRequest2
));
210 public void testEqualsTransivity() throws Exception
{
211 assertTrue("equals", fRequest1
.equals(fRequest1b
));
212 assertTrue("equals", fRequest1b
.equals(fRequest1c
));
213 assertTrue("equals", fRequest1
.equals(fRequest1c
));
216 public void testEqualsNull() throws Exception
{
217 assertFalse("equals", fRequest1
.equals(null));
218 assertFalse("equals", fRequest2
.equals(null));
221 public void testEqualsSuper() throws Exception
{
222 TmfCoalescedDataRequest
<TmfEvent
> dataRequest1
= new TmfCoalescedDataRequest
<TmfEvent
>(
223 fRequest1
.getDataType(), fRequest1
.getIndex(), fRequest1
.getNbRequested());
224 TmfCoalescedDataRequest
<TmfEvent
> dataRequest2
= new TmfCoalescedDataRequest
<TmfEvent
>(
225 fRequest1
.getDataType(), fRequest1
.getIndex(), fRequest1
.getNbRequested());
226 TmfCoalescedDataRequest
<TmfEvent
> dataRequest3
= new TmfCoalescedDataRequest
<TmfEvent
>(
227 fRequest3
.getDataType(), fRequest3
.getIndex(), fRequest3
.getNbRequested());
229 assertTrue("equals", fRequest1
.equals(dataRequest2
));
230 assertTrue("equals", fRequest2
.equals(dataRequest1
));
231 assertFalse("equals", fRequest1
.equals(dataRequest3
));
232 assertFalse("equals", fRequest3
.equals(dataRequest1
));
235 // ------------------------------------------------------------------------
237 // ------------------------------------------------------------------------
239 public void testHashCode() throws Exception
{
240 assertTrue("hashCode", fRequest1
.hashCode() == fRequest1
.hashCode());
241 assertTrue("hashCode", fRequest2
.hashCode() == fRequest2
.hashCode());
242 assertTrue("hashCode", fRequest1
.hashCode() != fRequest2
.hashCode());
245 // ------------------------------------------------------------------------
247 // ------------------------------------------------------------------------
249 public void testToString() {
250 String expected1
= "[TmfCoalescedEventRequest(0,TmfEvent," + range1
+ ",100,200)]";
251 String expected2
= "[TmfCoalescedEventRequest(1,TmfEvent," + range2
+ ",100,200)]";
252 String expected3
= "[TmfCoalescedEventRequest(2,TmfEvent," + range2
+ ",200,200)]";
253 String expected4
= "[TmfCoalescedEventRequest(3,TmfEvent," + range2
+ ",200,300)]";
255 assertEquals("toString", expected1
, fRequest1
.toString());
256 assertEquals("toString", expected2
, fRequest2
.toString());
257 assertEquals("toString", expected3
, fRequest3
.toString());
258 assertEquals("toString", expected4
, fRequest4
.toString());
261 // ------------------------------------------------------------------------
263 // ------------------------------------------------------------------------
265 public void testIsCompatible() {
266 TmfCoalescedEventRequest
<TmfEvent
> coalescedRequest
= new TmfCoalescedEventRequest
<TmfEvent
>(TmfEvent
.class, range1
, 100, 200);
267 TmfEventRequest
<TmfEvent
> request1
= new TmfEventRequestStub
<TmfEvent
>(TmfEvent
.class, range1
, 100, 200);
268 TmfEventRequest
<TmfEvent
> request2
= new TmfEventRequestStub
<TmfEvent
>(TmfEvent
.class, range2
, 100, 200);
269 TmfEventRequest
<TmfEvent
> request3
= new TmfEventRequestStub
<TmfEvent
>(TmfEvent
.class, range1
, 101, 200);
271 assertTrue ("isCompatible", coalescedRequest
.isCompatible(request1
));
272 assertTrue ("isCompatible", coalescedRequest
.isCompatible(request2
));
273 assertFalse("isCompatible", coalescedRequest
.isCompatible(request3
));
276 // ------------------------------------------------------------------------
278 // ------------------------------------------------------------------------
280 public void testDone() {
283 final boolean[] crFlags
= new boolean[4];
284 TmfCoalescedEventRequest
<TmfEvent
> request
= setupTestRequest(crFlags
);
285 TmfEventRequest
<TmfEvent
> subRequest1
= new TmfEventRequestStub
<TmfEvent
>(TmfEvent
.class, range1
, 100, 200);
286 TmfEventRequest
<TmfEvent
> subRequest2
= new TmfEventRequestStub
<TmfEvent
>(TmfEvent
.class, range1
, 100, 200);
287 request
.addRequest(subRequest1
);
288 request
.addRequest(subRequest2
);
292 // Validate the coalescing request
293 assertTrue ("isCompleted", request
.isCompleted());
294 assertFalse("isFailed", request
.isFailed());
295 assertFalse("isCancelled", request
.isCancelled());
297 assertTrue ("handleCompleted", crFlags
[0]);
298 assertTrue ("handleSuccess", crFlags
[1]);
299 assertFalse("handleFailure", crFlags
[2]);
300 assertFalse("handleCancel", crFlags
[3]);
302 // Validate the first coalesced request
303 assertTrue ("isCompleted", subRequest1
.isCompleted());
304 assertFalse("isFailed", subRequest1
.isFailed());
305 assertFalse("isCancelled", subRequest1
.isCancelled());
307 // Validate the second coalesced request
308 assertTrue ("isCompleted", subRequest2
.isCompleted());
309 assertFalse("isFailed", subRequest2
.isFailed());
310 assertFalse("isCancelled", subRequest2
.isCancelled());
313 // ------------------------------------------------------------------------
315 // ------------------------------------------------------------------------
317 public void testFail() {
319 final boolean[] crFlags
= new boolean[4];
320 TmfCoalescedEventRequest
<TmfEvent
> request
= setupTestRequest(crFlags
);
321 TmfEventRequest
<TmfEvent
> subRequest1
= new TmfEventRequestStub
<TmfEvent
>(TmfEvent
.class, range1
, 100, 200);
322 TmfEventRequest
<TmfEvent
> subRequest2
= new TmfEventRequestStub
<TmfEvent
>(TmfEvent
.class, range1
, 100, 200);
323 request
.addRequest(subRequest1
);
324 request
.addRequest(subRequest2
);
328 // Validate the coalescing request
329 assertTrue ("isCompleted", request
.isCompleted());
330 assertTrue ("isFailed", request
.isFailed());
331 assertFalse("isCancelled", request
.isCancelled());
333 assertTrue ("handleCompleted", crFlags
[0]);
334 assertFalse("handleSuccess", crFlags
[1]);
335 assertTrue ("handleFailure", crFlags
[2]);
336 assertFalse("handleCancel", crFlags
[3]);
338 // Validate the first coalesced request
339 assertTrue ("isCompleted", subRequest1
.isCompleted());
340 assertTrue ("isFailed", subRequest1
.isFailed());
341 assertFalse("isCancelled", subRequest1
.isCancelled());
343 // Validate the second coalesced request
344 assertTrue ("isCompleted", subRequest2
.isCompleted());
345 assertTrue ("isFailed", subRequest2
.isFailed());
346 assertFalse("isCancelled", subRequest2
.isCancelled());
349 // ------------------------------------------------------------------------
351 // ------------------------------------------------------------------------
353 public void testCancel() {
355 final boolean[] crFlags
= new boolean[4];
356 TmfCoalescedEventRequest
<TmfEvent
> request
= setupTestRequest(crFlags
);
357 TmfEventRequest
<TmfEvent
> subRequest1
= new TmfEventRequestStub
<TmfEvent
>(TmfEvent
.class, range1
, 100, 200);
358 TmfEventRequest
<TmfEvent
> subRequest2
= new TmfEventRequestStub
<TmfEvent
>(TmfEvent
.class, range1
, 100, 200);
359 request
.addRequest(subRequest1
);
360 request
.addRequest(subRequest2
);
364 // Validate the coalescing request
365 assertTrue ("isCompleted", request
.isCompleted());
366 assertFalse("isFailed", request
.isFailed());
367 assertTrue ("isCancelled", request
.isCancelled());
369 assertTrue ("handleCompleted", crFlags
[0]);
370 assertFalse("handleSuccess", crFlags
[1]);
371 assertFalse("handleFailure", crFlags
[2]);
372 assertTrue ("handleCancel", crFlags
[3]);
374 // Validate the first coalesced request
375 assertTrue ("isCompleted", subRequest1
.isCompleted());
376 assertFalse("isFailed", subRequest1
.isFailed());
377 assertTrue ("isCancelled", subRequest1
.isCancelled());
379 // Validate the second coalesced request
380 assertTrue ("isCompleted", subRequest2
.isCompleted());
381 assertFalse("isFailed", subRequest2
.isFailed());
382 assertTrue ("isCancelled", subRequest2
.isCancelled());
385 // ------------------------------------------------------------------------
387 // ------------------------------------------------------------------------
389 // ------------------------------------------------------------------------
391 // ------------------------------------------------------------------------
393 private static final String DIRECTORY
= "testfiles";
394 private static final String TEST_STREAM
= "A-Test-10K";
395 private static final int NB_EVENTS
= 10000;
396 private static final int BLOCK_SIZE
= 100;
398 // Initialize the test trace
399 private static TmfTraceStub fTrace
= null;
400 private synchronized TmfTraceStub
setupTrace(String path
) {
401 if (fTrace
== null) {
403 URL location
= FileLocator
.find(TmfCoreTestPlugin
.getDefault().getBundle(), new Path(path
), null);
404 File test
= new File(FileLocator
.toFileURL(location
).toURI());
405 fTrace
= new TmfTraceStub(test
.getPath(), 500);
406 } catch (URISyntaxException e
) {
408 } catch (IOException e
) {
415 Vector
<TmfEvent
> requestedEvents1
;
416 Vector
<TmfEvent
> requestedEvents2
;
417 Vector
<TmfEvent
> requestedEvents3
;
419 TmfEventRequest
<TmfEvent
> request1
;
420 TmfEventRequest
<TmfEvent
> request2
;
421 TmfEventRequest
<TmfEvent
> request3
;
423 ITmfDataProvider
<TmfEvent
>[] providers
;
425 private class TmfTestTriggerSignal
extends TmfSignal
{
426 public final boolean forceCancel
;
427 public TmfTestTriggerSignal(Object source
, boolean cancel
) {
429 forceCancel
= cancel
;
434 @SuppressWarnings("unchecked")
436 public void trigger(final TmfTestTriggerSignal signal
) {
438 TmfTimeRange range
= new TmfTimeRange(TmfTimestamp
.BigBang
, TmfTimestamp
.BigCrunch
);
440 requestedEvents1
= new Vector
<TmfEvent
>();
441 request1
= new TmfEventRequest
<TmfEvent
>(TmfEvent
.class, range
, NB_EVENTS
, BLOCK_SIZE
) {
444 public void handleData(TmfEvent event
) {
445 super.handleData(event
);
446 if (!isCompleted()) {
447 requestedEvents1
.add(event
);
448 if (++nbRead
>= BLOCK_SIZE
&& signal
.forceCancel
)
454 requestedEvents2
= new Vector
<TmfEvent
>();
455 request2
= new TmfEventRequest
<TmfEvent
>(TmfEvent
.class, range
, NB_EVENTS
, BLOCK_SIZE
) {
457 public void handleData(TmfEvent event
) {
458 super.handleData(event
);
459 if (!isCompleted()) {
460 requestedEvents2
.add(event
);
465 requestedEvents3
= new Vector
<TmfEvent
>();
466 request3
= new TmfEventRequest
<TmfEvent
>(TmfEvent
.class, range
, NB_EVENTS
, BLOCK_SIZE
) {
468 public void handleData(TmfEvent event
) {
469 super.handleData(event
);
470 if (!isCompleted()) {
471 requestedEvents3
.add(event
);
476 providers
= (ITmfDataProvider
<TmfEvent
>[]) TmfProviderManager
.getProviders(TmfEvent
.class, TmfTraceStub
.class);
477 providers
[0].sendRequest(request1
);
478 providers
[0].sendRequest(request2
);
479 providers
[0].sendRequest(request3
);
482 public void testCoalescedRequest() throws Exception
{
484 fTrace
= setupTrace(DIRECTORY
+ File
.separator
+ TEST_STREAM
);
486 TmfSignalManager
.register(this);
487 TmfTestTriggerSignal signal
= new TmfTestTriggerSignal(this, false);
488 TmfSignalManager
.dispatchSignal(signal
);
490 request1
.waitForCompletion();
491 request2
.waitForCompletion();
492 request3
.waitForCompletion();
494 assertEquals("Request1: nbEvents", NB_EVENTS
, requestedEvents1
.size());
495 assertTrue ("Request1: isCompleted", request1
.isCompleted());
496 assertFalse ("Request1: isCancelled", request1
.isCancelled());
498 assertEquals("Request2: nbEvents", NB_EVENTS
, requestedEvents2
.size());
499 assertTrue ("Request2: isCompleted", request2
.isCompleted());
500 assertFalse ("Request2: isCancelled", request2
.isCancelled());
502 assertEquals("Request3: nbEvents", NB_EVENTS
, requestedEvents3
.size());
503 assertTrue ("Request3: isCompleted", request3
.isCompleted());
504 assertFalse ("Request3: isCancelled", request3
.isCancelled());
506 // Ensure that we have distinct events.
507 // Don't go overboard: we are not validating the stub!
508 for (int i
= 0; i
< NB_EVENTS
; i
++) {
509 assertEquals("Distinct events", i
+1, requestedEvents1
.get(i
).getTimestamp().getValue());
510 assertEquals("Distinct events", i
+1, requestedEvents2
.get(i
).getTimestamp().getValue());
511 assertEquals("Distinct events", i
+1, requestedEvents3
.get(i
).getTimestamp().getValue());
514 TmfSignalManager
.deregister(this);
519 public void testCancelCoalescedRequest() throws Exception
{
521 fTrace
= setupTrace(DIRECTORY
+ File
.separator
+ TEST_STREAM
);
523 TmfSignalManager
.register(this);
524 TmfTestTriggerSignal signal
= new TmfTestTriggerSignal(this, true);
525 TmfSignalManager
.dispatchSignal(signal
);
527 request1
.waitForCompletion();
528 request2
.waitForCompletion();
529 request3
.waitForCompletion();
531 assertEquals("Request1: nbEvents", BLOCK_SIZE
, requestedEvents1
.size());
532 assertTrue ("Request1: isCompleted", request1
.isCompleted());
533 assertTrue ("Request1: isCancelled", request1
.isCancelled());
535 assertEquals("Request2: nbEvents", NB_EVENTS
, requestedEvents2
.size());
536 assertTrue ("Request2: isCompleted", request2
.isCompleted());
537 assertFalse ("Request2: isCancelled", request2
.isCancelled());
539 assertEquals("Request3: nbEvents", NB_EVENTS
, requestedEvents3
.size());
540 assertTrue ("Request3: isCompleted", request3
.isCompleted());
541 assertFalse ("Request3: isCancelled", request3
.isCancelled());
543 // Ensure that we have distinct events.
544 // Don't go overboard: we are not validating the stub!
545 for (int i
= 0; i
< NB_EVENTS
; i
++) {
546 assertEquals("Distinct events", i
+1, requestedEvents2
.get(i
).getTimestamp().getValue());
547 assertEquals("Distinct events", i
+1, requestedEvents3
.get(i
).getTimestamp().getValue());
550 TmfSignalManager
.deregister(this);