[Bug 303523] LTTng/TMF udpates:
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.tests / src / org / eclipse / linuxtools / tmf / tests / trace / TmfTraceTest.java
1 /*******************************************************************************
2 * Copyright (c) 2009, 2010 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.io.IOException;
17 import java.net.URISyntaxException;
18 import java.net.URL;
19 import java.util.Vector;
20
21 import junit.framework.TestCase;
22
23 import org.eclipse.core.runtime.FileLocator;
24 import org.eclipse.core.runtime.Path;
25 import org.eclipse.linuxtools.tmf.component.TmfProvider;
26 import org.eclipse.linuxtools.tmf.component.TmfProviderManager;
27 import org.eclipse.linuxtools.tmf.event.TmfEvent;
28 import org.eclipse.linuxtools.tmf.event.TmfTimeRange;
29 import org.eclipse.linuxtools.tmf.event.TmfTimestamp;
30 import org.eclipse.linuxtools.tmf.request.TmfEventRequest;
31 import org.eclipse.linuxtools.tmf.tests.TmfCoreTestPlugin;
32 import org.eclipse.linuxtools.tmf.trace.TmfTraceContext;
33 import org.eclipse.linuxtools.tmf.trace.TmfTraceStub;
34
35 /**
36 * <b><u>TmfTraceTest</u></b>
37 * <p>
38 * TODO: Implement me. Please.
39 */
40 public class TmfTraceTest extends TestCase {
41
42 private static final String DIRECTORY = "testfiles";
43 private static final String TEST_STREAM = "M-Test-10K";
44 private static final int NB_EVENTS = 10000;
45 private static TmfTraceStub fTrace = null;
46
47 private static byte SCALE = (byte) -3;
48
49 // ------------------------------------------------------------------------
50 // Housekeeping
51 // ------------------------------------------------------------------------
52
53 private TmfTraceStub setupTrace(String path) {
54 if (fTrace == null) {
55 try {
56 URL location = FileLocator.find(TmfCoreTestPlugin.getPlugin().getBundle(), new Path(path), null);
57 File test = new File(FileLocator.toFileURL(location).toURI());
58 TmfTraceStub trace = new TmfTraceStub(test.getPath(), 500, true);
59 fTrace = trace;
60 } catch (URISyntaxException e) {
61 e.printStackTrace();
62 } catch (IOException e) {
63 e.printStackTrace();
64 }
65 }
66 return fTrace;
67 }
68
69 public TmfTraceTest(String name) throws Exception {
70 super(name);
71 }
72
73 @Override
74 protected void setUp() throws Exception {
75 super.setUp();
76 setupTrace(DIRECTORY + File.separator + TEST_STREAM);
77 }
78
79 @Override
80 protected void tearDown() throws Exception {
81 super.tearDown();
82 }
83
84 // ------------------------------------------------------------------------
85 // Constructors
86 // ------------------------------------------------------------------------
87
88 public void testTmfTraceDefault() throws Exception {
89 TmfTraceStub trace = null;
90 try {
91 URL location = FileLocator.find(TmfCoreTestPlugin.getPlugin().getBundle(), new Path(DIRECTORY + File.separator + TEST_STREAM), null);
92 File test = new File(FileLocator.toFileURL(location).toURI());
93 trace = new TmfTraceStub(test.getPath(), true);
94 } catch (URISyntaxException e) {
95 e.printStackTrace();
96 } catch (IOException e) {
97 e.printStackTrace();
98 }
99 assertTrue("Opened trace", trace != null);
100 assertEquals("getCacheSize", TmfTraceStub.DEFAULT_CACHE_SIZE, trace.getCacheSize());
101 assertEquals("getTraceSize", NB_EVENTS, trace.getNbEvents());
102 }
103
104 public void testTmfTrace() throws Exception {
105 assertEquals("getCacheSize", 500, fTrace.getCacheSize());
106 assertEquals("getTraceSize", NB_EVENTS, fTrace.getNbEvents());
107 assertEquals("getRange-start", 1, fTrace.getTimeRange().getStartTime().getValue());
108 assertEquals("getRange-end", NB_EVENTS, fTrace.getTimeRange().getEndTime().getValue());
109 }
110
111 // ------------------------------------------------------------------------
112 // seek
113 // ------------------------------------------------------------------------
114
115 public void testSeekOnCacheBoundary() throws Exception {
116 TmfTraceContext context = fTrace.seekLocation(null);
117
118 context = fTrace.seekEvent(new TmfTimestamp(1, SCALE, 0));
119 TmfEvent event = fTrace.getNextEvent(context);
120 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
121 assertEquals("Event rank", 1, context.getRank());
122
123 context = fTrace.seekEvent(new TmfTimestamp(1001, SCALE, 0));
124 event = fTrace.getNextEvent(context);
125 assertEquals("Event timestamp", 1001, event.getTimestamp().getValue());
126 assertEquals("Event rank", 1001, context.getRank());
127
128 context = fTrace.seekEvent(new TmfTimestamp(4001, SCALE, 0));
129 event = fTrace.getNextEvent(context);
130 assertEquals("Event timestamp", 4001, event.getTimestamp().getValue());
131 assertEquals("Event rank", 4001, context.getRank());
132 }
133
134 public void testSeekNotOnCacheBoundary() throws Exception {
135 TmfTraceContext context = fTrace.seekLocation(null);
136
137 context = fTrace.seekEvent(new TmfTimestamp(10, SCALE, 0));
138 TmfEvent event = fTrace.getNextEvent(context);
139 assertEquals("Event timestamp", 10, event.getTimestamp().getValue());
140 assertEquals("Event rank", 10, context.getRank());
141
142 context = fTrace.seekEvent(new TmfTimestamp(999, SCALE, 0));
143 event = fTrace.getNextEvent(context);
144 assertEquals("Event timestamp", 999, event.getTimestamp().getValue());
145 assertEquals("Event rank", 999, context.getRank());
146
147 context = fTrace.seekEvent(new TmfTimestamp(1001, SCALE, 0));
148 event = fTrace.getNextEvent(context);
149 assertEquals("Event timestamp", 1001, event.getTimestamp().getValue());
150 assertEquals("Event rank", 1001, context.getRank());
151
152 context = fTrace.seekEvent(new TmfTimestamp(4499, SCALE, 0));
153 event = fTrace.getNextEvent(context);
154 assertEquals("Event timestamp", 4499, event.getTimestamp().getValue());
155 assertEquals("Event rank", 4499, context.getRank());
156 }
157
158 public void testSeekForEventOutOfBounds() throws Exception {
159 TmfTraceContext context = fTrace.seekLocation(null);
160
161 // On lower bound, returns the first event (ts = 1)
162 context = fTrace.seekEvent(new TmfTimestamp(-1, SCALE, 0));
163 TmfEvent event = fTrace.getNextEvent(context);
164 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
165
166 // On higher bound, returns null (no event)
167 context = fTrace.seekEvent(new TmfTimestamp(NB_EVENTS + 1, SCALE, 0));
168 event = fTrace.getNextEvent(context);
169 assertEquals("Event timestamp", null, event);
170 }
171
172 public void testSeekOnIndex() throws Exception {
173 TmfTraceContext context = fTrace.seekLocation(null);
174
175 // On lower bound, returns the first event (ts = 1)
176 context = fTrace.seekEvent(0);
177 TmfEvent event = fTrace.getNextEvent(context);
178 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
179
180 // On higher bound
181 context = fTrace.seekEvent(NB_EVENTS - 1);
182 event = fTrace.getNextEvent(context);
183 assertEquals("Event timestamp", NB_EVENTS, event.getTimestamp().getValue());
184
185 // Above high bound
186 context = fTrace.seekEvent(NB_EVENTS);
187 event = fTrace.getNextEvent(context);
188 assertEquals("Event", null, event);
189 }
190
191 // ------------------------------------------------------------------------
192 // getNextEvent
193 // ------------------------------------------------------------------------
194
195 public void testGetNextEvent() throws Exception {
196 TmfTraceContext context = fTrace.seekLocation(null);
197
198 // On lower bound, returns the first event (ts = 0)
199 context = fTrace.seekEvent(new TmfTimestamp(0, SCALE, 0));
200 TmfEvent event = fTrace.getNextEvent(context);
201 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
202
203 for (int i = 2; i < 20; i++) {
204 event = fTrace.getNextEvent(context);
205 assertEquals("Event timestamp", i, event.getTimestamp().getValue());
206 }
207 }
208
209 // ------------------------------------------------------------------------
210 // processRequest
211 // ------------------------------------------------------------------------
212
213 @SuppressWarnings("unchecked")
214 public void testProcessRequestForNbEvents() throws Exception {
215 final int BLOCK_SIZE = 100;
216 final int NB_EVENTS = 1000;
217 final Vector<TmfEvent> requestedEvents = new Vector<TmfEvent>();
218
219 TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BigBang, TmfTimestamp.BigCrunch);
220 final TmfEventRequest<TmfEvent> request = new TmfEventRequest<TmfEvent>(TmfEvent.class, range, NB_EVENTS, BLOCK_SIZE) {
221 @Override
222 public void handleData() {
223 TmfEvent[] events = getData();
224 for (TmfEvent e : events) {
225 requestedEvents.add(e);
226 }
227 }
228 };
229 TmfProvider<TmfEvent>[] providers = (TmfProvider<TmfEvent>[]) TmfProviderManager.getProviders(TmfEvent.class, TmfTraceStub.class);
230 providers[0].processRequest(request, true);
231
232 assertEquals("nbEvents", NB_EVENTS, requestedEvents.size());
233 assertTrue("isCompleted", request.isCompleted());
234 assertFalse("isCancelled", request.isCancelled());
235
236 // Ensure that we have distinct events.
237 // Don't go overboard: we are not validating the stub!
238 for (int i = 0; i < NB_EVENTS; i++) {
239 assertEquals("Distinct events", i+1, requestedEvents.get(i).getTimestamp().getValue());
240 }
241 }
242
243 @SuppressWarnings("unchecked")
244 public void testProcessRequestForAllEvents() throws Exception {
245 final int BLOCK_SIZE = 1;
246 final Vector<TmfEvent> requestedEvents = new Vector<TmfEvent>();
247 // long nbExpectedEvents = NB_EVENTS;
248
249 TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BigBang, TmfTimestamp.BigCrunch);
250 final TmfEventRequest<TmfEvent> request = new TmfEventRequest<TmfEvent>(TmfEvent.class, range, NB_EVENTS, BLOCK_SIZE) {
251 @Override
252 public void handleData() {
253 TmfEvent[] events = getData();
254 for (TmfEvent e : events) {
255 requestedEvents.add(e);
256 }
257 }
258 };
259 TmfProvider<TmfEvent>[] providers = (TmfProvider<TmfEvent>[]) TmfProviderManager.getProviders(TmfEvent.class, TmfTraceStub.class);
260 providers[0].processRequest(request, true);
261
262 assertEquals("nbEvents", NB_EVENTS, requestedEvents.size());
263 assertTrue("isCompleted", request.isCompleted());
264 assertFalse("isCancelled", request.isCancelled());
265
266 // Ensure that we have distinct events.
267 // Don't go overboard: we are not validating the stub!
268 for (int i = 0; i < NB_EVENTS; i++) {
269 assertEquals("Distinct events", i+1, requestedEvents.get(i).getTimestamp().getValue());
270 }
271 }
272
273 // ------------------------------------------------------------------------
274 // cancel
275 // ------------------------------------------------------------------------
276
277 @SuppressWarnings("unchecked")
278 public void testCancel() throws Exception {
279 final Vector<TmfEvent> requestedEvents = new Vector<TmfEvent>();
280
281 TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BigBang, TmfTimestamp.BigCrunch);
282 final TmfEventRequest<TmfEvent> request = new TmfEventRequest<TmfEvent>(TmfEvent.class, range, NB_EVENTS, NB_EVENTS) {
283 @Override
284 public void handleData() {
285 TmfEvent[] events = getData();
286 for (TmfEvent e : events) {
287 requestedEvents.add(e);
288 }
289 // Cancel request after the first chunk is received
290 cancel();
291 }
292 };
293 TmfProvider<TmfEvent>[] providers = (TmfProvider<TmfEvent>[]) TmfProviderManager.getProviders(TmfEvent.class, TmfTraceStub.class);
294 providers[0].processRequest(request, true);
295
296 assertEquals("nbEvents", NB_EVENTS, requestedEvents.size());
297 assertTrue("isCompleted", request.isCompleted());
298 assertTrue("isCancelled", request.isCancelled());
299 }
300
301 }
This page took 0.037953 seconds and 5 git commands to generate.