1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
39
40
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
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
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
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
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
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 }