1
2
3
4 package javax.jmdns.test;
5
6 import static junit.framework.Assert.assertFalse;
7 import static junit.framework.Assert.assertTrue;
8
9 import javax.jmdns.impl.DNSStatefulObject.DNSStatefulObjectSemaphore;
10
11 import org.junit.After;
12 import org.junit.Before;
13 import org.junit.Test;
14
15
16
17
18 public class DNSStatefulObjectTest {
19
20 public static final class WaitingThread extends Thread {
21
22 private final DNSStatefulObjectSemaphore _semaphore;
23 private final long _timeout;
24
25 private boolean _hasFinished;
26
27 public WaitingThread(DNSStatefulObjectSemaphore semaphore, long timeout) {
28 super("Waiting thread");
29 _semaphore = semaphore;
30 _timeout = timeout;
31 _hasFinished = false;
32 }
33
34 @Override
35 public void run() {
36 _semaphore.waitForEvent(_timeout);
37 _hasFinished = true;
38 }
39
40
41
42
43 public boolean hasFinished() {
44 return _hasFinished;
45 }
46
47 }
48
49 DNSStatefulObjectSemaphore _semaphore;
50
51 @Before
52 public void setup() {
53 _semaphore = new DNSStatefulObjectSemaphore("test");
54 }
55
56 @After
57 public void teardown() {
58 _semaphore = null;
59 }
60
61 @Test
62 public void testWaitAndSignal() throws InterruptedException {
63 WaitingThread thread = new WaitingThread(_semaphore, Long.MAX_VALUE);
64 thread.start();
65 Thread.sleep(1);
66 assertFalse("The thread should be waiting.", thread.hasFinished());
67 _semaphore.signalEvent();
68 Thread.sleep(1);
69 assertTrue("The thread should have finished.", thread.hasFinished());
70 }
71
72 @Test
73 public void testWaitAndTimeout() throws InterruptedException {
74 WaitingThread thread = new WaitingThread(_semaphore, 100);
75 thread.start();
76 Thread.sleep(1);
77 assertFalse("The thread should be waiting.", thread.hasFinished());
78 Thread.sleep(150);
79 assertTrue("The thread should have finished.", thread.hasFinished());
80 }
81
82 }