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
.TmfCoalescedEventRequest
;
31 import org
.eclipse
.linuxtools
.tmf
.request
.TmfDataRequest
;
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 * TODO: Implement me. Please.
45 public class TmfCoalescedEventRequestTest
extends TestCase
{
47 private static final String DIRECTORY
= "testfiles";
48 private static final String TEST_STREAM
= "A-Test-10K";
49 private static final int NB_EVENTS
= 10000;
50 private static final int BLOCK_SIZE
= 100;
52 private static TmfTraceStub fTrace
= null;
54 private TmfTraceStub
setupTrace(String path
) {
57 URL location
= FileLocator
.find(TmfCoreTestPlugin
.getDefault().getBundle(), new Path(path
), null);
58 File test
= new File(FileLocator
.toFileURL(location
).toURI());
59 fTrace
= new TmfTraceStub(test
.getPath(), 500, true);
61 } catch (URISyntaxException e
) {
63 } catch (IOException e
) {
70 // ------------------------------------------------------------------------
72 // ------------------------------------------------------------------------
74 public TmfCoalescedEventRequestTest(String name
) {
79 public void setUp() throws Exception
{
84 public void tearDown() throws Exception
{
88 // ------------------------------------------------------------------------
90 // ------------------------------------------------------------------------
92 public void testTmfCoalescedEventRequest() {
93 TmfCoalescedEventRequest
<TmfEvent
> request
= new TmfCoalescedEventRequest
<TmfEvent
>(TmfEvent
.class);
95 assertEquals("StartTime", TmfTimestamp
.BigBang
, request
.getRange().getStartTime());
96 assertEquals("EndTime", TmfTimestamp
.BigCrunch
, request
.getRange().getEndTime());
98 assertEquals("getIndex", 0, request
.getIndex());
99 assertEquals("getNbRequestedEvents", TmfDataRequest
.ALL_DATA
, request
.getNbRequested());
100 assertEquals("getBlockize", TmfDataRequest
.DEFAULT_BLOCK_SIZE
, request
.getBlockize());
103 public void testTmfCoalescedEventRequestTimeRange() {
104 TmfTimeRange range
= new TmfTimeRange(new TmfTimestamp(), TmfTimestamp
.BigCrunch
);
105 TmfCoalescedEventRequest
<TmfEvent
> request
= new TmfCoalescedEventRequest
<TmfEvent
>(TmfEvent
.class, range
);
107 assertEquals("StartTime", range
.getStartTime(), request
.getRange().getStartTime());
108 assertEquals("EndTime", range
.getEndTime(), request
.getRange().getEndTime());
110 assertEquals("getIndex", 0, request
.getIndex());
111 assertEquals("getNbRequestedEvents", TmfDataRequest
.ALL_DATA
, request
.getNbRequested());
112 assertEquals("getBlockize", TmfDataRequest
.DEFAULT_BLOCK_SIZE
, request
.getBlockize());
115 public void testTmfCoalescedEventRequestTimeRangeNbRequested() {
116 TmfTimeRange range
= new TmfTimeRange(new TmfTimestamp(), TmfTimestamp
.BigCrunch
);
117 TmfCoalescedEventRequest
<TmfEvent
> request
= new TmfCoalescedEventRequest
<TmfEvent
>(TmfEvent
.class, range
, 100);
119 assertEquals("StartTime", range
.getStartTime(), request
.getRange().getStartTime());
120 assertEquals("EndTime", range
.getEndTime(), request
.getRange().getEndTime());
122 assertEquals("getIndex", 0, request
.getIndex());
123 assertEquals("getNbRequestedEvents", 100, request
.getNbRequested());
124 assertEquals("getBlockize", TmfDataRequest
.DEFAULT_BLOCK_SIZE
, request
.getBlockize());
127 public void testTmfCoalescedEventRequestTimeRangeNbRequestedBlocksize() {
128 TmfTimeRange range
= new TmfTimeRange(new TmfTimestamp(), TmfTimestamp
.BigCrunch
);
129 TmfCoalescedEventRequest
<TmfEvent
> request
= new TmfCoalescedEventRequest
<TmfEvent
>(TmfEvent
.class, range
, 100, 200);
131 assertEquals("StartTime", range
.getStartTime(), request
.getRange().getStartTime());
132 assertEquals("EndTime", range
.getEndTime(), request
.getRange().getEndTime());
134 assertEquals("getIndex", 0, request
.getIndex());
135 assertEquals("getNbRequestedEvents", 100, request
.getNbRequested());
136 assertEquals("getBlockize", 200, request
.getBlockize());
139 // ------------------------------------------------------------------------
141 // ------------------------------------------------------------------------
143 public void testIsCompatible() {
144 TmfTimestamp startTime
= new TmfTimestamp(10);
145 TmfTimestamp endTime
= new TmfTimestamp(20);
146 TmfTimeRange range1
= new TmfTimeRange(startTime
, endTime
);
147 TmfTimeRange range2
= new TmfTimeRange(TmfTimestamp
.BigBang
, endTime
);
148 TmfTimeRange range3
= new TmfTimeRange(startTime
, TmfTimestamp
.BigCrunch
);
150 TmfCoalescedEventRequest
<TmfEvent
> coalescedRequest
= new TmfCoalescedEventRequest
<TmfEvent
>(TmfEvent
.class, range1
);
151 TmfDataRequest
<TmfEvent
> request1
= new TmfEventRequestStub
<TmfEvent
>(TmfEvent
.class, range1
);
152 TmfDataRequest
<TmfEvent
> request2
= new TmfEventRequestStub
<TmfEvent
>(TmfEvent
.class, range2
);
153 TmfDataRequest
<TmfEvent
> request3
= new TmfEventRequestStub
<TmfEvent
>(TmfEvent
.class, range3
);
155 assertTrue ("isCompatible", coalescedRequest
.isCompatible(request1
));
156 assertFalse("isCompatible", coalescedRequest
.isCompatible(request2
));
157 assertFalse("isCompatible", coalescedRequest
.isCompatible(request3
));
160 // ------------------------------------------------------------------------
162 // ------------------------------------------------------------------------
164 Vector
<TmfEvent
> requestedEvents1
;
165 Vector
<TmfEvent
> requestedEvents2
;
166 Vector
<TmfEvent
> requestedEvents3
;
168 TmfEventRequest
<TmfEvent
> request1
;
169 TmfEventRequest
<TmfEvent
> request2
;
170 TmfEventRequest
<TmfEvent
> request3
;
172 private class TmfTestTriggerSignal
extends TmfSignal
{
173 public final boolean forceCancel
;
174 public TmfTestTriggerSignal(Object source
, boolean cancel
) {
176 forceCancel
= cancel
;
180 @SuppressWarnings("unchecked")
182 public void trigger(final TmfTestTriggerSignal signal
) {
184 TmfTimeRange range
= new TmfTimeRange(TmfTimestamp
.BigBang
, TmfTimestamp
.BigCrunch
);
186 requestedEvents1
= new Vector
<TmfEvent
>();
187 request1
= new TmfEventRequest
<TmfEvent
>(TmfEvent
.class, range
, NB_EVENTS
, BLOCK_SIZE
) {
189 public void handleData() {
190 if (!isCompleted()) {
191 TmfEvent
[] events
= getData();
192 for (TmfEvent e
: events
) {
193 requestedEvents1
.add(e
);
195 if (signal
.forceCancel
)
201 requestedEvents2
= new Vector
<TmfEvent
>();
202 request2
= new TmfEventRequest
<TmfEvent
>(TmfEvent
.class, range
, NB_EVENTS
, BLOCK_SIZE
) {
204 public void handleData() {
205 if (!isCompleted()) {
206 TmfEvent
[] events
= getData();
207 for (TmfEvent e
: events
) {
208 requestedEvents2
.add(e
);
214 requestedEvents3
= new Vector
<TmfEvent
>();
215 request3
= new TmfEventRequest
<TmfEvent
>(TmfEvent
.class, range
, NB_EVENTS
, BLOCK_SIZE
) {
217 public void handleData() {
218 if (!isCompleted()) {
219 TmfEvent
[] events
= getData();
220 for (TmfEvent e
: events
) {
221 requestedEvents3
.add(e
);
227 ITmfDataProvider
<TmfEvent
>[] providers
= (ITmfDataProvider
<TmfEvent
>[]) TmfProviderManager
.getProviders(TmfEvent
.class, TmfTraceStub
.class);
228 providers
[0].sendRequest(request1
);
229 providers
[0].sendRequest(request2
);
230 providers
[0].sendRequest(request3
);
233 public void testCoalescedRequest() throws Exception
{
235 setupTrace(DIRECTORY
+ File
.separator
+ TEST_STREAM
);
237 TmfSignalManager
.register(this);
238 TmfTestTriggerSignal signal
= new TmfTestTriggerSignal(this, false);
239 TmfSignalManager
.dispatchSignal(signal
);
241 request1
.waitForCompletion();
242 request2
.waitForCompletion();
243 request3
.waitForCompletion();
245 assertEquals("Request1: nbEvents", NB_EVENTS
, requestedEvents1
.size());
246 assertTrue ("Request1: isCompleted", request1
.isCompleted());
247 assertFalse ("Request1: isCancelled", request1
.isCancelled());
249 assertEquals("Request2: nbEvents", NB_EVENTS
, requestedEvents2
.size());
250 assertTrue ("Request2: isCompleted", request2
.isCompleted());
251 assertFalse ("Request2: isCancelled", request2
.isCancelled());
253 assertEquals("Request3: nbEvents", NB_EVENTS
, requestedEvents3
.size());
254 assertTrue ("Request3: isCompleted", request3
.isCompleted());
255 assertFalse ("Request3: isCancelled", request3
.isCancelled());
257 // Ensure that we have distinct events.
258 // Don't go overboard: we are not validating the stub!
259 for (int i
= 0; i
< NB_EVENTS
; i
++) {
260 assertEquals("Distinct events", i
+1, requestedEvents1
.get(i
).getTimestamp().getValue());
261 assertEquals("Distinct events", i
+1, requestedEvents2
.get(i
).getTimestamp().getValue());
262 assertEquals("Distinct events", i
+1, requestedEvents3
.get(i
).getTimestamp().getValue());
265 TmfSignalManager
.deregister(this);
268 public void testCancelCoalescedRequest() throws Exception
{
269 setupTrace(DIRECTORY
+ File
.separator
+ TEST_STREAM
);
271 TmfSignalManager
.register(this);
272 TmfTestTriggerSignal signal
= new TmfTestTriggerSignal(this, true);
273 TmfSignalManager
.dispatchSignal(signal
);
275 request1
.waitForCompletion();
276 request2
.waitForCompletion();
277 request3
.waitForCompletion();
279 assertEquals("Request1: nbEvents", BLOCK_SIZE
, requestedEvents1
.size());
280 assertTrue ("Request1: isCompleted", request1
.isCompleted());
281 assertTrue ("Request1: isCancelled", request1
.isCancelled());
283 assertEquals("Request2: nbEvents", NB_EVENTS
, requestedEvents2
.size());
284 assertTrue ("Request2: isCompleted", request2
.isCompleted());
285 assertFalse ("Request2: isCancelled", request2
.isCancelled());
287 assertEquals("Request3: nbEvents", NB_EVENTS
, requestedEvents3
.size());
288 assertTrue ("Request3: isCompleted", request3
.isCompleted());
289 assertFalse ("Request3: isCancelled", request3
.isCancelled());
291 // Ensure that we have distinct events.
292 // Don't go overboard: we are not validating the stub!
293 for (int i
= 0; i
< NB_EVENTS
; i
++) {
294 assertEquals("Distinct events", i
+1, requestedEvents2
.get(i
).getTimestamp().getValue());
295 assertEquals("Distinct events", i
+1, requestedEvents3
.get(i
).getTimestamp().getValue());
298 TmfSignalManager
.deregister(this);