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
;
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
;
23 import junit
.framework
.TestCase
;
26 * <b><u>TmfTraceTest</u></b>
28 * TODO: Implement me. Please.
30 public class TmfTraceTest
extends TestCase
{
32 private static final String DIRECTORY
= "testfiles";
33 private static final String TEST_STREAM
= "M-Test-10K";
34 private static String testfile
;
35 private static final int NB_EVENTS
= 10000;
36 private static TmfTraceStub fTrace
;
38 private static byte SCALE
= (byte) -3;
40 // ========================================================================
42 // ========================================================================
44 public TmfTraceTest(String name
) throws Exception
{
46 String directory
= new File(".").getCanonicalPath() + File
.separator
+ DIRECTORY
;
47 testfile
= directory
+ File
.separator
+ TEST_STREAM
;
48 fTrace
= new TmfTraceStub(testfile
, 500, true);
52 protected void setUp() throws Exception
{
57 protected void tearDown() throws Exception
{
61 // ========================================================================
63 // ========================================================================
65 public void testTmfTraceDefault() throws Exception
{
66 TmfTraceStub trace
= new TmfTraceStub(testfile
, true);
68 assertEquals("getCacheSize", TmfTraceStub
.DEFAULT_CACHE_SIZE
, trace
.getCacheSize());
69 assertEquals("getTraceSize", 0, trace
.getNbEvents());
70 assertEquals("getRange-start", 0, trace
.getTimeRange().getStartTime().getValue());
71 assertEquals("getRange-end", 0, trace
.getTimeRange().getEndTime().getValue());
74 public void testTmfTrace() throws Exception
{
75 assertEquals("getCacheSize", 500, fTrace
.getCacheSize());
76 assertEquals("getTraceSize", 0, fTrace
.getNbEvents());
77 assertEquals("getRange-start", 0, fTrace
.getTimeRange().getStartTime().getValue());
78 assertEquals("getRange-end", 0, fTrace
.getTimeRange().getEndTime().getValue());
81 // ========================================================================
83 // ========================================================================
85 public void testSeekOnCacheBoundary() throws Exception
{
86 TmfTraceContext context
= fTrace
.seekLocation(null);
88 context
= fTrace
.seekEvent(new TmfTimestamp(0, SCALE
, 0));
89 TmfEvent event
= fTrace
.getNextEvent(context
);
90 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
92 context
= fTrace
.seekEvent(new TmfTimestamp(1000, SCALE
, 0));
93 event
= fTrace
.getNextEvent(context
);
94 assertEquals("Event timestamp", 1000, event
.getTimestamp().getValue());
96 context
= fTrace
.seekEvent(new TmfTimestamp(4000, SCALE
, 0));
97 event
= fTrace
.getNextEvent(context
);
98 assertEquals("Event timestamp", 4000, event
.getTimestamp().getValue());
101 public void testSeekNotOnCacheBoundary() throws Exception
{
102 TmfTraceContext context
= fTrace
.seekLocation(null);
104 context
= fTrace
.seekEvent(new TmfTimestamp(1, SCALE
, 0));
105 TmfEvent event
= fTrace
.getNextEvent(context
);
106 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
108 context
= fTrace
.seekEvent(new TmfTimestamp(999, SCALE
, 0));
109 event
= fTrace
.getNextEvent(context
);
110 assertEquals("Event timestamp", 999, event
.getTimestamp().getValue());
112 context
= fTrace
.seekEvent(new TmfTimestamp(4499, SCALE
, 0));
113 event
= fTrace
.getNextEvent(context
);
114 assertEquals("Event timestamp", 4499, event
.getTimestamp().getValue());
117 public void testSeekForEventOutOfBounds() throws Exception
{
118 TmfTraceContext context
= fTrace
.seekLocation(null);
120 // On lower bound, returns the first event (ts = 1)
121 context
= fTrace
.seekEvent(new TmfTimestamp(-1, SCALE
, 0));
122 TmfEvent event
= fTrace
.getNextEvent(context
);
123 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
125 // On higher bound, returns null (no event)
126 context
= fTrace
.seekEvent(new TmfTimestamp(NB_EVENTS
+ 1, SCALE
, 0));
127 event
= fTrace
.getNextEvent(context
);
128 assertEquals("Event timestamp", null, event
);
131 // ========================================================================
133 // ========================================================================
135 public void testGetNextEvent() throws Exception
{
136 TmfTraceContext context
= fTrace
.seekLocation(null);
138 // On lower bound, returns the first event (ts = 0)
139 context
= fTrace
.seekEvent(new TmfTimestamp(0, SCALE
, 0));
140 TmfEvent event
= fTrace
.getNextEvent(context
);
141 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
143 for (int i
= 2; i
< 20; i
++) {
144 event
= fTrace
.getNextEvent(context
);
145 assertEquals("Event timestamp", i
, event
.getTimestamp().getValue());
149 // ========================================================================
151 // ========================================================================
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
, 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());
181 public void testProcessRequestForAllEvents() throws Exception
{
182 final int BLOCK_SIZE
= 1;
183 final Vector
<TmfEvent
> requestedEvents
= new Vector
<TmfEvent
>();
184 long nbExpectedEvents
= NB_EVENTS
;
186 TmfTimeRange range
= new TmfTimeRange(TmfTimestamp
.BigBang
, TmfTimestamp
.BigCrunch
);
187 final TmfDataRequest
<TmfEvent
> request
= new TmfDataRequest
<TmfEvent
>(range
, NB_EVENTS
, BLOCK_SIZE
) {
189 public void handleData() {
190 TmfEvent
[] events
= getData();
191 for (TmfEvent e
: events
) {
192 requestedEvents
.add(e
);
196 fTrace
.processRequest(request
, true);
198 assertEquals("nbEvents", NB_EVENTS
, requestedEvents
.size());
199 assertTrue("isCompleted", request
.isCompleted());
200 assertFalse("isCancelled", request
.isCancelled());
202 // Ensure that we have distinct events.
203 // Don't go overboard: we are not validating the stub!
204 for (int i
= 0; i
< NB_EVENTS
; i
++) {
205 assertEquals("Distinct events", i
+1, requestedEvents
.get(i
).getTimestamp().getValue());
209 // ========================================================================
211 // ========================================================================
213 public void testCancel() throws Exception
{
214 final Vector
<TmfEvent
> requestedEvents
= new Vector
<TmfEvent
>();
216 TmfTimeRange range
= new TmfTimeRange(TmfTimestamp
.BigBang
, TmfTimestamp
.BigCrunch
);
217 final TmfDataRequest
<TmfEvent
> request
= new TmfDataRequest
<TmfEvent
>(range
, NB_EVENTS
, NB_EVENTS
) {
219 public void handleData() {
220 TmfEvent
[] events
= getData();
221 for (TmfEvent e
: events
) {
222 requestedEvents
.add(e
);
224 // Cancel request after the first chunk is received
228 fTrace
.processRequest(request
, true);
230 assertEquals("nbEvents", NB_EVENTS
, requestedEvents
.size());
231 assertTrue("isCompleted", request
.isCompleted());
232 assertTrue("isCancelled", request
.isCancelled());
This page took 0.036844 seconds and 5 git commands to generate.