tmf: Fix regression in event requests
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core.tests / src / org / eclipse / linuxtools / tmf / core / tests / request / TmfRequestExecutorTest.java
1 /*******************************************************************************
2 * Copyright (c) 2009, 2010, 2012 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.core.tests.request;
14
15 import junit.framework.TestCase;
16
17 import org.eclipse.linuxtools.internal.tmf.core.component.TmfEventThread;
18 import org.eclipse.linuxtools.internal.tmf.core.request.TmfRequestExecutor;
19 import org.eclipse.linuxtools.tmf.core.component.TmfDataProvider;
20 import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
21 import org.eclipse.linuxtools.tmf.core.event.TmfEvent;
22 import org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest;
23 import org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest.ExecutionType;
24 import org.eclipse.linuxtools.tmf.core.request.TmfDataRequest;
25 import org.eclipse.linuxtools.tmf.core.signal.TmfSignal;
26 import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
27 import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation;
28
29 /**
30 * Test suite for the TmfRequestExecutor class.
31 */
32 @SuppressWarnings({ "nls" })
33 public class TmfRequestExecutorTest extends TestCase {
34
35 // ------------------------------------------------------------------------
36 // Variables
37 // ------------------------------------------------------------------------
38
39 private TmfRequestExecutor fExecutor;
40
41 // ------------------------------------------------------------------------
42 // Housekeeping
43 // ------------------------------------------------------------------------
44
45 /**
46 * @param name the test name
47 */
48 public TmfRequestExecutorTest(String name) {
49 super(name);
50 }
51
52 @Override
53 protected void setUp() throws Exception {
54 super.setUp();
55 fExecutor = new TmfRequestExecutor();
56
57 }
58
59 @Override
60 protected void tearDown() throws Exception {
61 super.tearDown();
62 fExecutor.stop();
63 }
64
65 // ------------------------------------------------------------------------
66 // Constructors
67 // ------------------------------------------------------------------------
68
69 /**
70 * Test method for {@link org.eclipse.linuxtools.internal.tmf.core.request.TmfRequestExecutor#TmfRequestExecutor()}.
71 */
72 public void testTmfRequestExecutor() {
73 TmfRequestExecutor executor = new TmfRequestExecutor();
74 assertFalse("isShutdown", executor.isShutdown());
75 assertFalse("isTerminated", executor.isTerminated());
76 }
77
78 /**
79 * Test method for {@link org.eclipse.linuxtools.internal.tmf.core.request.TmfRequestExecutor#stop()}.
80 */
81 public void testStop() {
82 TmfRequestExecutor executor = new TmfRequestExecutor();
83 executor.stop();
84 assertTrue("isShutdown", executor.isShutdown());
85 assertTrue("isTerminated", executor.isTerminated());
86 }
87
88 // ------------------------------------------------------------------------
89 // execute
90 // ------------------------------------------------------------------------
91
92 // Dummy context
93 private static class MyContext implements ITmfContext {
94 private long fNbRequested;
95 private long fRank;
96
97 public MyContext(long requested) {
98 fNbRequested = requested;
99 fRank = 0;
100 }
101 @Override
102 public long getRank() {
103 return (fRank <= fNbRequested) ? fRank : -1;
104 }
105 @Override
106 public ITmfLocation getLocation() {
107 return null;
108 }
109 @Override
110 public boolean hasValidRank() {
111 return true;
112 }
113 @Override
114 public void setLocation(ITmfLocation location) {
115 }
116 @Override
117 public void setRank(long rank) {
118 fRank = rank;
119 }
120 @Override
121 public void increaseRank() {
122 fRank++;
123 }
124 @Override
125 public void dispose() {
126 }
127 @Override
128 public MyContext clone() {
129 return this;
130 }
131 }
132
133 // Dummy provider
134 private static class MyProvider extends TmfDataProvider {
135 private ITmfEvent fEvent = new TmfEvent();
136
137 @Override
138 public String getName() {
139 return null;
140 }
141 @Override
142 public void dispose() {
143 }
144 @Override
145 public void broadcast(TmfSignal signal) {
146 }
147 @Override
148 public void sendRequest(ITmfDataRequest request) {
149 }
150 @Override
151 public void fireRequest() {
152 }
153 @Override
154 public void notifyPendingRequest(boolean isIncrement) {
155 }
156 @Override
157 public ITmfEvent getNext(ITmfContext context) {
158 context.increaseRank();
159 return context.getRank() >= 0 ? fEvent : null;
160 }
161 @Override
162 public ITmfContext armRequest(ITmfDataRequest request) {
163 return new MyContext(request.getNbRequested());
164 }
165 }
166
167 // Dummy request
168 private static class MyRequest extends TmfDataRequest {
169 public MyRequest(ExecutionType priority, int requested) {
170 super(ITmfEvent.class, 0, requested, priority);
171 }
172 @Override
173 public void done() {
174 synchronized (monitor) {
175 monitor.notifyAll();
176 }
177 }
178 }
179
180 // Dummy thread
181 private static class MyThread extends TmfEventThread {
182 public MyThread(TmfDataProvider provider, ITmfDataRequest request) {
183 super(provider, request);
184 }
185 }
186
187 private final static Object monitor = new Object();
188
189 /**
190 * Test method for {@link org.eclipse.linuxtools.internal.tmf.core.request.TmfRequestExecutor#execute(java.lang.Runnable)}.
191 */
192 public void testExecute() {
193 MyProvider provider = new MyProvider();
194 MyRequest request1 = new MyRequest(ExecutionType.BACKGROUND, Integer.MAX_VALUE / 5);
195 MyThread thread1 = new MyThread(provider, request1);
196 MyRequest request2 = new MyRequest(ExecutionType.FOREGROUND, Integer.MAX_VALUE / 10);
197 MyThread thread2 = new MyThread(provider, request2);
198 MyRequest request3 = new MyRequest(ExecutionType.FOREGROUND, Integer.MAX_VALUE / 10);
199 MyThread thread3 = new MyThread(provider, request3);
200
201 // Start thread1
202 fExecutor.execute(thread1);
203 try {
204 Thread.sleep(1000);
205 } catch (InterruptedException e) {
206 }
207 assertTrue("isRunning", thread1.isRunning());
208
209 // Start higher priority thread2
210 fExecutor.execute(thread2);
211 try {
212 Thread.sleep(1000);
213 } catch (InterruptedException e) {
214 }
215 assertFalse("isRunning", thread1.isRunning());
216 assertTrue("isRunning", thread2.isRunning());
217
218 // Wait for end of thread2
219 try {
220 synchronized (monitor) {
221 monitor.wait();
222 Thread.sleep(1000);
223 }
224 } catch (InterruptedException e) {
225 }
226 assertTrue("isCompleted", thread2.isCompleted());
227 assertTrue("isRunning", thread1.isRunning());
228
229 // Start higher priority thread3
230 fExecutor.execute(thread3);
231 try {
232 Thread.sleep(500);
233 } catch (InterruptedException e) {
234 }
235 assertFalse("isRunning", thread1.isRunning());
236 assertTrue("isRunning", thread3.isRunning());
237
238 // Wait for end of thread3
239 try {
240 synchronized (monitor) {
241 monitor.wait();
242 Thread.sleep(500);
243 }
244 } catch (InterruptedException e) {
245 }
246 assertTrue("isCompleted", thread3.isCompleted());
247 assertTrue("isRunning", thread1.isRunning());
248
249 // Wait for thread1 completion
250 try {
251 synchronized (monitor) {
252 monitor.wait();
253 }
254 } catch (InterruptedException e) {
255 }
256 assertTrue("isCompleted", thread1.isCompleted());
257 }
258
259 // ------------------------------------------------------------------------
260 // toString
261 // ------------------------------------------------------------------------
262
263 /**
264 * Test method for {@link org.eclipse.linuxtools.internal.tmf.core.request.TmfRequestExecutor#toString()}.
265 */
266 public void testToString() {
267 TmfRequestExecutor executor = new TmfRequestExecutor();
268 String expected = "[TmfRequestExecutor(ThreadPoolExecutor)]";
269 assertEquals("toString", expected, executor.toString());
270 }
271
272 }
This page took 0.051399 seconds and 5 git commands to generate.