KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > mx4j > timer > TimeQueueTest


1 /*
2  * Copyright (C) The MX4J Contributors.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the MX4J License version 1.0.
6  * See the terms of the MX4J License in the documentation provided with this software.
7  */

8
9 package test.mx4j.timer;
10
11 import java.lang.reflect.Field JavaDoc;
12
13 import mx4j.timer.TimeQueue;
14 import mx4j.timer.TimeTask;
15 import test.MX4JTestCase;
16
17 /**
18  * @version $Revision: 1.3 $
19  */

20 public class TimeQueueTest extends MX4JTestCase
21 {
22    public TimeQueueTest(String JavaDoc s)
23    {
24       super(s);
25    }
26
27    public void testStop() throws Exception JavaDoc
28    {
29       TimeQueue queue = new TimeQueue();
30       queue.start();
31
32       // Wait a while to let the thread start
33
sleep(1000);
34
35       final int sleep = 5000;
36
37       // Post a task to simulate work
38
TimeTask task = new TimeTask()
39       {
40          public void run()
41          {
42             sleep(sleep);
43          }
44       };
45
46       queue.schedule(task);
47
48       // Wait for the task to be executed
49
sleep(1000);
50
51       // Stop the queue. This will cause the task above to interrupt,
52
// but we set the flag again as would be in a normal task
53
queue.stop();
54
55       // Wait until the task is finished; the TimeQueue should have cleaned up
56
sleep(sleep);
57
58       // I want to be sure the thread has really shutdown
59
Field JavaDoc field = queue.getClass().getDeclaredField("thread");
60       field.setAccessible(true);
61       Thread JavaDoc thread = (Thread JavaDoc)field.get(queue);
62       if (thread != null && thread.isAlive()) fail("TimeQueue not stopped");
63    }
64 }
65
Popular Tags