View Javadoc

1   // Copyright (C) 2005 - 2008 Philip Aston
2   // All rights reserved.
3   //
4   // This file is part of The Grinder software distribution. Refer to
5   // the file LICENSE which is part of The Grinder distribution for
6   // licensing details. The Grinder distribution is available on the
7   // Internet at http://grinder.sourceforge.net/
8   //
9   // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
10  // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
11  // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
12  // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
13  // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
14  // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
15  // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
16  // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
17  // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
18  // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
19  // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
20  // OF THE POSSIBILITY OF SUCH DAMAGE.
21  
22  package net.grinder.util.thread;
23  
24  import java.util.Timer;
25  import java.util.TimerTask;
26  
27  import junit.framework.TestCase;
28  
29  
30  /**
31   * Unit tests for {@link BooleanCondition}.
32   *
33   * @author Philip Aston
34   */
35  public class TestBooleanCondition extends TestCase {
36  
37    public void testWithSingleThread() throws Exception {
38      final BooleanCondition booleanCondition = new BooleanCondition();
39  
40      // State is initially false, so we shouldn't block here.
41      assertFalse(booleanCondition.await(false));
42      assertFalse(booleanCondition.await(false));
43      assertFalse(booleanCondition.get());
44  
45      booleanCondition.set(true);
46      assertTrue(booleanCondition.get());
47      assertTrue(booleanCondition.await(true));
48  
49      booleanCondition.set(false);
50      assertFalse(booleanCondition.get());
51      assertFalse(booleanCondition.await(false));
52    }
53  
54    public void testWithMultipleThreads() throws Exception {
55      final Timer timer = new Timer();
56  
57      final BooleanCondition booleanCondition = new BooleanCondition();
58  
59      final TimerTask setTrueTask = new TimerTask() {
60        public void run() { booleanCondition.set(true); }
61      };
62  
63      timer.schedule(setTrueTask, 0, 1);
64  
65      assertTrue(booleanCondition.await(true));
66      assertTrue(booleanCondition.await(true));
67      assertTrue(booleanCondition.get());
68  
69      setTrueTask.cancel();
70  
71      final TimerTask wakeUpAllWaitersTask = new TimerTask() {
72        public void run() { booleanCondition.wakeUpAllWaiters(); }
73      };
74  
75      timer.schedule(wakeUpAllWaitersTask, 0, 1);
76  
77      // Will return true, because state is false but interrupted.
78      assertTrue(booleanCondition.await(false));
79  
80      // State should still be true.
81      assertTrue(booleanCondition.await(true));
82  
83      wakeUpAllWaitersTask.cancel();
84  
85      timer.cancel();
86    }
87  }