Commit | Line | Data |
---|---|---|
4ab33d2b AO |
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: | |
165c977c | 10 | * Francois Chouinard - Initial API and implementation |
4ab33d2b AO |
11 | *******************************************************************************/ |
12 | ||
165c977c | 13 | package org.eclipse.linuxtools.tmf.request; |
4ab33d2b | 14 | |
165c977c FC |
15 | import static org.junit.Assert.assertEquals; |
16 | import static org.junit.Assert.assertFalse; | |
17 | import static org.junit.Assert.assertTrue; | |
4ab33d2b AO |
18 | |
19 | import java.util.Vector; | |
20 | ||
21 | import org.eclipse.linuxtools.tmf.event.TmfEvent; | |
1f506a43 | 22 | import org.eclipse.linuxtools.tmf.event.TmfTimeRange; |
4ab33d2b | 23 | import org.eclipse.linuxtools.tmf.event.TmfTimestamp; |
165c977c FC |
24 | import org.eclipse.linuxtools.tmf.request.ITmfRequestHandler; |
25 | import org.eclipse.linuxtools.tmf.request.TmfDataRequest; | |
4ab33d2b AO |
26 | import org.junit.BeforeClass; |
27 | import org.junit.Test; | |
28 | ||
29 | /** | |
165c977c | 30 | * <b><u>TmfDataRequestTest</u></b> |
4ab33d2b AO |
31 | * <p> |
32 | * TODO: Implement me. Please. | |
33 | */ | |
165c977c | 34 | public class TmfDataRequestTest { |
4ab33d2b | 35 | |
165c977c | 36 | private static ITmfRequestHandler<TmfEvent> fProcessor = null; |
4ab33d2b AO |
37 | |
38 | @BeforeClass | |
39 | public static void setUpBeforeClass() throws Exception { | |
40 | fProcessor = new TmfRequestHandlerStub(); | |
41 | } | |
42 | ||
43 | // ======================================================================== | |
44 | // Constructor | |
45 | // ======================================================================== | |
46 | ||
47 | @Test | |
48 | public void testConstructorForRange() throws Exception { | |
1f506a43 | 49 | TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BigBang, TmfTimestamp.BigCrunch); |
165c977c | 50 | TmfDataRequest<TmfEvent> request = new TmfDataRequest<TmfEvent>(range, 0, -1, 1); |
4ab33d2b AO |
51 | |
52 | assertEquals("StartTime", TmfTimestamp.BigBang, request.getRange().getStartTime()); | |
53 | assertEquals("EndTime", TmfTimestamp.BigCrunch, request.getRange().getEndTime()); | |
54 | assertEquals("Offset", 0, request.getOffset()); | |
165c977c | 55 | assertEquals("NbRequestedEvents", -1, request.getNbRequestedItems()); |
4ab33d2b AO |
56 | } |
57 | ||
58 | @Test | |
59 | public void testConstructorForNbEvents() throws Exception { | |
1f506a43 | 60 | TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BigBang, TmfTimestamp.BigCrunch); |
165c977c | 61 | TmfDataRequest<TmfEvent> request = new TmfDataRequest<TmfEvent>(range, 0, 10, 1); |
4ab33d2b AO |
62 | |
63 | assertEquals("StartTime", TmfTimestamp.BigBang, request.getRange().getStartTime()); | |
64 | assertEquals("EndTime", TmfTimestamp.BigCrunch, request.getRange().getEndTime()); | |
65 | assertEquals("Offset", 0, request.getOffset()); | |
165c977c | 66 | assertEquals("NbRequestedEvents", 10, request.getNbRequestedItems()); |
4ab33d2b AO |
67 | } |
68 | ||
69 | @Test | |
70 | public void testConstructorWithOffset() throws Exception { | |
1f506a43 | 71 | TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BigBang, TmfTimestamp.BigCrunch); |
165c977c | 72 | TmfDataRequest<TmfEvent> request = new TmfDataRequest<TmfEvent>(range, 5, 10, 1); |
4ab33d2b AO |
73 | |
74 | assertEquals("StartTime", TmfTimestamp.BigBang, request.getRange().getStartTime()); | |
75 | assertEquals("EndTime", TmfTimestamp.BigCrunch, request.getRange().getEndTime()); | |
76 | assertEquals("Offset", 5, request.getOffset()); | |
165c977c | 77 | assertEquals("NbRequestedEvents", 10, request.getNbRequestedItems()); |
4ab33d2b AO |
78 | } |
79 | ||
80 | @Test | |
81 | public void testConstructorWithNegativeOffset() throws Exception { | |
1f506a43 | 82 | TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BigBang, TmfTimestamp.BigCrunch); |
165c977c | 83 | TmfDataRequest<TmfEvent> request = new TmfDataRequest<TmfEvent>(range, -5, 10, 1); |
4ab33d2b AO |
84 | |
85 | assertEquals("StartTime", TmfTimestamp.BigBang, request.getRange().getStartTime()); | |
86 | assertEquals("EndTime", TmfTimestamp.BigCrunch, request.getRange().getEndTime()); | |
87 | assertEquals("Offset", -5, request.getOffset()); | |
165c977c | 88 | assertEquals("NbRequestedEvents", 10, request.getNbRequestedItems()); |
4ab33d2b AO |
89 | } |
90 | ||
91 | // ======================================================================== | |
92 | // process | |
93 | // ======================================================================== | |
94 | ||
95 | @Test | |
96 | public void testProcessRequestForNbEvents() throws Exception { | |
97 | ||
98 | final int NB_EVENTS = 10 * 1000; | |
99 | final int BLOCK_SIZE = 100; | |
100 | final Vector<TmfEvent> requestedEvents = new Vector<TmfEvent>(); | |
101 | ||
1f506a43 | 102 | TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BigBang, TmfTimestamp.BigCrunch); |
165c977c | 103 | final TmfDataRequest<TmfEvent> request = new TmfDataRequest<TmfEvent>(range, 0, NB_EVENTS, BLOCK_SIZE) { |
4ab33d2b | 104 | @Override |
0ab46cd3 | 105 | public void handleData() { |
165c977c | 106 | TmfEvent[] events = getData(); |
4ab33d2b AO |
107 | for (TmfEvent e : events) { |
108 | requestedEvents.add(e); | |
109 | } | |
110 | } | |
111 | }; | |
165c977c | 112 | fProcessor.processRequest(request, true); |
4ab33d2b AO |
113 | |
114 | assertEquals("nbEvents", NB_EVENTS, requestedEvents.size()); | |
115 | assertTrue("isCompleted", request.isCompleted()); | |
116 | assertFalse("isCancelled", request.isCancelled()); | |
117 | ||
118 | // Ensure that we have distinct events. | |
119 | // Don't go overboard: we are not validating the stub! | |
120 | for (int i = 0; i < NB_EVENTS; i++) { | |
165c977c | 121 | assertEquals("Distinct events", i+1, requestedEvents.get(i).getTimestamp().getValue()); |
4ab33d2b AO |
122 | } |
123 | } | |
124 | ||
125 | @Test | |
126 | public void testProcessRequestForAllEvents() throws Exception { | |
127 | ||
128 | final int NB_EVENTS = -1; | |
129 | final int BLOCK_SIZE = 1; | |
130 | final Vector<TmfEvent> requestedEvents = new Vector<TmfEvent>(); | |
131 | int nbExpectedEvents = TmfRequestHandlerStub.MAX_GENERATED_EVENTS; | |
132 | ||
1f506a43 | 133 | TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BigBang, TmfTimestamp.BigCrunch); |
165c977c | 134 | final TmfDataRequest<TmfEvent> request = new TmfDataRequest<TmfEvent>(range, 0, NB_EVENTS, BLOCK_SIZE) { |
4ab33d2b | 135 | @Override |
0ab46cd3 | 136 | public void handleData() { |
165c977c | 137 | TmfEvent[] events = getData(); |
4ab33d2b AO |
138 | for (TmfEvent e : events) { |
139 | requestedEvents.add(e); | |
140 | } | |
141 | } | |
142 | }; | |
165c977c | 143 | fProcessor.processRequest(request, true); |
4ab33d2b AO |
144 | |
145 | assertEquals("nbEvents", nbExpectedEvents, requestedEvents.size()); | |
146 | assertTrue("isCompleted", request.isCompleted()); | |
147 | assertFalse("isCancelled", request.isCancelled()); | |
148 | ||
149 | // Ensure that we have distinct events. | |
150 | // Don't go overboard: we are not validating the stub! | |
151 | for (int i = 0; i < nbExpectedEvents; i++) { | |
165c977c | 152 | assertEquals("Distinct events", i+1, requestedEvents.get(i).getTimestamp().getValue()); |
4ab33d2b AO |
153 | } |
154 | } | |
155 | ||
156 | // @Test | |
157 | public void testProcessRequestWithOffset() throws Exception { | |
158 | ||
159 | final int NB_EVENTS = -1; | |
160 | final int BLOCK_SIZE = 1; | |
161 | final int OFFSET = 5; | |
162 | final Vector<TmfEvent> requestedEvents = new Vector<TmfEvent>(); | |
163 | int nbExpectedEvents = TmfRequestHandlerStub.MAX_GENERATED_EVENTS; | |
164 | ||
1f506a43 | 165 | TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BigBang, TmfTimestamp.BigCrunch); |
165c977c | 166 | final TmfDataRequest<TmfEvent> request = new TmfDataRequest<TmfEvent>(range, OFFSET, NB_EVENTS, BLOCK_SIZE) { |
4ab33d2b | 167 | @Override |
0ab46cd3 | 168 | public void handleData() { |
165c977c | 169 | TmfEvent[] events = getData(); |
4ab33d2b AO |
170 | for (TmfEvent e : events) { |
171 | requestedEvents.add(e); | |
172 | } | |
173 | } | |
174 | }; | |
165c977c | 175 | fProcessor.processRequest(request, true); |
4ab33d2b AO |
176 | |
177 | assertEquals("nbEvents", nbExpectedEvents, requestedEvents.size()); | |
178 | assertTrue("isCompleted", request.isCompleted()); | |
179 | assertFalse("isCancelled", request.isCancelled()); | |
180 | ||
181 | // Ensure that we have distinct events. | |
182 | // Don't go overboard: we are not validating the stub! | |
183 | for (int i = 0; i < nbExpectedEvents; i++) { | |
184 | assertEquals("Distinct events", i + OFFSET, requestedEvents.get(i).getTimestamp().getValue()); | |
185 | } | |
186 | } | |
187 | ||
188 | // l@Test | |
189 | public void testProcessRequestWithNegativeOffset() throws Exception { | |
190 | ||
191 | final int NB_EVENTS = -1; | |
192 | final int BLOCK_SIZE = 1; | |
193 | final int OFFSET = -5; | |
194 | final Vector<TmfEvent> requestedEvents = new Vector<TmfEvent>(); | |
195 | int nbExpectedEvents = TmfRequestHandlerStub.MAX_GENERATED_EVENTS; | |
196 | ||
1f506a43 | 197 | TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BigBang, TmfTimestamp.BigCrunch); |
165c977c | 198 | final TmfDataRequest<TmfEvent> request = new TmfDataRequest<TmfEvent>(range, OFFSET, NB_EVENTS, BLOCK_SIZE) { |
4ab33d2b | 199 | @Override |
0ab46cd3 | 200 | public void handleData() { |
165c977c | 201 | TmfEvent[] events = getData(); |
4ab33d2b AO |
202 | for (TmfEvent e : events) { |
203 | requestedEvents.add(e); | |
204 | } | |
205 | } | |
206 | }; | |
165c977c | 207 | fProcessor.processRequest(request, true); |
4ab33d2b AO |
208 | |
209 | assertEquals("nbEvents", nbExpectedEvents, requestedEvents.size()); | |
210 | assertTrue("isCompleted", request.isCompleted()); | |
211 | assertFalse("isCancelled", request.isCancelled()); | |
212 | ||
213 | // Ensure that we have distinct events. | |
214 | // Don't go overboard: we are not validating the stub! | |
215 | for (int i = 0; i < nbExpectedEvents; i++) { | |
216 | assertEquals("Distinct events", i + OFFSET, requestedEvents.get(i).getTimestamp().getValue()); | |
217 | } | |
218 | } | |
219 | ||
220 | // ======================================================================== | |
221 | // cancel | |
222 | // ======================================================================== | |
223 | ||
224 | @Test | |
225 | public void testCancel() throws Exception { | |
226 | ||
227 | final int NB_EVENTS = 10 * 1000; | |
228 | final int BLOCK_SIZE = 100; | |
229 | final Vector<TmfEvent> requestedEvents = new Vector<TmfEvent>(); | |
230 | ||
1f506a43 | 231 | TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BigBang, TmfTimestamp.BigCrunch); |
165c977c | 232 | final TmfDataRequest<TmfEvent> request = new TmfDataRequest<TmfEvent>(range, 0, NB_EVENTS, BLOCK_SIZE) { |
4ab33d2b | 233 | @Override |
0ab46cd3 | 234 | public void handleData() { |
165c977c | 235 | TmfEvent[] events = getData(); |
4ab33d2b AO |
236 | for (TmfEvent e : events) { |
237 | requestedEvents.add(e); | |
238 | } | |
239 | // Cancel request after the first chunk is received | |
240 | cancel(); | |
241 | } | |
242 | }; | |
165c977c | 243 | fProcessor.processRequest(request, true); |
4ab33d2b AO |
244 | |
245 | assertEquals("nbEvents", BLOCK_SIZE, requestedEvents.size()); | |
246 | assertTrue("isCompleted", request.isCompleted()); | |
247 | assertTrue("isCancelled", request.isCancelled()); | |
248 | } | |
249 | ||
250 | } |