KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > blocks > MessageDispatcherThreadingTest


1 package org.jgroups.blocks;
2
3 import junit.framework.TestCase;
4
5 import org.jgroups.tests.MessageDispatcherTest;
6
7 import java.util.Arrays JavaDoc;
8 import java.util.HashSet JavaDoc;
9 import java.util.Set JavaDoc;
10
11 /*
12  * Created on May 16, 2005
13  */

14
15 /**
16  * @author BConlon
17  * @version $Revision: 1.1 $
18  */

19 public class MessageDispatcherThreadingTest
20     extends TestCase {
21
22     //~ Methods ----------------------------------------------------------------------------------------------
23

24     public void testAllThreadsAreStopped() throws Exception JavaDoc {
25         // get the threads for this thread group
26
ThreadGroup JavaDoc threadGroup = Thread.currentThread().getThreadGroup();
27         Thread JavaDoc[] originalThreads = new Thread JavaDoc[threadGroup.activeCount()];
28
29         // populate the array with the threads in this group.
30
threadGroup.enumerate(originalThreads);
31         Set JavaDoc originalThreadSet = new HashSet JavaDoc(Arrays.asList(originalThreads));
32
33         new MessageDispatcherTest().start();
34
35         // wait for anything necessary to finish up.
36
Thread.sleep(10000);
37
38         Thread JavaDoc[] currentThreads = new Thread JavaDoc[threadGroup.activeCount()];
39
40         // populate the array with the threads in this group.
41
boolean residualThreadsFound = false;
42         threadGroup.enumerate(currentThreads);
43
44         // Look for threads not alive when we started.
45
for (int i = 0; i < currentThreads.length; i++) {
46             Thread JavaDoc thread = currentThreads[i];
47             if (! originalThreadSet.contains(thread) && thread.isAlive()) {
48                 residualThreadsFound = true;
49                 System.out.println("The following thread was left over: " + thread);
50             }
51         }
52         assertFalse("Residual threads were found", residualThreadsFound);
53     }
54 }
55
56
Popular Tags