/
basic.t
153 lines (138 loc) · 4.57 KB
/
basic.t
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
use v6;
use Test;
plan 34;
# real scheduling here
my $name = $*SCHEDULER.^name;
ok $*SCHEDULER ~~ Scheduler, "$name does Scheduler role";
{
my $loads = $*SCHEDULER.loads;
ok $loads == 0, "$name returns a value before cuing";
}
#?rakudo skip "waiting for new '.loads' semantics RT #124774"
{
my $x = False;
my $c = $*SCHEDULER.cue({
pass "Cued code on $name ran";
$x = True;
});
isa-ok $c, Cancellation;
1 while $*SCHEDULER.loads;
ok $x, "Code was cued to $name by default";
LEAVE $c.cancel;
}
#?rakudo skip "waiting for new '.loads' semantics RT #124775"
{
my $message;
my $c = $*SCHEDULER.uncaught_handler = sub ($exception) {
$message = $exception.message;
};
$*SCHEDULER.cue({ die "oh noes" });
isa-ok $c, Cancellation;
1 while $*SCHEDULER.loads;
is $message, "oh noes", "$name setting uncaught_handler works";
LEAVE $c.cancel;
}
#?rakudo skip "waiting for new '.loads' semantics RT #124776"
{
my $tracker;
my $c = $*SCHEDULER.cue(
{ $tracker = 'cued,'; die "oops" },
:catch( -> $ex {
is $ex.message, "oops", "$name passed correct exception to handler";
$tracker ~= 'caught';
})
);
isa-ok $c, Cancellation;
1 while $*SCHEDULER.loads;
is $tracker, "cued,caught", "Code run on $name, then handler";
LEAVE $c.cancel;
}
#?rakudo skip "waiting for new '.loads' semantics RT #124777"
{
my $tracker;
my $c = $*SCHEDULER.cue(
{ $tracker = 'cued,' },
:catch( -> $ex { $tracker ~= 'caught' })
);
isa-ok $c, Cancellation;
1 while $*SCHEDULER.loads;
is $tracker, "cued,", "Catch handler on $name not run if no error";
LEAVE $c.cancel;
}
{
dies-ok { $*SCHEDULER.cue({ ... }, :at(now + 2), :in(1)) },
"$name cannot combine :in and :at";
dies-ok { $*SCHEDULER.cue({ ... }, :every(0.1), :at(now + 2), :in(1)) },
"$name cannot combine :every with :in and :at";
dies-ok { $*SCHEDULER.cue({ ... }, :at(now + 2), :in(1)), :catch({...}) },
"$name cannot combine :catch with :in and :at";
dies-ok { $*SCHEDULER.cue({ ... }, :every(0.1), :at(now + 2), :in(1)), :catch({...}) },
"$name cannot combine :every/:catch with :in and :at";
dies-ok { $*SCHEDULER.cue({ ... }, :every(0.1), :times(10), :stop({...})) },
"$name cannot combine :every, :times and :stop";
}
# fake scheduling from here on out
$*SCHEDULER = CurrentThreadScheduler.new;
$name = $*SCHEDULER.^name;
ok $*SCHEDULER ~~ Scheduler, "{$*SCHEDULER.^name} does Scheduler role";
{
my $x = False;
my $c = $*SCHEDULER.cue({
pass "Cued code on $name ran";
$x = True;
});
ok $c.can("cancel"), 'can we cancel (1)';
1 while $*SCHEDULER.loads;
ok $x, "Code was cued to $name by default";
LEAVE $c.cancel;
}
{
my $message;
$*SCHEDULER.uncaught_handler = sub ($exception) {
$message = $exception.message;
};
my $c = $*SCHEDULER.cue({ die "oh noes" });
ok $c.can("cancel"), 'can we cancel (2)';
1 while $*SCHEDULER.loads;
is $message, "oh noes", "$name setting uncaught_handler works";
LEAVE $c.?cancel;
}
{
my $tracker;
my $c = $*SCHEDULER.cue(
{ $tracker = 'cued,'; die "oops" },
:catch( -> $ex {
is $ex.message, "oops", "$name passed correct exception to handler";
$tracker ~= 'caught';
})
);
ok $c.can("cancel"), 'can we cancel (3)';
1 while $*SCHEDULER.loads;
is $tracker, "cued,caught", "Code run on $name, then handler";
LEAVE $c.?cancel;
}
{
my $tracker;
my $c = $*SCHEDULER.cue(
{ $tracker = 'cued,' },
:catch( -> $ex { $tracker ~= 'caught' })
);
ok $c.can("cancel"), 'can we cancel (4)';
1 while $*SCHEDULER.loads;
is $tracker, "cued,", "Catch handler on $name not run if no error";
LEAVE $c.cancel;
}
{
dies-ok { $*SCHEDULER.cue({ ... }, :every(1)) },
"$name cannot specify :every in CurrentThreadScheduler";
dies-ok { $*SCHEDULER.cue({ ... }, :at(now + 2), :in(1)) },
"$name cannot combine :in and :at";
dies-ok { $*SCHEDULER.cue({ ... }, :every(0.1), :at(now + 2), :in(1)) },
"$name cannot combine :every with :in and :at";
dies-ok { $*SCHEDULER.cue({ ... }, :at(now + 2), :in(1)), :catch({...}) },
"$name cannot combine :catch with :in and :at";
dies-ok { $*SCHEDULER.cue({ ... }, :every(0.1), :at(now + 2), :in(1)), :catch({...}) },
"$name cannot combine :every/:catch with :in and :at";
dies-ok { $*SCHEDULER.cue({ ... }, :every(0.1), :times(10), :stop({...})) },
"$name cannot combine :every, :times and :stop";
}