1 /*******************************************************************************
2 * Copyright (c) 2009, 2013 Ericsson
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
10 * Francois Chouinard - Initial API and implementation
11 * Alexandre Montplaisir - Port to JUnit4
12 *******************************************************************************/
14 package org
.eclipse
.linuxtools
.tmf
.core
.tests
.request
;
16 import static org
.junit
.Assert
.assertEquals
;
17 import static org
.junit
.Assert
.assertFalse
;
18 import static org
.junit
.Assert
.assertTrue
;
20 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.component
.TmfEventThread
;
21 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.request
.TmfRequestExecutor
;
22 import org
.eclipse
.linuxtools
.tmf
.core
.component
.TmfDataProvider
;
23 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfEvent
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.request
.ITmfDataRequest
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.request
.ITmfDataRequest
.ExecutionType
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.request
.TmfDataRequest
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfSignal
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfContext
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfLocation
;
32 import org
.junit
.After
;
33 import org
.junit
.Before
;
34 import org
.junit
.Test
;
37 * Test suite for the TmfRequestExecutor class.
39 public class TmfRequestExecutorTest
{
41 // ------------------------------------------------------------------------
43 // ------------------------------------------------------------------------
45 private TmfRequestExecutor fExecutor
;
47 // ------------------------------------------------------------------------
49 // ------------------------------------------------------------------------
56 fExecutor
= new TmfRequestExecutor();
63 public void tearDown() {
67 // ------------------------------------------------------------------------
69 // ------------------------------------------------------------------------
73 * {@link org.eclipse.linuxtools.internal.tmf.core.request.TmfRequestExecutor#TmfRequestExecutor()}
76 public void testTmfRequestExecutor() {
77 TmfRequestExecutor executor
= new TmfRequestExecutor();
78 assertFalse("isShutdown", executor
.isShutdown());
79 assertFalse("isTerminated", executor
.isTerminated());
84 * {@link org.eclipse.linuxtools.internal.tmf.core.request.TmfRequestExecutor#stop()}
87 public void testStop() {
88 TmfRequestExecutor executor
= new TmfRequestExecutor();
90 assertTrue("isShutdown", executor
.isShutdown());
91 assertTrue("isTerminated", executor
.isTerminated());
94 // ------------------------------------------------------------------------
96 // ------------------------------------------------------------------------
99 private static class MyContext
implements ITmfContext
{
100 private long fNbRequested
;
103 public MyContext(long requested
) {
104 fNbRequested
= requested
;
109 public long getRank() {
110 return (fRank
<= fNbRequested
) ? fRank
: -1;
114 public ITmfLocation
getLocation() {
119 public boolean hasValidRank() {
124 public void setLocation(ITmfLocation location
) {
128 public void setRank(long rank
) {
133 public void increaseRank() {
138 public void dispose() {
142 public MyContext
clone() {
148 private static class MyProvider
extends TmfDataProvider
{
149 private ITmfEvent fEvent
= new TmfEvent();
152 public String
getName() {
157 public void dispose() {
161 public void broadcast(TmfSignal signal
) {
165 public void sendRequest(ITmfDataRequest request
) {
169 public void fireRequest() {
173 public void notifyPendingRequest(boolean isIncrement
) {
177 public ITmfEvent
getNext(ITmfContext context
) {
178 context
.increaseRank();
179 return context
.getRank() >= 0 ? fEvent
: null;
183 public ITmfContext
armRequest(ITmfDataRequest request
) {
184 return new MyContext(request
.getNbRequested());
189 private static class MyRequest
extends TmfDataRequest
{
190 public MyRequest(ExecutionType priority
, int requested
) {
191 super(ITmfEvent
.class, 0, requested
, priority
);
196 synchronized (monitor
) {
203 private static class MyThread
extends TmfEventThread
{
204 public MyThread(TmfDataProvider provider
, ITmfDataRequest request
) {
205 super(provider
, request
);
209 private final static Object monitor
= new Object();
213 * {@link org.eclipse.linuxtools.internal.tmf.core.request.TmfRequestExecutor#execute(java.lang.Runnable)}
216 public void testExecute() {
217 MyProvider provider
= new MyProvider();
218 MyRequest request1
= new MyRequest(ExecutionType
.BACKGROUND
, Integer
.MAX_VALUE
/ 5);
219 MyThread thread1
= new MyThread(provider
, request1
);
220 MyRequest request2
= new MyRequest(ExecutionType
.FOREGROUND
, Integer
.MAX_VALUE
/ 10);
221 MyThread thread2
= new MyThread(provider
, request2
);
222 MyRequest request3
= new MyRequest(ExecutionType
.FOREGROUND
, Integer
.MAX_VALUE
/ 10);
223 MyThread thread3
= new MyThread(provider
, request3
);
226 fExecutor
.execute(thread1
);
229 } catch (InterruptedException e
) {
231 assertTrue("isRunning", thread1
.isRunning());
233 // Start higher priority thread2
234 fExecutor
.execute(thread2
);
237 } catch (InterruptedException e
) {
239 assertFalse("isRunning", thread1
.isRunning());
240 assertTrue("isRunning", thread2
.isRunning());
242 // Wait for end of thread2
244 synchronized (monitor
) {
248 } catch (InterruptedException e
) {
250 assertTrue("isCompleted", thread2
.isCompleted());
251 assertTrue("isRunning", thread1
.isRunning());
253 // Start higher priority thread3
254 fExecutor
.execute(thread3
);
257 } catch (InterruptedException e
) {
259 assertFalse("isRunning", thread1
.isRunning());
260 assertTrue("isRunning", thread3
.isRunning());
262 // Wait for end of thread3
264 synchronized (monitor
) {
268 } catch (InterruptedException e
) {
270 assertTrue("isCompleted", thread3
.isCompleted());
271 assertTrue("isRunning", thread1
.isRunning());
273 // Wait for thread1 completion
275 synchronized (monitor
) {
278 } catch (InterruptedException e
) {
280 assertTrue("isCompleted", thread1
.isCompleted());
283 // ------------------------------------------------------------------------
285 // ------------------------------------------------------------------------
289 * {@link org.eclipse.linuxtools.internal.tmf.core.request.TmfRequestExecutor#toString()}
292 public void testToString() {
293 TmfRequestExecutor executor
= new TmfRequestExecutor();
294 String expected
= "[TmfRequestExecutor(ThreadPoolExecutor)]";
295 assertEquals("toString", expected
, executor
.toString());