1 /*******************************************************************************
2 * Copyright (c) 2009, 2010, 2012 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 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.core
.tests
.request
;
15 import junit
.framework
.TestCase
;
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
;
30 * Test suite for the TmfRequestExecutor class.
32 @SuppressWarnings({ "nls" })
33 public class TmfRequestExecutorTest
extends TestCase
{
35 // ------------------------------------------------------------------------
37 // ------------------------------------------------------------------------
39 private TmfRequestExecutor fExecutor
;
41 // ------------------------------------------------------------------------
43 // ------------------------------------------------------------------------
46 * @param name the test name
48 public TmfRequestExecutorTest(String name
) {
53 protected void setUp() throws Exception
{
55 fExecutor
= new TmfRequestExecutor();
60 protected void tearDown() throws Exception
{
65 // ------------------------------------------------------------------------
67 // ------------------------------------------------------------------------
70 * Test method for {@link org.eclipse.linuxtools.internal.tmf.core.request.TmfRequestExecutor#TmfRequestExecutor()}.
72 public void testTmfRequestExecutor() {
73 TmfRequestExecutor executor
= new TmfRequestExecutor();
74 assertFalse("isShutdown", executor
.isShutdown());
75 assertFalse("isTerminated", executor
.isTerminated());
79 * Test method for {@link org.eclipse.linuxtools.internal.tmf.core.request.TmfRequestExecutor#stop()}.
81 public void testStop() {
82 TmfRequestExecutor executor
= new TmfRequestExecutor();
84 assertTrue("isShutdown", executor
.isShutdown());
85 assertTrue("isTerminated", executor
.isTerminated());
88 // ------------------------------------------------------------------------
90 // ------------------------------------------------------------------------
93 private static class MyContext
implements ITmfContext
{
94 private long fNbRequested
;
97 public MyContext(long requested
) {
98 fNbRequested
= requested
;
102 public long getRank() {
103 return (fRank
<= fNbRequested
) ? fRank
: -1;
106 public ITmfLocation
getLocation() {
110 public boolean hasValidRank() {
114 public void setLocation(ITmfLocation location
) {
117 public void setRank(long rank
) {
121 public void increaseRank() {
125 public void dispose() {
128 public MyContext
clone() {
134 private static class MyProvider
extends TmfDataProvider
{
135 private ITmfEvent fEvent
= new TmfEvent();
138 public String
getName() {
142 public void dispose() {
145 public void broadcast(TmfSignal signal
) {
148 public void sendRequest(ITmfDataRequest request
) {
151 public void fireRequest() {
154 public void notifyPendingRequest(boolean isIncrement
) {
157 public ITmfEvent
getNext(ITmfContext context
) {
158 context
.increaseRank();
159 return context
.getRank() >= 0 ? fEvent
: null;
162 public ITmfContext
armRequest(ITmfDataRequest request
) {
163 return new MyContext(request
.getNbRequested());
168 private static class MyRequest
extends TmfDataRequest
{
169 public MyRequest(ExecutionType priority
, int requested
) {
170 super(ITmfEvent
.class, 0, requested
, priority
);
174 synchronized (monitor
) {
181 private static class MyThread
extends TmfEventThread
{
182 public MyThread(TmfDataProvider provider
, ITmfDataRequest request
) {
183 super(provider
, request
);
187 private final static Object monitor
= new Object();
190 * Test method for {@link org.eclipse.linuxtools.internal.tmf.core.request.TmfRequestExecutor#execute(java.lang.Runnable)}.
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
);
202 fExecutor
.execute(thread1
);
205 } catch (InterruptedException e
) {
207 assertTrue("isRunning", thread1
.isRunning());
209 // Start higher priority thread2
210 fExecutor
.execute(thread2
);
213 } catch (InterruptedException e
) {
215 assertFalse("isRunning", thread1
.isRunning());
216 assertTrue("isRunning", thread2
.isRunning());
218 // Wait for end of thread2
220 synchronized (monitor
) {
224 } catch (InterruptedException e
) {
226 assertTrue("isCompleted", thread2
.isCompleted());
227 assertTrue("isRunning", thread1
.isRunning());
229 // Start higher priority thread3
230 fExecutor
.execute(thread3
);
233 } catch (InterruptedException e
) {
235 assertFalse("isRunning", thread1
.isRunning());
236 assertTrue("isRunning", thread3
.isRunning());
238 // Wait for end of thread3
240 synchronized (monitor
) {
244 } catch (InterruptedException e
) {
246 assertTrue("isCompleted", thread3
.isCompleted());
247 assertTrue("isRunning", thread1
.isRunning());
249 // Wait for thread1 completion
251 synchronized (monitor
) {
254 } catch (InterruptedException e
) {
256 assertTrue("isCompleted", thread1
.isCompleted());
259 // ------------------------------------------------------------------------
261 // ------------------------------------------------------------------------
264 * Test method for {@link org.eclipse.linuxtools.internal.tmf.core.request.TmfRequestExecutor#toString()}.
266 public void testToString() {
267 TmfRequestExecutor executor
= new TmfRequestExecutor();
268 String expected
= "[TmfRequestExecutor(ThreadPoolExecutor)]";
269 assertEquals("toString", expected
, executor
.toString());