View Javadoc

1   // Copyright (C) 2001 - 2011 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;
23  
24  import static org.junit.Assert.assertTrue;
25  import static org.junit.Assert.fail;
26  import static org.mockito.Matchers.contains;
27  import static org.mockito.Matchers.isA;
28  import static org.mockito.Mockito.mock;
29  import static org.mockito.Mockito.verify;
30  import static org.mockito.Mockito.verifyNoMoreInteractions;
31  import net.grinder.testutility.Time;
32  
33  import org.junit.Test;
34  import org.slf4j.Logger;
35  
36  
37  /**
38   * Unit tests for {@link SleeperImplementation}.
39   *
40   * @author Philip Aston
41   */
42  public class TestSleeper {
43  
44    private final TimeAuthority m_timeAuthority = new StandardTimeAuthority();
45  
46    @Test public void testConstruction() throws Exception {
47      try {
48        new SleeperImplementation(m_timeAuthority, null, -1, 1);
49        fail("IllegalArgumentException expected");
50      }
51      catch (IllegalArgumentException e) {
52      }
53  
54      try {
55        new SleeperImplementation(m_timeAuthority, null, 1, -1);
56        fail("IllegalArgumentException expected");
57      }
58      catch (IllegalArgumentException e) {
59      }
60  
61      new SleeperImplementation(m_timeAuthority, null, 1, 1);
62    }
63  
64    @Test public void testSleepNormal() throws Exception {
65      // Warm up Hot Spot.
66      final Sleeper sleep0 =
67        new SleeperImplementation(m_timeAuthority, null, 1, 0);
68  
69      final long t1 = sleep0.getTimeInMilliseconds();
70  
71      Time time0 = new Time(0, 1000) {
72          public void doIt() throws Exception { sleep0.sleepNormal(10); }
73        };
74  
75      for (int i=0; i<10; i++) { time0.run(); }
76  
77      // Now do the tests.
78      final Sleeper sleep1 =
79        new SleeperImplementation(m_timeAuthority, null, 1, 0);
80  
81      assertTrue(
82        new Time(50, 70) {
83          public void doIt() throws Exception  { sleep1.sleepNormal(50); }
84        }.run());
85  
86      assertTrue(
87        new Time(0, 10) {
88          public void doIt() throws Exception  { sleep1.sleepNormal(0); }
89        }.run());
90  
91      final Sleeper sleep2 =
92        new SleeperImplementation(m_timeAuthority, null, 2, 0);
93  
94      assertTrue(
95        new Time(100, 120) {
96          public void doIt() throws Exception  { sleep2.sleepNormal(50); }
97        }.run());
98  
99      final Sleeper sleep3 =
100       new SleeperImplementation(m_timeAuthority, null, 1, 0.1);
101 
102     final Time time = new Time(40, 60) {
103         public void doIt() throws Exception { sleep3.sleepNormal(50);}
104       };
105 
106     int in = 0;
107     for (int i=0; i<30; i++) {
108       if (time.run()) {
109         ++in;
110       }
111     }
112 
113     assertTrue(in > 20);
114 
115     final Sleeper sleep4 =
116       new SleeperImplementation(m_timeAuthority, null, 0, 0);
117 
118     assertTrue(
119       new Time(0, 10) {
120         public void doIt() throws Exception  { sleep4.sleepNormal(50); }
121       }.run());
122 
123     assertTrue(sleep0.getTimeInMilliseconds() > t1);
124   }
125 
126   @Test public void testSleepFlat() throws Exception {
127     // Warm up Hot Spot.
128     final Sleeper sleep0 =
129       new SleeperImplementation(m_timeAuthority, null, 1, 0);
130 
131     Time time0 = new Time(0, 1000) {
132         public void doIt() throws Exception { sleep0.sleepFlat(10); }
133       };
134 
135     for (int i=0; i<10; i++) { time0.run(); }
136 
137     // Now do the tests.
138     final Sleeper sleep1 =
139       new SleeperImplementation(m_timeAuthority, null, 1, 0);
140 
141     assertTrue(
142       new Time(0, 70) {
143         public void doIt() throws Exception  { sleep1.sleepFlat(50); }
144       }.run());
145 
146     assertTrue(
147       new Time(0, 10) {
148         public void doIt() throws Exception  { sleep1.sleepFlat(0); }
149       }.run());
150 
151     final Sleeper sleep2 =
152       new SleeperImplementation(m_timeAuthority, null, 2, 0);
153 
154     assertTrue(
155       new Time(0, 120) {
156         public void doIt() throws Exception  { sleep2.sleepFlat(50); }
157       }.run());
158 
159     final Logger logger = mock(Logger.class);
160 
161     final Sleeper sleep3 =
162       new SleeperImplementation(m_timeAuthority, logger, 1, 0);
163     sleep3.sleepFlat(10);
164 
165     verify(logger).info(contains("sleeping"), isA(Long.class));
166     verifyNoMoreInteractions(logger);
167   }
168 
169   @Test public void testShutdown() throws Exception {
170     final TakeFifty t1 = new TakeFifty();
171 
172     assertTrue(
173       new Time(500, 1000) {
174         public void doIt() throws Exception {
175           t1.start();
176           Thread.sleep(500);
177           t1.getSleeper().shutdown();
178           t1.join();
179         }
180       }.run());
181 
182     try {
183       t1.getSleeper().sleepFlat(10);
184       fail("Expected ShutdownException");
185     }
186     catch (SleeperImplementation.ShutdownException e) {
187     }
188   }
189 
190   @Test public void testShutdownAllCurrentSleepers() throws Exception {
191     final Thread t1 = new TakeFifty();
192     final Thread t2 = new TakeFifty();
193 
194     assertTrue(
195       new Time(500, 1000) {
196         public void doIt() throws Exception {
197           t1.start();
198           t2.start();
199           Thread.sleep(500);
200           SleeperImplementation.shutdownAllCurrentSleepers();
201           t1.join();
202           t2.join();
203         }
204       }.run());
205 
206     // No-op.
207     SleeperImplementation.shutdownAllCurrentSleepers();
208   }
209 
210   private final class TakeFifty extends Thread {
211     private final Sleeper m_sleeper;
212 
213     public TakeFifty() throws Sleeper.ShutdownException {
214       m_sleeper = new SleeperImplementation(m_timeAuthority, null, 1, 0);
215     }
216 
217     public final void run() {
218       try {
219         m_sleeper.sleepNormal(50000);
220       }
221       catch (SleeperImplementation.ShutdownException e) {
222       }
223     }
224 
225     public final Sleeper getSleeper() {
226       return m_sleeper;
227     }
228   }
229 }