- Minor modification of the FW API (better trace/parser integration)
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.tests / src / org / eclipse / linuxtools / tmf / trace / TmfExperimentTest.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>TmfEventLogTest</u></b>
31 * <p>
32 * TODO: Implement me. Please.
33 */
34 public class TmfExperimentTest {
35
36 private static final String DIRECTORY = "testfiles";
37 private static final String TEST_STREAM = "M-Test-10K";
38 private static final String EXPERIMENT = "MyExperiment";
39 private static String testfile;
40 private static int NB_EVENTS = 10000;
41 private static int fDefaultBlockSize = 1000;
42
43 // private static ITmfEventParser fParser;
44 private static ITmfTrace fStream;
45 private static TmfExperiment fExperiment;
46
47 // private static byte SCALE = (byte) -3;
48
49 @BeforeClass
50 public static void setUpBeforeClass() throws Exception {
51 String directory = new File(".").getCanonicalPath() + File.separator + DIRECTORY;
52 testfile = directory + File.separator + TEST_STREAM;
53
54 fStream = new TmfTraceStub(testfile);
55 fExperiment = new TmfExperiment(EXPERIMENT, new ITmfTrace[] { fStream });
56 while (fStream.getNbEvents() < NB_EVENTS) {
57 Thread.sleep(1000);
58 }
59 }
60
61 // ========================================================================
62 // Constructor
63 // ========================================================================
64
65 @Test
66 public void testBasicTmfTrace() {
67 assertEquals("GetId", EXPERIMENT, fExperiment.getExperimentId());
68 assertEquals("GetEpoch", TmfTimestamp.BigBang, fExperiment.getEpoch());
69 assertEquals("GetNbEvents", NB_EVENTS, fExperiment.getNbEvents());
70
71 TmfTimeRange timeRange = fExperiment.getTimeRange();
72 assertEquals("GetTimeRange-start", 1, timeRange.getStartTime().getValue());
73 assertEquals("GetTimeRange-end", NB_EVENTS, timeRange.getEndTime().getValue());
74 }
75
76 // ========================================================================
77 // Operators
78 // ========================================================================
79
80 @Test
81 public void testProcessRequestForNbEvents() throws Exception {
82 final int BLOCK_SIZE = 100;
83 final Vector<TmfEvent> requestedEvents = new Vector<TmfEvent>();
84
85 TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BigBang, TmfTimestamp.BigCrunch);
86 final TmfDataRequest<TmfEvent> request = new TmfDataRequest<TmfEvent>(range, 0, NB_EVENTS, BLOCK_SIZE) {
87 @Override
88 public void handleData() {
89 TmfEvent[] events = getData();
90 for (TmfEvent e : events) {
91 requestedEvents.add(e);
92 }
93 }
94 };
95 fExperiment.processRequest(request, true);
96
97 assertEquals("nbEvents", NB_EVENTS, requestedEvents.size());
98 assertTrue("isCompleted", request.isCompleted());
99 assertFalse("isCancelled", request.isCancelled());
100
101 // Ensure that we have distinct events.
102 // Don't go overboard: we are not validating the stub!
103 for (int i = 0; i < NB_EVENTS; i++) {
104 assertEquals("Distinct events", i+1, requestedEvents.get(i).getTimestamp().getValue());
105 }
106 }
107
108 @Test
109 public void testProcessRequestForAllEvents() throws Exception {
110 final int NB_EVENTS = -1;
111 final int BLOCK_SIZE = 1;
112 final Vector<TmfEvent> requestedEvents = new Vector<TmfEvent>();
113 int nbExpectedEvents = fExperiment.getNbEvents();
114
115 TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BigBang, TmfTimestamp.BigCrunch);
116 final TmfDataRequest<TmfEvent> request = new TmfDataRequest<TmfEvent>(range, 0, NB_EVENTS, BLOCK_SIZE) {
117 @Override
118 public void handleData() {
119 TmfEvent[] events = getData();
120 for (TmfEvent e : events) {
121 requestedEvents.add(e);
122 }
123 }
124 };
125 fExperiment.processRequest(request, true);
126
127 assertEquals("nbEvents", nbExpectedEvents, requestedEvents.size());
128 assertTrue("isCompleted", request.isCompleted());
129 assertFalse("isCancelled", request.isCancelled());
130
131 // Ensure that we have distinct events.
132 // Don't go overboard: we are not validating the stub!
133 for (int i = 0; i < nbExpectedEvents; i++) {
134 assertEquals("Distinct events", i+1, requestedEvents.get(i).getTimestamp().getValue());
135 }
136 }
137
138 // ========================================================================
139 // cancel
140 // ========================================================================
141
142 @Test
143 public void testCancel() throws Exception {
144 final int BLOCK_SIZE = fDefaultBlockSize;
145 final Vector<TmfEvent> requestedEvents = new Vector<TmfEvent>();
146
147 TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BigBang, TmfTimestamp.BigCrunch);
148 final TmfDataRequest<TmfEvent> request = new TmfDataRequest<TmfEvent>(range, 0, NB_EVENTS, BLOCK_SIZE) {
149 @Override
150 public void handleData() {
151 TmfEvent[] events = getData();
152 for (TmfEvent e : events) {
153 requestedEvents.add(e);
154 }
155 // Cancel request after the first chunk is received
156 cancel();
157 }
158 };
159 fExperiment.processRequest(request, true);
160
161 assertEquals("nbEvents", BLOCK_SIZE, requestedEvents.size());
162 assertTrue("isCompleted", request.isCompleted());
163 assertTrue("isCancelled", request.isCancelled());
164 }
165
166 }
This page took 0.035726 seconds and 5 git commands to generate.