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
.trace
;
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
.event
.TmfEvent
;
26 import org
.eclipse
.linuxtools
.tmf
.event
.TmfTimeRange
;
27 import org
.eclipse
.linuxtools
.tmf
.event
.TmfTimestamp
;
28 import org
.eclipse
.linuxtools
.tmf
.experiment
.TmfExperiment
;
29 import org
.eclipse
.linuxtools
.tmf
.experiment
.TmfExperimentContext
;
30 import org
.eclipse
.linuxtools
.tmf
.request
.TmfEventRequest
;
31 import org
.eclipse
.linuxtools
.tmf
.tests
.TmfCoreTestPlugin
;
32 import org
.eclipse
.linuxtools
.tmf
.trace
.ITmfTrace
;
33 import org
.eclipse
.linuxtools
.tmf
.trace
.TmfTraceStub
;
36 * <b><u>TmfExperimentTest</u></b>
38 * TODO: Implement me. Please.
40 public class TmfExperimentTest
extends TestCase
{
42 private static final String DIRECTORY
= "testfiles";
43 private static final String TEST_STREAM
= "M-Test-10K";
44 private static final String EXPERIMENT
= "MyExperiment";
45 private static int NB_EVENTS
= 10000;
46 private static int fDefaultBlockSize
= 1000;
48 private static ITmfTrace fTrace
;
49 private static TmfExperiment
<TmfEvent
> fExperiment
;
51 private static byte SCALE
= (byte) -3;
53 // ------------------------------------------------------------------------
55 // ------------------------------------------------------------------------
57 private ITmfTrace
setupTrace(String path
) {
60 URL location
= FileLocator
.find(TmfCoreTestPlugin
.getPlugin().getBundle(), new Path(path
), null);
61 File test
= new File(FileLocator
.toFileURL(location
).toURI());
62 TmfTraceStub trace
= new TmfTraceStub(test
.getPath(), true);
64 } catch (URISyntaxException e
) {
66 } catch (IOException e
) {
73 private void setupExperiment() {
74 if (fExperiment
== null) {
75 fExperiment
= new TmfExperiment
<TmfEvent
>(TmfEvent
.class, EXPERIMENT
, new ITmfTrace
[] { fTrace
});
76 fExperiment
.indexExperiment(true);
80 public TmfExperimentTest(String name
) throws Exception
{
85 protected void setUp() throws Exception
{
87 setupTrace(DIRECTORY
+ File
.separator
+ TEST_STREAM
);
92 protected void tearDown() throws Exception
{
96 // ------------------------------------------------------------------------
98 // ------------------------------------------------------------------------
100 public void testBasicTmfExperiment() {
101 assertEquals("GetId", EXPERIMENT
, fExperiment
.getName());
102 assertEquals("GetEpoch", TmfTimestamp
.Zero
, fExperiment
.getEpoch());
103 assertEquals("GetNbEvents", NB_EVENTS
, fExperiment
.getNbEvents());
105 TmfTimeRange timeRange
= fExperiment
.getTimeRange();
106 assertEquals("getStartTime", 1, timeRange
.getStartTime().getValue());
107 assertEquals("getEndTime", NB_EVENTS
, timeRange
.getEndTime().getValue());
110 // ------------------------------------------------------------------------
112 // ------------------------------------------------------------------------
114 public void testSeekEOnCacheBoundary() throws Exception
{
115 TmfExperimentContext context
= fExperiment
.seekLocation(null);
117 context
= fExperiment
.seekEvent(new TmfTimestamp(1, SCALE
, 0));
118 TmfEvent event
= fExperiment
.getNextEvent(context
);
119 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
120 assertEquals("Event rank", 1, context
.getRank());
122 context
= fExperiment
.seekEvent(new TmfTimestamp(1001, SCALE
, 0));
123 event
= fExperiment
.getNextEvent(context
);
124 assertEquals("Event timestamp", 1001, event
.getTimestamp().getValue());
125 assertEquals("Event rank", 1001, context
.getRank());
127 context
= fExperiment
.seekEvent(new TmfTimestamp(4001, SCALE
, 0));
128 event
= fExperiment
.getNextEvent(context
);
129 assertEquals("Event timestamp", 4001, event
.getTimestamp().getValue());
130 assertEquals("Event rank", 4001, context
.getRank());
133 public void testSeekNotOnCacheBoundary() throws Exception
{
134 TmfExperimentContext context
= fExperiment
.seekLocation(null);
136 context
= fExperiment
.seekEvent(new TmfTimestamp(10, SCALE
, 0));
137 TmfEvent event
= fExperiment
.getNextEvent(context
);
138 assertEquals("Event timestamp", 10, event
.getTimestamp().getValue());
139 assertEquals("Event rank", 10, context
.getRank());
141 context
= fExperiment
.seekEvent(new TmfTimestamp(999, SCALE
, 0));
142 event
= fExperiment
.getNextEvent(context
);
143 assertEquals("Event timestamp", 999, event
.getTimestamp().getValue());
144 assertEquals("Event rank", 999, context
.getRank());
146 context
= fExperiment
.seekEvent(new TmfTimestamp(1001, SCALE
, 0));
147 event
= fExperiment
.getNextEvent(context
);
148 assertEquals("Event timestamp", 1001, event
.getTimestamp().getValue());
149 assertEquals("Event rank", 1001, context
.getRank());
151 context
= fExperiment
.seekEvent(new TmfTimestamp(4499, SCALE
, 0));
152 event
= fExperiment
.getNextEvent(context
);
153 assertEquals("Event timestamp", 4499, event
.getTimestamp().getValue());
154 assertEquals("Event rank", 4499, context
.getRank());
157 public void testSeekForEventOutOfBounds() throws Exception
{
158 TmfExperimentContext context
= fExperiment
.seekLocation(null);
160 // On lower bound, returns the first event (ts = 1)
161 context
= fExperiment
.seekEvent(new TmfTimestamp(-1, SCALE
, 0));
162 TmfEvent event
= fExperiment
.getNextEvent(context
);
163 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
165 // On higher bound, returns null (no event)
166 context
= fExperiment
.seekEvent(new TmfTimestamp(NB_EVENTS
+ 1, SCALE
, 0));
167 event
= fExperiment
.getNextEvent(context
);
168 assertEquals("Event timestamp", null, event
);
171 public void testSeekOnIndex() throws Exception
{
172 TmfExperimentContext context
= fExperiment
.seekLocation(null);
174 // On lower bound, returns the first event (ts = 1)
175 context
= fExperiment
.seekEvent(0);
176 TmfEvent event
= fExperiment
.getNextEvent(context
);
177 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
180 context
= fExperiment
.seekEvent(NB_EVENTS
- 1);
181 event
= fExperiment
.getNextEvent(context
);
182 assertEquals("Event timestamp", NB_EVENTS
, event
.getTimestamp().getValue());
185 context
= fExperiment
.seekEvent(NB_EVENTS
);
186 event
= fExperiment
.getNextEvent(context
);
187 assertEquals("Event", null, event
);
190 // ------------------------------------------------------------------------
192 // ------------------------------------------------------------------------
194 public void testProcessRequestForNbEvents() throws Exception
{
195 final int blockSize
= 100;
196 final int nbEvents
= 1000;
197 final Vector
<TmfEvent
> requestedEvents
= new Vector
<TmfEvent
>();
199 TmfTimeRange range
= new TmfTimeRange(TmfTimestamp
.BigBang
, TmfTimestamp
.BigCrunch
);
200 final TmfEventRequest
<TmfEvent
> request
= new TmfEventRequest
<TmfEvent
>(TmfEvent
.class, range
, nbEvents
, blockSize
) {
202 public void handleData() {
203 TmfEvent
[] events
= getData();
204 for (TmfEvent e
: events
) {
205 requestedEvents
.add(e
);
209 fExperiment
.sendRequest(request
);
210 request
.waitForCompletion();
212 assertEquals("nbEvents", nbEvents
, requestedEvents
.size());
213 assertTrue("isCompleted", request
.isCompleted());
214 assertFalse("isCancelled", request
.isCancelled());
216 // Ensure that we have distinct events.
217 // Don't go overboard: we are not validating the stub!
218 for (int i
= 0; i
< nbEvents
; i
++) {
219 assertEquals("Distinct events", i
+1, requestedEvents
.get(i
).getTimestamp().getValue());
223 public void testProcessRequestForNbEvents2() throws Exception
{
224 final int blockSize
= 2 * NB_EVENTS
;
225 final int nbEvents
= 1000;
226 final Vector
<TmfEvent
> requestedEvents
= new Vector
<TmfEvent
>();
228 TmfTimeRange range
= new TmfTimeRange(TmfTimestamp
.BigBang
, TmfTimestamp
.BigCrunch
);
229 final TmfEventRequest
<TmfEvent
> request
= new TmfEventRequest
<TmfEvent
>(TmfEvent
.class, range
, nbEvents
, blockSize
) {
231 public void handleData() {
232 TmfEvent
[] events
= getData();
233 for (TmfEvent e
: events
) {
234 requestedEvents
.add(e
);
238 fExperiment
.sendRequest(request
);
239 request
.waitForCompletion();
241 assertEquals("nbEvents", nbEvents
, requestedEvents
.size());
242 assertTrue("isCompleted", request
.isCompleted());
243 assertFalse("isCancelled", request
.isCancelled());
245 // Ensure that we have distinct events.
246 // Don't go overboard: we are not validating the stub!
247 for (int i
= 0; i
< nbEvents
; i
++) {
248 assertEquals("Distinct events", i
+1, requestedEvents
.get(i
).getTimestamp().getValue());
252 public void testProcessRequestForAllEvents() throws Exception
{
253 final int nbEvents
= TmfEventRequest
.ALL_DATA
;
254 final int blockSize
= 1;
255 final Vector
<TmfEvent
> requestedEvents
= new Vector
<TmfEvent
>();
256 long nbExpectedEvents
= fExperiment
.getNbEvents();
258 TmfTimeRange range
= new TmfTimeRange(TmfTimestamp
.BigBang
, TmfTimestamp
.BigCrunch
);
259 final TmfEventRequest
<TmfEvent
> request
= new TmfEventRequest
<TmfEvent
>(TmfEvent
.class, range
, nbEvents
, blockSize
) {
261 public void handleData() {
262 TmfEvent
[] events
= getData();
263 for (TmfEvent e
: events
) {
264 requestedEvents
.add(e
);
268 fExperiment
.sendRequest(request
);
269 request
.waitForCompletion();
271 assertEquals("nbEvents", nbExpectedEvents
, requestedEvents
.size());
272 assertTrue("isCompleted", request
.isCompleted());
273 assertFalse("isCancelled", request
.isCancelled());
275 // Ensure that we have distinct events.
276 // Don't go overboard: we are not validating the stub!
277 for (int i
= 0; i
< nbExpectedEvents
; i
++) {
278 assertEquals("Distinct events", i
+1, requestedEvents
.get(i
).getTimestamp().getValue());
282 // ------------------------------------------------------------------------
284 // ------------------------------------------------------------------------
286 public void testCancel() throws Exception
{
287 final int nbEvents
= NB_EVENTS
;
288 final int blockSize
= fDefaultBlockSize
;
289 final Vector
<TmfEvent
> requestedEvents
= new Vector
<TmfEvent
>();
291 TmfTimeRange range
= new TmfTimeRange(TmfTimestamp
.BigBang
, TmfTimestamp
.BigCrunch
);
292 final TmfEventRequest
<TmfEvent
> request
= new TmfEventRequest
<TmfEvent
>(TmfEvent
.class, range
, nbEvents
, blockSize
) {
294 public void handleData() {
295 TmfEvent
[] events
= getData();
296 for (TmfEvent e
: events
) {
297 requestedEvents
.add(e
);
299 // Cancel request after the first chunk is received
303 fExperiment
.sendRequest(request
);
304 request
.waitForCompletion();
306 assertEquals("nbEvents", blockSize
, requestedEvents
.size());
307 assertTrue("isCompleted", request
.isCompleted());
308 assertTrue("isCancelled", request
.isCancelled());
311 // ------------------------------------------------------------------------
313 // ------------------------------------------------------------------------
315 public void testGetRank() throws Exception
{
316 assertEquals("getRank", 0, fExperiment
.getRank(new TmfTimestamp()));
317 assertEquals("getRank", 0, fExperiment
.getRank(new TmfTimestamp( 1, (byte) -3)));
318 assertEquals("getRank", 10, fExperiment
.getRank(new TmfTimestamp( 11, (byte) -3)));
319 assertEquals("getRank", 100, fExperiment
.getRank(new TmfTimestamp( 101, (byte) -3)));
320 assertEquals("getRank", 1000, fExperiment
.getRank(new TmfTimestamp(1001, (byte) -3)));