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
CommitLineData
d18dd09b 1/*******************************************************************************
96b353c5
FC
2 * Copyright (c) 2009, 2010, 2012 Ericsson
3 *
d18dd09b
ASL
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
96b353c5 8 *
d18dd09b
ASL
9 * Contributors:
10 * Francois Chouinard - Initial API and implementation
11 *******************************************************************************/
12
6c13869b 13package org.eclipse.linuxtools.tmf.core.tests.request;
d18dd09b 14
d18dd09b
ASL
15import junit.framework.TestCase;
16
2f185a65 17import org.eclipse.linuxtools.internal.tmf.core.component.TmfEventThread;
8fd82db5 18import org.eclipse.linuxtools.internal.tmf.core.request.TmfRequestExecutor;
2f185a65
FC
19import org.eclipse.linuxtools.tmf.core.component.TmfDataProvider;
20import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
21import org.eclipse.linuxtools.tmf.core.event.TmfEvent;
5419a136
AM
22import org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest;
23import org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest.ExecutionType;
24import org.eclipse.linuxtools.tmf.core.request.TmfDataRequest;
2f185a65
FC
25import org.eclipse.linuxtools.tmf.core.signal.TmfSignal;
26import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
27import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation;
5c00c0b7 28
d18dd09b 29/**
d18dd09b
ASL
30 * Test suite for the TmfRequestExecutor class.
31 */
5419a136 32@SuppressWarnings({ "nls" })
d18dd09b
ASL
33public class TmfRequestExecutorTest extends TestCase {
34
96b353c5
FC
35 // ------------------------------------------------------------------------
36 // Variables
37 // ------------------------------------------------------------------------
38
39 private TmfRequestExecutor fExecutor;
5c00c0b7
FC
40
41 // ------------------------------------------------------------------------
42 // Housekeeping
43 // ------------------------------------------------------------------------
44
d18dd09b 45 /**
5c00c0b7 46 * @param name the test name
d18dd09b
ASL
47 */
48 public TmfRequestExecutorTest(String name) {
49 super(name);
50 }
51
96b353c5
FC
52 @Override
53 protected void setUp() throws Exception {
54 super.setUp();
55 fExecutor = new TmfRequestExecutor();
d18dd09b 56
96b353c5
FC
57 }
58
59 @Override
60 protected void tearDown() throws Exception {
61 super.tearDown();
62 fExecutor.stop();
63 }
d18dd09b 64
5c00c0b7
FC
65 // ------------------------------------------------------------------------
66 // Constructors
67 // ------------------------------------------------------------------------
d18dd09b
ASL
68
69 /**
8fd82db5 70 * Test method for {@link org.eclipse.linuxtools.internal.tmf.core.request.TmfRequestExecutor#TmfRequestExecutor()}.
d18dd09b
ASL
71 */
72 public void testTmfRequestExecutor() {
5c00c0b7 73 TmfRequestExecutor executor = new TmfRequestExecutor();
5c00c0b7
FC
74 assertFalse("isShutdown", executor.isShutdown());
75 assertFalse("isTerminated", executor.isTerminated());
d18dd09b
ASL
76 }
77
2fb2eb37 78 /**
8fd82db5 79 * Test method for {@link org.eclipse.linuxtools.internal.tmf.core.request.TmfRequestExecutor#stop()}.
2fb2eb37 80 */
5c00c0b7
FC
81 public void testStop() {
82 TmfRequestExecutor executor = new TmfRequestExecutor();
83 executor.stop();
5c00c0b7
FC
84 assertTrue("isShutdown", executor.isShutdown());
85 assertTrue("isTerminated", executor.isTerminated());
2fb2eb37 86 }
d18dd09b 87
ff4ed569 88 // ------------------------------------------------------------------------
5419a136 89 // execute
ff4ed569
FC
90 // ------------------------------------------------------------------------
91
2f185a65 92 // Dummy context
5419a136 93 private static class MyContext implements ITmfContext {
2f185a65
FC
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
5419a136 148 public void sendRequest(ITmfDataRequest request) {
2f185a65
FC
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
5419a136 162 public ITmfContext armRequest(ITmfDataRequest request) {
2f185a65
FC
163 return new MyContext(request.getNbRequested());
164 }
165 }
166
167 // Dummy request
5419a136
AM
168 private static class MyRequest extends TmfDataRequest {
169 public MyRequest(ExecutionType priority, int requested) {
170 super(ITmfEvent.class, 0, requested, priority);
2f185a65
FC
171 }
172 @Override
5419a136 173 public void done() {
2f185a65
FC
174 synchronized (monitor) {
175 monitor.notifyAll();
176 }
177 }
178 }
179
180 // Dummy thread
181 private static class MyThread extends TmfEventThread {
5419a136 182 public MyThread(TmfDataProvider provider, ITmfDataRequest request) {
2f185a65 183 super(provider, request);
2f185a65
FC
184 }
185 }
186
5419a136 187 private final static Object monitor = new Object();
2f185a65
FC
188
189 /**
190 * Test method for {@link org.eclipse.linuxtools.internal.tmf.core.request.TmfRequestExecutor#execute(java.lang.Runnable)}.
191 */
192 public void testExecute() {
2f185a65 193 MyProvider provider = new MyProvider();
5419a136
AM
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);
2f185a65
FC
200
201 // Start thread1
5419a136
AM
202 fExecutor.execute(thread1);
203 try {
204 Thread.sleep(1000);
205 } catch (InterruptedException e) {
2f185a65 206 }
5419a136 207 assertTrue("isRunning", thread1.isRunning());
2f185a65
FC
208
209 // Start higher priority thread2
5419a136
AM
210 fExecutor.execute(thread2);
211 try {
212 Thread.sleep(1000);
213 } catch (InterruptedException e) {
2f185a65 214 }
5419a136
AM
215 assertFalse("isRunning", thread1.isRunning());
216 assertTrue("isRunning", thread2.isRunning());
2f185a65
FC
217
218 // Wait for end of thread2
219 try {
5419a136
AM
220 synchronized (monitor) {
221 monitor.wait();
222 Thread.sleep(1000);
2f185a65
FC
223 }
224 } catch (InterruptedException e) {
225 }
5419a136
AM
226 assertTrue("isCompleted", thread2.isCompleted());
227 assertTrue("isRunning", thread1.isRunning());
2f185a65
FC
228
229 // Start higher priority thread3
5419a136
AM
230 fExecutor.execute(thread3);
231 try {
232 Thread.sleep(500);
233 } catch (InterruptedException e) {
2f185a65 234 }
5419a136
AM
235 assertFalse("isRunning", thread1.isRunning());
236 assertTrue("isRunning", thread3.isRunning());
2f185a65
FC
237
238 // Wait for end of thread3
239 try {
5419a136
AM
240 synchronized (monitor) {
241 monitor.wait();
242 Thread.sleep(500);
2f185a65
FC
243 }
244 } catch (InterruptedException e) {
245 }
5419a136
AM
246 assertTrue("isCompleted", thread3.isCompleted());
247 assertTrue("isRunning", thread1.isRunning());
2f185a65
FC
248
249 // Wait for thread1 completion
250 try {
5419a136
AM
251 synchronized (monitor) {
252 monitor.wait();
2f185a65
FC
253 }
254 } catch (InterruptedException e) {
255 }
5419a136 256 assertTrue("isCompleted", thread1.isCompleted());
2f185a65 257 }
ff4ed569
FC
258
259 // ------------------------------------------------------------------------
260 // toString
261 // ------------------------------------------------------------------------
262
263 /**
8fd82db5 264 * Test method for {@link org.eclipse.linuxtools.internal.tmf.core.request.TmfRequestExecutor#toString()}.
ff4ed569
FC
265 */
266 public void testToString() {
96b353c5
FC
267 TmfRequestExecutor executor = new TmfRequestExecutor();
268 String expected = "[TmfRequestExecutor(ThreadPoolExecutor)]";
269 assertEquals("toString", expected, executor.toString());
ff4ed569 270 }
d18dd09b
ASL
271
272}
This page took 0.049698 seconds and 5 git commands to generate.