General improvements:
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.tests / src / org / eclipse / linuxtools / tmf / 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.trace;
14
15 import static org.junit.Assert.assertEquals;
16 import static org.junit.Assert.assertFalse;
17 import static org.junit.Assert.assertTrue;
18
19 import java.io.File;
20 import java.util.Vector;
21
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;
28
29 /**
30 * <b><u>TmfEventStreamTest</u></b>
31 * <p>
32 * TODO: Implement me. Please.
33 */
34 public class TmfTraceTest {
35
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;
41
42 private static byte SCALE = (byte) -3;
43
44 /**
45 * @throws java.lang.Exception
46 */
47 @BeforeClass
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);
52 }
53
54 // ========================================================================
55 // Constructor
56 // ========================================================================
57
58 @Test
59 public void testDefaultConstructor() throws Exception {
60 TmfTraceStub trace = new TmfTraceStub(testfile, true);
61
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());
66 }
67
68 @Test
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());
74 }
75
76 // ========================================================================
77 // seek
78 // ========================================================================
79
80 @Test
81 public void testSeekOnCacheBoundary() throws Exception {
82 TmfTraceContext context = fTrace.seekLocation(null);
83
84 context = fTrace.seekEvent(new TmfTimestamp(0, SCALE, 0));
85 TmfEvent event = fTrace.getNextEvent(context);
86 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
87
88 context = fTrace.seekEvent(new TmfTimestamp(1000, SCALE, 0));
89 event = fTrace.getNextEvent(context);
90 assertEquals("Event timestamp", 1000, event.getTimestamp().getValue());
91
92 context = fTrace.seekEvent(new TmfTimestamp(4000, SCALE, 0));
93 event = fTrace.getNextEvent(context);
94 assertEquals("Event timestamp", 4000, event.getTimestamp().getValue());
95 }
96
97 @Test
98 public void testSeekNotOnCacheBoundary() throws Exception {
99 TmfTraceContext context = fTrace.seekLocation(null);
100
101 context = fTrace.seekEvent(new TmfTimestamp(1, SCALE, 0));
102 TmfEvent event = fTrace.getNextEvent(context);
103 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
104
105 context = fTrace.seekEvent(new TmfTimestamp(999, SCALE, 0));
106 event = fTrace.getNextEvent(context);
107 assertEquals("Event timestamp", 999, event.getTimestamp().getValue());
108
109 context = fTrace.seekEvent(new TmfTimestamp(4499, SCALE, 0));
110 event = fTrace.getNextEvent(context);
111 assertEquals("Event timestamp", 4499, event.getTimestamp().getValue());
112 }
113
114 @Test
115 public void testSeekForEventOutOfBounds() throws Exception {
116 TmfTraceContext context = fTrace.seekLocation(null);
117
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());
122
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);
127 }
128
129 // ========================================================================
130 // getNextEvent
131 // ========================================================================
132
133 @Test
134 public void testGetNextEvent() throws Exception {
135 TmfTraceContext context = fTrace.seekLocation(null);
136
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());
141
142 for (int i = 2; i < 20; i++) {
143 event = fTrace.getNextEvent(context);
144 assertEquals("Event timestamp", i, event.getTimestamp().getValue());
145 }
146 }
147
148 // ========================================================================
149 // processRequest
150 // ========================================================================
151
152 @Test
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>();
157
158 TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BigBang, TmfTimestamp.BigCrunch);
159 final TmfDataRequest<TmfEvent> request = new TmfDataRequest<TmfEvent>(range, 0, NB_EVENTS, BLOCK_SIZE) {
160 @Override
161 public void handleData() {
162 TmfEvent[] events = getData();
163 for (TmfEvent e : events) {
164 requestedEvents.add(e);
165 }
166 }
167 };
168 fTrace.processRequest(request, true);
169
170 assertEquals("nbEvents", NB_EVENTS, requestedEvents.size());
171 assertTrue("isCompleted", request.isCompleted());
172 assertFalse("isCancelled", request.isCancelled());
173
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());
178 }
179 }
180
181 @Test
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();
187
188 TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BigBang, TmfTimestamp.BigCrunch);
189 final TmfDataRequest<TmfEvent> request = new TmfDataRequest<TmfEvent>(range, 0, 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", nbExpectedEvents, 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 < nbExpectedEvents; i++) {
207 assertEquals("Distinct events", i+1, requestedEvents.get(i).getTimestamp().getValue());
208 }
209 }
210
211 // ========================================================================
212 // cancel
213 // ========================================================================
214
215 @Test
216 public void testCancel() throws Exception {
217 final Vector<TmfEvent> requestedEvents = new Vector<TmfEvent>();
218
219 TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BigBang, TmfTimestamp.BigCrunch);
220 final TmfDataRequest<TmfEvent> request = new TmfDataRequest<TmfEvent>(range, 0, NB_EVENTS, NB_EVENTS) {
221 @Override
222 public void handleData() {
223 TmfEvent[] events = getData();
224 for (TmfEvent e : events) {
225 requestedEvents.add(e);
226 }
227 // Cancel request after the first chunk is received
228 cancel();
229 }
230 };
231 fTrace.processRequest(request, true);
232
233 assertEquals("nbEvents", NB_EVENTS, requestedEvents.size());
234 assertTrue("isCompleted", request.isCompleted());
235 assertTrue("isCancelled", request.isCancelled());
236 }
237
238 }
This page took 0.036517 seconds and 5 git commands to generate.