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
.tests
.trace
;
16 import java
.util
.Vector
;
18 import org
.eclipse
.linuxtools
.tmf
.event
.TmfEvent
;
19 import org
.eclipse
.linuxtools
.tmf
.event
.TmfTimeRange
;
20 import org
.eclipse
.linuxtools
.tmf
.event
.TmfTimestamp
;
21 import org
.eclipse
.linuxtools
.tmf
.request
.TmfDataRequest
;
22 import org
.eclipse
.linuxtools
.tmf
.trace
.TmfTraceContext
;
23 import org
.eclipse
.linuxtools
.tmf
.trace
.TmfTraceStub
;
25 import junit
.framework
.TestCase
;
28 * <b><u>TmfTraceTest</u></b>
30 * TODO: Implement me. Please.
32 public class TmfTraceTest
extends TestCase
{
34 private static final String DIRECTORY
= "testfiles";
35 private static final String TEST_STREAM
= "M-Test-10K";
36 private static String testfile
;
37 private static final int NB_EVENTS
= 10000;
38 private static TmfTraceStub fTrace
;
40 private static byte SCALE
= (byte) -3;
42 // ========================================================================
44 // ========================================================================
46 public TmfTraceTest(String name
) throws Exception
{
48 String directory
= new File(".").getCanonicalPath() + File
.separator
+ DIRECTORY
;
49 testfile
= directory
+ File
.separator
+ TEST_STREAM
;
50 fTrace
= new TmfTraceStub(testfile
, 500, true);
54 protected void setUp() throws Exception
{
59 protected void tearDown() throws Exception
{
63 // ========================================================================
65 // ========================================================================
67 public void testTmfTraceDefault() throws Exception
{
68 TmfTraceStub trace
= new TmfTraceStub(testfile
, true);
70 assertEquals("getCacheSize", TmfTraceStub
.DEFAULT_CACHE_SIZE
, trace
.getCacheSize());
71 assertEquals("getTraceSize", 0, trace
.getNbEvents());
72 assertEquals("getRange-start", 0, trace
.getTimeRange().getStartTime().getValue());
73 assertEquals("getRange-end", 0, trace
.getTimeRange().getEndTime().getValue());
76 public void testTmfTrace() throws Exception
{
77 assertEquals("getCacheSize", 500, fTrace
.getCacheSize());
78 assertEquals("getTraceSize", 0, fTrace
.getNbEvents());
79 assertEquals("getRange-start", 0, fTrace
.getTimeRange().getStartTime().getValue());
80 assertEquals("getRange-end", 0, fTrace
.getTimeRange().getEndTime().getValue());
83 // ========================================================================
85 // ========================================================================
87 public void testSeekOnCacheBoundary() throws Exception
{
88 TmfTraceContext context
= fTrace
.seekLocation(null);
90 context
= fTrace
.seekEvent(new TmfTimestamp(0, SCALE
, 0));
91 TmfEvent event
= fTrace
.getNextEvent(context
);
92 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
94 context
= fTrace
.seekEvent(new TmfTimestamp(1000, SCALE
, 0));
95 event
= fTrace
.getNextEvent(context
);
96 assertEquals("Event timestamp", 1000, event
.getTimestamp().getValue());
98 context
= fTrace
.seekEvent(new TmfTimestamp(4000, SCALE
, 0));
99 event
= fTrace
.getNextEvent(context
);
100 assertEquals("Event timestamp", 4000, event
.getTimestamp().getValue());
103 public void testSeekNotOnCacheBoundary() throws Exception
{
104 TmfTraceContext context
= fTrace
.seekLocation(null);
106 context
= fTrace
.seekEvent(new TmfTimestamp(1, SCALE
, 0));
107 TmfEvent event
= fTrace
.getNextEvent(context
);
108 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
110 context
= fTrace
.seekEvent(new TmfTimestamp(999, SCALE
, 0));
111 event
= fTrace
.getNextEvent(context
);
112 assertEquals("Event timestamp", 999, event
.getTimestamp().getValue());
114 context
= fTrace
.seekEvent(new TmfTimestamp(4499, SCALE
, 0));
115 event
= fTrace
.getNextEvent(context
);
116 assertEquals("Event timestamp", 4499, event
.getTimestamp().getValue());
119 public void testSeekForEventOutOfBounds() throws Exception
{
120 TmfTraceContext context
= fTrace
.seekLocation(null);
122 // On lower bound, returns the first event (ts = 1)
123 context
= fTrace
.seekEvent(new TmfTimestamp(-1, SCALE
, 0));
124 TmfEvent event
= fTrace
.getNextEvent(context
);
125 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
127 // On higher bound, returns null (no event)
128 context
= fTrace
.seekEvent(new TmfTimestamp(NB_EVENTS
+ 1, SCALE
, 0));
129 event
= fTrace
.getNextEvent(context
);
130 assertEquals("Event timestamp", null, event
);
133 // ========================================================================
135 // ========================================================================
137 public void testGetNextEvent() throws Exception
{
138 TmfTraceContext context
= fTrace
.seekLocation(null);
140 // On lower bound, returns the first event (ts = 0)
141 context
= fTrace
.seekEvent(new TmfTimestamp(0, SCALE
, 0));
142 TmfEvent event
= fTrace
.getNextEvent(context
);
143 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
145 for (int i
= 2; i
< 20; i
++) {
146 event
= fTrace
.getNextEvent(context
);
147 assertEquals("Event timestamp", i
, event
.getTimestamp().getValue());
151 // ========================================================================
153 // ========================================================================
155 public void testProcessRequestForNbEvents() throws Exception
{
156 final int BLOCK_SIZE
= 100;
157 final int NB_EVENTS
= 1000;
158 final Vector
<TmfEvent
> requestedEvents
= new Vector
<TmfEvent
>();
160 TmfTimeRange range
= new TmfTimeRange(TmfTimestamp
.BigBang
, TmfTimestamp
.BigCrunch
);
161 final TmfDataRequest
<TmfEvent
> request
= new TmfDataRequest
<TmfEvent
>(range
, NB_EVENTS
, BLOCK_SIZE
) {
163 public void handleData() {
164 TmfEvent
[] events
= getData();
165 for (TmfEvent e
: events
) {
166 requestedEvents
.add(e
);
170 fTrace
.processRequest(request
, true);
172 assertEquals("nbEvents", NB_EVENTS
, requestedEvents
.size());
173 assertTrue("isCompleted", request
.isCompleted());
174 assertFalse("isCancelled", request
.isCancelled());
176 // Ensure that we have distinct events.
177 // Don't go overboard: we are not validating the stub!
178 for (int i
= 0; i
< NB_EVENTS
; i
++) {
179 assertEquals("Distinct events", i
+1, requestedEvents
.get(i
).getTimestamp().getValue());
183 public void testProcessRequestForAllEvents() throws Exception
{
184 final int BLOCK_SIZE
= 1;
185 final Vector
<TmfEvent
> requestedEvents
= new Vector
<TmfEvent
>();
186 // long nbExpectedEvents = NB_EVENTS;
188 TmfTimeRange range
= new TmfTimeRange(TmfTimestamp
.BigBang
, TmfTimestamp
.BigCrunch
);
189 final TmfDataRequest
<TmfEvent
> request
= new TmfDataRequest
<TmfEvent
>(range
, 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", NB_EVENTS
, 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
< NB_EVENTS
; i
++) {
207 assertEquals("Distinct events", i
+1, requestedEvents
.get(i
).getTimestamp().getValue());
211 // ========================================================================
213 // ========================================================================
215 public void testCancel() throws Exception
{
216 final Vector
<TmfEvent
> requestedEvents
= new Vector
<TmfEvent
>();
218 TmfTimeRange range
= new TmfTimeRange(TmfTimestamp
.BigBang
, TmfTimestamp
.BigCrunch
);
219 final TmfDataRequest
<TmfEvent
> request
= new TmfDataRequest
<TmfEvent
>(range
, NB_EVENTS
, NB_EVENTS
) {
221 public void handleData() {
222 TmfEvent
[] events
= getData();
223 for (TmfEvent e
: events
) {
224 requestedEvents
.add(e
);
226 // Cancel request after the first chunk is received
230 fTrace
.processRequest(request
, true);
232 assertEquals("nbEvents", NB_EVENTS
, requestedEvents
.size());
233 assertTrue("isCompleted", request
.isCompleted());
234 assertTrue("isCancelled", request
.isCancelled());
This page took 0.038339 seconds and 6 git commands to generate.