@@ -3,38 +3,77 @@ use Test;
3
3
4
4
# L<S29/Context/"=item sleep">
5
5
6
- plan 12 ;
6
+ plan 21 ;
7
+
8
+ my $ seconds = 3 ;
9
+ my $ nil is default (Nil );
10
+ my $ b ;
7
11
8
12
{
9
- diag " Sleeping for 3s " ;
10
- my $ start = time ;
11
- my $ sleep_says = sleep 3 ;
12
- my $ diff = time - $ start ;
13
+ diag " sleep() for $ seconds seconds " ;
14
+ my $ start = now ;
15
+ $ nil = sleep $ seconds ;
16
+ my $ diff = now - $ start ;
13
17
14
- # ?pugs todo
15
- ok ( $ sleep_says >= 2 , ' Sleep says it slept at least 2 seconds ' );
16
- ok ( $ sleep_says <= 10 , ' ... and no more than 10 ' ) ;
18
+ # ?pugs todo "NYI"
19
+ # ?niecza todo "NYI"
20
+ ok $ nil === Nil , ' sleep() always returns Nil ' ;
17
21
18
- ok ( $ diff >= 2 , ' We actually slept at least 2 seconds' );
19
- ok ( $ diff <= 10 , ' ... and no more than 10' );
20
- } # 4
22
+ ok $ diff >= $ seconds - 1 , ' we actually slept at some seconds' ;
23
+ ok $ diff <= $ seconds + 5 , ' ... but not too long' ;
24
+
25
+ # ?pugs 2 skip "NYI"
26
+ # ?niecza 2 skip "NYI"
27
+ $ nil = 1 ;
28
+ lives_ok { $ nil = sleep (-1 ) }, " silently ignores negative times" ;
29
+ ok $ nil === Nil , ' sleep() always returns Nil' ;
30
+ } # 5
31
+
32
+ # ?pugs skip "NYI"
33
+ # ?niecza skip "NYI"
34
+ {
35
+ diag " sleep-timer() for $ seconds seconds" ;
36
+ my $ then = now;
37
+ my $ left = sleep-timer $ seconds ;
38
+ my $ now = now;
39
+
40
+ isa_ok $ left , Duration , ' did we get a Duration back (1)' ;
41
+ ok $ now - $ then + $ seconds >= $ left , ' does Duration returned make sense' ;
21
42
22
- # ?pugs skip "not yet implemented"
23
- # ?niecza todo "not yet implemented"
43
+ $ left = sleep-timer -1 ;
44
+ isa_ok $ left , Duration , ' did we get a Duration back (2)' ;
45
+ is $ left , 0 , ' no time left to wait' ;
46
+
47
+ $ left = sleep-timer 0 ;
48
+ isa_ok $ left , Duration , ' did we get a Duration back (3)' ;
49
+ is $ left , 0 , ' no time left to wait either' ;
50
+ } # 6
51
+
52
+ # ?pugs skip "NYI"
53
+ # ?niecza skip "NYI"
24
54
{
25
- is interval( 1.5 ), 0 , " first call doesn't wait" ;
26
- my $ start = time . Num ;
27
- ok 0 <= interval(2.5 ) < 1.5 , " first sleep" ;
28
- ok 0 <= interval(5 ) < 2.5 , " second sleep" ;
29
- ok 0 <= interval(0 ) < 5 , " third sleep" ;
30
- is interval(3 ), 0 , " fourth sleep" ;
31
- ok 0 <= interval(0 ) < 3 , " fifth sleep" ;
32
- ok 12 <= time . Num - $ start <= 15 , " some overall time" ;
33
- } # 7
34
-
35
- # ?pugs todo "not yet implemented"
55
+ diag " sleep-till() for $ seconds seconds" ;
56
+ my $ then = now;
57
+ my $ slept = sleep-till $ then + $ seconds ;
58
+ my $ now = now;
59
+
60
+ isa_ok $ slept , Bool , ' did we get a Bool back' ;
61
+ ok $ slept , ' did we actually wait' ;
62
+ ok $ now - $ then + $ seconds >= 0 , ' does elapsed time make sense' ;
63
+
64
+ nok sleep-till($ then + $ seconds ), ' should not actually sleep again' ;
65
+ } # 4
66
+
67
+ # ?pugs todo "NYI"
68
+ # ?niecza todo "NYI"
36
69
{
37
- dies_ok( { sleep (-1 ) }, " cannot go back in time" );
38
- } # 1
70
+ diag " checking infinite waiting times" ;
71
+ isa_ok eval(' $b={sleep(Inf)}' ), Block , ' sleep(Inf) compiles' ;
72
+ isa_ok eval(' $b={sleep(*)}' ), Block , ' sleep(*) compiles' ;
73
+ isa_ok eval(' $b={sleep-timer(Inf)}' ), Block , ' sleep-timer(Inf) compiles' ;
74
+ isa_ok eval(' $b={sleep-timer(*)}' ), Block , ' sleep-timer(*) compiles' ;
75
+ isa_ok eval(' $b={sleep-till(Inf)}' ), Block , ' sleep-till(Inf) compiles' ;
76
+ isa_ok eval(' $b={sleep-till(*)}' ), Block , ' sleep-till(*) compiles' ;
77
+ } # 6
39
78
40
79
# vim: ft=perl6
0 commit comments