(no commit message)
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.tests / src / org / eclipse / linuxtools / tmf / tests / trace / TmfTraceTest.java
1 /*******************************************************************************
2 * Copyright (c) 2009 Ericsson
3 *
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
8 *
9 * Contributors:
10 * Francois Chouinard - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.tmf.tests.trace;
14
15 import java.io.File;
16 import java.util.Vector;
17
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;
24
25 import junit.framework.TestCase;
26
27 /**
28 * <b><u>TmfTraceTest</u></b>
29 * <p>
30 * TODO: Implement me. Please.
31 */
32 public class TmfTraceTest extends TestCase {
33
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;
39
40 private static byte SCALE = (byte) -3;
41
42 // ========================================================================
43 // Housekeeping
44 // ========================================================================
45
46 public TmfTraceTest(String name) throws Exception {
47 super(name);
48 String directory = new File(".").getCanonicalPath() + File.separator + DIRECTORY;
49 testfile = directory + File.separator + TEST_STREAM;
50 fTrace = new TmfTraceStub(testfile, 500, true);
51 }
52
53 @Override
54 protected void setUp() throws Exception {
55 super.setUp();
56 }
57
58 @Override
59 protected void tearDown() throws Exception {
60 super.tearDown();
61 }
62
63 // ========================================================================
64 // Constructors
65 // ========================================================================
66
67 public void testTmfTraceDefault() throws Exception {
68 TmfTraceStub trace = new TmfTraceStub(testfile, true);
69
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());
74 }
75
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());
81 }
82
83 // ========================================================================
84 // seek
85 // ========================================================================
86
87 public void testSeekOnCacheBoundary() throws Exception {
88 TmfTraceContext context = fTrace.seekLocation(null);
89
90 context = fTrace.seekEvent(new TmfTimestamp(0, SCALE, 0));
91 TmfEvent event = fTrace.getNextEvent(context);
92 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
93
94 context = fTrace.seekEvent(new TmfTimestamp(1000, SCALE, 0));
95 event = fTrace.getNextEvent(context);
96 assertEquals("Event timestamp", 1000, event.getTimestamp().getValue());
97
98 context = fTrace.seekEvent(new TmfTimestamp(4000, SCALE, 0));
99 event = fTrace.getNextEvent(context);
100 assertEquals("Event timestamp", 4000, event.getTimestamp().getValue());
101 }
102
103 public void testSeekNotOnCacheBoundary() throws Exception {
104 TmfTraceContext context = fTrace.seekLocation(null);
105
106 context = fTrace.seekEvent(new TmfTimestamp(1, SCALE, 0));
107 TmfEvent event = fTrace.getNextEvent(context);
108 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
109
110 context = fTrace.seekEvent(new TmfTimestamp(999, SCALE, 0));
111 event = fTrace.getNextEvent(context);
112 assertEquals("Event timestamp", 999, event.getTimestamp().getValue());
113
114 context = fTrace.seekEvent(new TmfTimestamp(4499, SCALE, 0));
115 event = fTrace.getNextEvent(context);
116 assertEquals("Event timestamp", 4499, event.getTimestamp().getValue());
117 }
118
119 public void testSeekForEventOutOfBounds() throws Exception {
120 TmfTraceContext context = fTrace.seekLocation(null);
121
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());
126
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);
131 }
132
133 // ========================================================================
134 // getNextEvent
135 // ========================================================================
136
137 public void testGetNextEvent() throws Exception {
138 TmfTraceContext context = fTrace.seekLocation(null);
139
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());
144
145 for (int i = 2; i < 20; i++) {
146 event = fTrace.getNextEvent(context);
147 assertEquals("Event timestamp", i, event.getTimestamp().getValue());
148 }
149 }
150
151 // ========================================================================
152 // processRequest
153 // ========================================================================
154
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>();
159
160 TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BigBang, TmfTimestamp.BigCrunch);
161 final TmfDataRequest<TmfEvent> request = new TmfDataRequest<TmfEvent>(range, NB_EVENTS, BLOCK_SIZE) {
162 @Override
163 public void handleData() {
164 TmfEvent[] events = getData();
165 for (TmfEvent e : events) {
166 requestedEvents.add(e);
167 }
168 }
169 };
170 fTrace.processRequest(request, true);
171
172 assertEquals("nbEvents", NB_EVENTS, requestedEvents.size());
173 assertTrue("isCompleted", request.isCompleted());
174 assertFalse("isCancelled", request.isCancelled());
175
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());
180 }
181 }
182
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;
187
188 TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BigBang, TmfTimestamp.BigCrunch);
189 final TmfDataRequest<TmfEvent> request = new TmfDataRequest<TmfEvent>(range, NB_EVENTS, BLOCK_SIZE) {
190 @Override
191 public void handleData() {
192 TmfEvent[] events = getData();
193 for (TmfEvent e : events) {
194 requestedEvents.add(e);
195 }
196 }
197 };
198 fTrace.processRequest(request, true);
199
200 assertEquals("nbEvents", NB_EVENTS, requestedEvents.size());
201 assertTrue("isCompleted", request.isCompleted());
202 assertFalse("isCancelled", request.isCancelled());
203
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());
208 }
209 }
210
211 // ========================================================================
212 // cancel
213 // ========================================================================
214
215 public void testCancel() throws Exception {
216 final Vector<TmfEvent> requestedEvents = new Vector<TmfEvent>();
217
218 TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BigBang, TmfTimestamp.BigCrunch);
219 final TmfDataRequest<TmfEvent> request = new TmfDataRequest<TmfEvent>(range, NB_EVENTS, NB_EVENTS) {
220 @Override
221 public void handleData() {
222 TmfEvent[] events = getData();
223 for (TmfEvent e : events) {
224 requestedEvents.add(e);
225 }
226 // Cancel request after the first chunk is received
227 cancel();
228 }
229 };
230 fTrace.processRequest(request, true);
231
232 assertEquals("nbEvents", NB_EVENTS, requestedEvents.size());
233 assertTrue("isCompleted", request.isCompleted());
234 assertTrue("isCancelled", request.isCancelled());
235 }
236 }
This page took 0.038339 seconds and 6 git commands to generate.