File: sleep.java

package info (click to toggle)
mauve 20080616-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 26,856 kB
  • ctags: 21,952
  • sloc: java: 234,107; sh: 2,834; xml: 208; makefile: 59
file content (447 lines) | stat: -rw-r--r-- 10,635 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
// Tags: JDK1.0

// Copyright (C) 2004 Free Software Foundation, Inc.
// Written by Mark Wielaard (mark@klomp.org)

// This file is part of Mauve.

// Mauve is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2, or (at your option)
// any later version.

// Mauve is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Mauve; see the file COPYING.  If not, write to
// the Free Software Foundation, 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA.

package gnu.testlet.java.lang.Thread;

import gnu.testlet.Testlet;
import gnu.testlet.TestHarness;

public class sleep implements Testlet, Runnable
{
  private TestHarness harness;

  private Thread thread;
  private boolean helper_started;
  private boolean helper_done;

  private final static long SLEEP_TIME = 5 * 1000; // 5 seconds

  // Time the helper thread should sleep before interrupting the main
  // thread Or zero for immediate interruption (won't use
  // synchronization either in that case).
  private long helper_sleep = 0;

  // Helper method that runs from another thread.
  // Sleeps a bit and then interrupts the main thread.
  public void run()
  {
    try
      {
	if (helper_sleep == 0)
	  {
	    thread.interrupt();
	    helper_done = true;
	    return;
	  }
	
	// Make sure main thread know we are about to sleep.
	// (It should also go to sleep)
	synchronized(this)
	  {
	    helper_started = true;
	    this.notify();
	  }

	Thread.sleep(helper_sleep);
	thread.interrupt();

	// Main thread should still have the lock on this
	synchronized(this)
	  {
	    helper_done = true;
	  }
      }
    catch (InterruptedException ie)
      {
	harness.debug("Interrupted in helper thread");
	harness.check(false);
      }
  }

  public void test (TestHarness h)
  {
    harness = h;
    Thread helper = new Thread(this);

    harness.checkPoint("Interrupted sleep");

    // Get a lock on this to coordinate with the runner thread.
    // We should not loose it while sleeping.
    synchronized(this)
      {
	helper_done = false;
	helper_sleep = SLEEP_TIME / 2;
	thread = Thread.currentThread();
	long past = System.currentTimeMillis();
	
	helper.start();
	
	// Wait for the helper to start (and sleep immediately).
	try
	  {
	    while (!helper_started)
	      this.wait();
	  }
	catch (InterruptedException ie)
	  {
	    harness.debug("Interrupted during helper start");
	    harness.check(false);
	  }
	
	// Go to sleep.
	// Helper thread sleeps less time and should interrupt us.
	boolean interrupted_exception = false;
	try
	  {
	    Thread.sleep(SLEEP_TIME);
	  }
	catch (InterruptedException ie)
	  {
	    interrupted_exception = true;
	  }
	harness.check(interrupted_exception);
	
	// About half the time should have been spent sleeping.
	long present = System.currentTimeMillis();
	long diff = present - past;
	harness.debug("diff: " + diff);
	harness.check(diff >= SLEEP_TIME / 2);
	harness.check(diff < SLEEP_TIME);

	// Even though we are interrupted,
	// the thread interrupted flag should be cleared.
	harness.check(!Thread.interrupted());
	
	// We are still holding the lock so the helper_thread
	// cannot be done yet.
	harness.check(!helper_done);
      }
    
    // Now wait for the helper thead to finish
    try
      {
	helper.join();
      }
    catch(InterruptedException ie)
      {
	harness.debug("Interruped during joining the helper thread");
	harness.check(false);
      }
    harness.check(helper_done);
    
    // Invalid argument checks.
    harness.checkPoint("Invalid argument");
    invalid(Long.MIN_VALUE);
    invalid(-1);

    invalid(Long.MIN_VALUE, Integer.MIN_VALUE);
    invalid(Long.MIN_VALUE, -1);
    invalid(Long.MIN_VALUE, 0);
    invalid(Long.MIN_VALUE, 1);
    invalid(Long.MIN_VALUE, 999999);
    invalid(Long.MIN_VALUE, 1000000);
    invalid(Long.MIN_VALUE, Integer.MAX_VALUE);

    invalid(-1, Integer.MIN_VALUE);
    invalid(-1, -1);
    invalid(-1, 0);
    invalid(-1, 1);
    invalid(-1, 999999);
    invalid(-1, 1000000);
    invalid(-1, Integer.MAX_VALUE);

    invalid(0, Integer.MIN_VALUE);
    invalid(0, -1);
    invalid(0, 1000000);
    invalid(0, Integer.MAX_VALUE);

    invalid(1, Integer.MIN_VALUE);
    invalid(1, -1);
    invalid(1, 1000000);
    invalid(1, Integer.MAX_VALUE);

    invalid(Long.MAX_VALUE, Integer.MIN_VALUE);
    invalid(Long.MAX_VALUE, -1);
    invalid(Long.MAX_VALUE, 1000000);
    invalid(Long.MAX_VALUE, Integer.MAX_VALUE);

    // (Large) valid argument checks
    valid(Integer.MAX_VALUE);
    valid(Long.MAX_VALUE);
    valid(Integer.MAX_VALUE, 0);
    valid(Long.MAX_VALUE, 0);
    valid(Integer.MAX_VALUE, 1);
    valid(Long.MAX_VALUE, 1);
    valid(Integer.MAX_VALUE, 999999);
    valid(Long.MAX_VALUE, 999999);

    // (Near) zero argument checks.
    harness.checkPoint("(Near) zero sleep");
    long past = System.currentTimeMillis();
    nearZero(0);
    nearZero(1);
    nearZero(0, 0);
    nearZero(0, 1);
    nearZero(0, 999999);
    nearZero(1, 0);
    nearZero(1, 1);
    nearZero(1, 999999);

    // The thread should have slept at least 5 miliseconds.
    // But certainly not more than 500 miliseconds.
    long present = System.currentTimeMillis();
    long diff = present - past;
    harness.debug("diff: " + diff);
    harness.check(diff > 5);
    harness.check(diff < 500);


    // A thread in interrupted state that goes to sleep gets
    // InterruptedException.
    harness.checkPoint("Interrupted state sleep");
    past = System.currentTimeMillis();
    interruptedSleep(0);
    interruptedSleep(1);
    interruptedSleep(5000);
    interruptedSleep(0, 0);
    interruptedSleep(1, 0);
    interruptedSleep(0, 1);
    interruptedSleep(1, 1);
    interruptedSleep(5000, 0);
    interruptedSleep(5000, 5000);

    // The thread should not actually have slept (much) since it was always
    // immediately waken up by the InterrupedException.
    present = System.currentTimeMillis();
    harness.check(present - past < 5000);
  }

  private void invalid(long milli)
  {
    boolean illegal_argument = false;
    try
      {
	Thread.sleep(milli);
      }
    catch (IllegalArgumentException iae)
      {
	illegal_argument = true;
      }
    catch(InterruptedException ie)
      {
	harness.debug("InterruptedException in invalid(" + milli + ")");
	harness.check(false);
      }
    harness.check(illegal_argument);
  }

  private void invalid(long milli, int nano)
  {
    boolean illegal_argument = false;
    try
      {
	Thread.sleep(milli, nano);
      }
    catch (IllegalArgumentException iae)
      {
	illegal_argument = true;
      }
    catch(InterruptedException ie)
      {
	harness.debug("InterruptedException in invalid("
		      + milli + ", " + nano + ")");
	harness.check(false);
      }
    harness.check(illegal_argument);

  }

  private void valid(long milli)
  {
    harness.checkPoint("valid long:" + milli);
    Thread helper = new Thread(this);
    helper_started = false;
    helper_done = false;
    helper_sleep = 1000;
    thread = Thread.currentThread();

    // Wait for the helper to start (and sleep immediately).
    helper.start();
    synchronized(this)
      {
	try
	  {
	    while (!helper_started)
	      this.wait();
	  }
	catch (InterruptedException ie)
	  {
	    harness.debug("Interrupted during helper start");
	    harness.check(false);
	  }
      }
    
    boolean interrupted_exception = false;
    try
      {
	Thread.sleep(milli);
      }
    catch (InterruptedException ie)
      {
	interrupted_exception = true;
      }
    harness.check(interrupted_exception);

    try
      {
	helper.join();
      }
    catch(InterruptedException ie)
      {
	harness.debug("Interruped during joining the helper thread");
	harness.check(false);
      }
    harness.check(helper_done);
  }

  private void valid(long milli, int nano)
  {
    harness.checkPoint("valid long " + milli + " int " + nano);
    Thread helper = new Thread(this);
    helper_started = false;
    helper_done = false;
    helper_sleep = 1000;
    thread = Thread.currentThread();
    
    // Wait for the helper to start (and sleep immediately).
    helper.start();
    synchronized(this)
      {
	try
	  {
	    while (!helper_started)
	      this.wait();
	  }
	catch (InterruptedException ie)
	  {
	    harness.debug("Interrupted during helper start");
	    harness.check(false);
	  }
      }
    
    boolean interrupted_exception = false;
    try
      {
	Thread.sleep(milli, nano);
      }
    catch (InterruptedException ie)
      {
	interrupted_exception = true;
      }
    catch (Exception x)
      {
        harness.debug(x);
        try
          {
            // wait for the interrupt from the helper
            Thread.sleep(1000);
          }
        catch (InterruptedException _)
          {
          }
      }
    harness.check(interrupted_exception);

    try
      {
	helper.join();
      }
    catch(InterruptedException ie)
      {
	harness.debug("Interrupted during joining the helper thread");
	harness.check(false);
      }
    harness.check(helper_done);
  }

  private void nearZero(long milli)
  {
    try
      {
	Thread.sleep(milli);
	harness.check(true);
      }
    catch(InterruptedException ie)
      {
	harness.debug("InterruptedException in nearZero(" + milli + ")");
	harness.check(false);
      }
  }

  private void nearZero(long milli, int nano)
  {
    try
      {
	Thread.sleep(milli, nano);
	harness.check(true);
      }
    catch(InterruptedException ie)
      {
	harness.debug("InterruptedException in nearZero("
		      + milli + ", " + nano + ")");
	harness.check(false);
      }
  }

  private void interruptedSleep(long milli)
  {
    boolean interrupted_exception = false;
    Thread.currentThread().interrupt();
    try
      {
	Thread.sleep(milli);
      }
    catch(InterruptedException ie)
      {
	interrupted_exception = true;
      }
    harness.check(interrupted_exception);
    harness.check(!Thread.interrupted());
  }

  private void interruptedSleep(long milli, int nano)
  {
    boolean interrupted_exception = false;
    Thread.currentThread().interrupt();
    try
      {
	Thread.sleep(milli, nano);
      }
    catch(InterruptedException ie)
      {
	interrupted_exception = true;
      }
    harness.check(interrupted_exception);
    harness.check(!Thread.interrupted());
  }
}