1 /*******************************************************************************
2 * Copyright (c) 2009 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
.trace
;
15 import static org
.junit
.Assert
.assertEquals
;
16 import static org
.junit
.Assert
.assertFalse
;
17 import static org
.junit
.Assert
.assertTrue
;
20 import java
.util
.Vector
;
22 import org
.eclipse
.linuxtools
.tmf
.event
.TmfEvent
;
23 import org
.eclipse
.linuxtools
.tmf
.event
.TmfTimeRange
;
24 import org
.eclipse
.linuxtools
.tmf
.event
.TmfTimestamp
;
25 import org
.eclipse
.linuxtools
.tmf
.request
.TmfDataRequest
;
26 import org
.junit
.BeforeClass
;
27 import org
.junit
.Test
;
30 * <b><u>TmfEventStreamTest</u></b>
32 * TODO: Implement me. Please.
34 public class TmfTraceTest
{
36 private static final String DIRECTORY
= "testfiles";
37 private static final String TEST_STREAM
= "M-Test-10K";
38 private static String testfile
;
39 private static final int NB_EVENTS
= 10000;
40 private static TmfTraceStub fTrace
;
42 private static byte SCALE
= (byte) -3;
45 * @throws java.lang.Exception
48 public static void setUpBeforeClass() throws Exception
{
49 String directory
= new File(".").getCanonicalPath() + File
.separator
+ DIRECTORY
;
50 testfile
= directory
+ File
.separator
+ TEST_STREAM
;
51 fTrace
= new TmfTraceStub(testfile
, 500, true);
54 // ========================================================================
56 // ========================================================================
59 public void testDefaultConstructor() throws Exception
{
60 TmfTraceStub trace
= new TmfTraceStub(testfile
, true);
62 assertEquals("getCacheSize", TmfTraceStub
.DEFAULT_CACHE_SIZE
, trace
.getCacheSize());
63 assertEquals("getTraceSize", NB_EVENTS
, trace
.getNbEvents());
64 assertEquals("getRange-start", 1, trace
.getTimeRange().getStartTime().getValue());
65 assertEquals("getRange-end", NB_EVENTS
, trace
.getTimeRange().getEndTime().getValue());
69 public void testNormalConstructor() throws Exception
{
70 assertEquals("getCacheSize", 500, fTrace
.getCacheSize());
71 assertEquals("getTraceSize", NB_EVENTS
, fTrace
.getNbEvents());
72 assertEquals("getRange-start", 1, fTrace
.getTimeRange().getStartTime().getValue());
73 assertEquals("getRange-end", NB_EVENTS
, fTrace
.getTimeRange().getEndTime().getValue());
76 // ========================================================================
78 // ========================================================================
81 public void testSeekOnCacheBoundary() throws Exception
{
82 TmfTraceContext context
= fTrace
.seekLocation(null);
84 context
= fTrace
.seekEvent(new TmfTimestamp(0, SCALE
, 0));
85 TmfEvent event
= fTrace
.getNextEvent(context
);
86 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
88 context
= fTrace
.seekEvent(new TmfTimestamp(1000, SCALE
, 0));
89 event
= fTrace
.getNextEvent(context
);
90 assertEquals("Event timestamp", 1000, event
.getTimestamp().getValue());
92 context
= fTrace
.seekEvent(new TmfTimestamp(4000, SCALE
, 0));
93 event
= fTrace
.getNextEvent(context
);
94 assertEquals("Event timestamp", 4000, event
.getTimestamp().getValue());
98 public void testSeekNotOnCacheBoundary() throws Exception
{
99 TmfTraceContext context
= fTrace
.seekLocation(null);
101 context
= fTrace
.seekEvent(new TmfTimestamp(1, SCALE
, 0));
102 TmfEvent event
= fTrace
.getNextEvent(context
);
103 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
105 context
= fTrace
.seekEvent(new TmfTimestamp(999, SCALE
, 0));
106 event
= fTrace
.getNextEvent(context
);
107 assertEquals("Event timestamp", 999, event
.getTimestamp().getValue());
109 context
= fTrace
.seekEvent(new TmfTimestamp(4499, SCALE
, 0));
110 event
= fTrace
.getNextEvent(context
);
111 assertEquals("Event timestamp", 4499, event
.getTimestamp().getValue());
115 public void testSeekForEventOutOfBounds() throws Exception
{
116 TmfTraceContext context
= fTrace
.seekLocation(null);
118 // On lower bound, returns the first event (ts = 1)
119 context
= fTrace
.seekEvent(new TmfTimestamp(-1, SCALE
, 0));
120 TmfEvent event
= fTrace
.getNextEvent(context
);
121 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
123 // On higher bound, returns null (no event)
124 context
= fTrace
.seekEvent(new TmfTimestamp(NB_EVENTS
+ 1, SCALE
, 0));
125 event
= fTrace
.getNextEvent(context
);
126 assertEquals("Event timestamp", null, event
);
129 // ========================================================================
131 // ========================================================================
134 public void testGetNextEvent() throws Exception
{
135 TmfTraceContext context
= fTrace
.seekLocation(null);
137 // On lower bound, returns the first event (ts = 0)
138 context
= fTrace
.seekEvent(new TmfTimestamp(0, SCALE
, 0));
139 TmfEvent event
= fTrace
.getNextEvent(context
);
140 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
142 for (int i
= 2; i
< 20; i
++) {
143 event
= fTrace
.getNextEvent(context
);
144 assertEquals("Event timestamp", i
, event
.getTimestamp().getValue());
148 // ========================================================================
150 // ========================================================================
153 public void testProcessRequestForNbEvents() throws Exception
{
154 final int BLOCK_SIZE
= 100;
155 final int NB_EVENTS
= 1000;
156 final Vector
<TmfEvent
> requestedEvents
= new Vector
<TmfEvent
>();
158 TmfTimeRange range
= new TmfTimeRange(TmfTimestamp
.BigBang
, TmfTimestamp
.BigCrunch
);
159 final TmfDataRequest
<TmfEvent
> request
= new TmfDataRequest
<TmfEvent
>(range
, 0, NB_EVENTS
, BLOCK_SIZE
) {
161 public void handleData() {
162 TmfEvent
[] events
= getData();
163 for (TmfEvent e
: events
) {
164 requestedEvents
.add(e
);
168 fTrace
.processRequest(request
, true);
170 assertEquals("nbEvents", NB_EVENTS
, requestedEvents
.size());
171 assertTrue("isCompleted", request
.isCompleted());
172 assertFalse("isCancelled", request
.isCancelled());
174 // Ensure that we have distinct events.
175 // Don't go overboard: we are not validating the stub!
176 for (int i
= 0; i
< NB_EVENTS
; i
++) {
177 assertEquals("Distinct events", i
+1, requestedEvents
.get(i
).getTimestamp().getValue());
182 public void testProcessRequestForAllEvents() throws Exception
{
183 final int NB_EVENTS
= -1;
184 final int BLOCK_SIZE
= 1;
185 final Vector
<TmfEvent
> requestedEvents
= new Vector
<TmfEvent
>();
186 long nbExpectedEvents
= fTrace
.getNbEvents();
188 TmfTimeRange range
= new TmfTimeRange(TmfTimestamp
.BigBang
, TmfTimestamp
.BigCrunch
);
189 final TmfDataRequest
<TmfEvent
> request
= new TmfDataRequest
<TmfEvent
>(range
, 0, NB_EVENTS
, BLOCK_SIZE
) {
191 public void handleData() {
192 TmfEvent
[] events
= getData();
193 for (TmfEvent e
: events
) {
194 requestedEvents
.add(e
);
198 fTrace
.processRequest(request
, true);
200 assertEquals("nbEvents", nbExpectedEvents
, requestedEvents
.size());
201 assertTrue("isCompleted", request
.isCompleted());
202 assertFalse("isCancelled", request
.isCancelled());
204 // Ensure that we have distinct events.
205 // Don't go overboard: we are not validating the stub!
206 for (int i
= 0; i
< nbExpectedEvents
; i
++) {
207 assertEquals("Distinct events", i
+1, requestedEvents
.get(i
).getTimestamp().getValue());
211 // ========================================================================
213 // ========================================================================
216 public void testCancel() throws Exception
{
217 final Vector
<TmfEvent
> requestedEvents
= new Vector
<TmfEvent
>();
219 TmfTimeRange range
= new TmfTimeRange(TmfTimestamp
.BigBang
, TmfTimestamp
.BigCrunch
);
220 final TmfDataRequest
<TmfEvent
> request
= new TmfDataRequest
<TmfEvent
>(range
, 0, NB_EVENTS
, NB_EVENTS
) {
222 public void handleData() {
223 TmfEvent
[] events
= getData();
224 for (TmfEvent e
: events
) {
225 requestedEvents
.add(e
);
227 // Cancel request after the first chunk is received
231 fTrace
.processRequest(request
, true);
233 assertEquals("nbEvents", NB_EVENTS
, requestedEvents
.size());
234 assertTrue("isCompleted", request
.isCompleted());
235 assertTrue("isCancelled", request
.isCancelled());
This page took 0.036517 seconds and 5 git commands to generate.