/
nonblocking-await.t
291 lines (271 loc) · 8.01 KB
/
nonblocking-await.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
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
use v6.d;
use Test;
plan 28;
# Limit scheduler to just 4 real threads, so we'll clearly be needing the
# non-blocking await support for these to pass.
PROCESS::<$SCHEDULER> := ThreadPoolScheduler.new(max_threads => 4);
{
sub conc-fib($n) {
start {
$n <= 1
?? 1
!! await(conc-fib($n - 2)) + await(conc-fib($n - 1))
}
}
is await(conc-fib(15)), 987, 'Recursion creating a deep await-tree completes correctly';
}
{
sub conc-fib($n) {
start {
$n <= 1
?? die "oopsy"
!! await(conc-fib($n - 2)) + await(conc-fib($n - 1))
}
}
#?rakudo.jvm skip 'hangs on JVM'
throws-like { await(conc-fib(15)) }, Exception,
message => "oopsy",
'Deep Promise tree conveys exception up to the top';
}
{
my @proms = (1..100).map: {
start {
await Promise.in(0.01);
await Promise.in(0.05);
$_
}
};
is ([+] await @proms), 5050, 'Hundreds of time-based Promises awaited completes correctly';
}
{
my $p = Promise.new;
my @proms = (1..100).map: {
start {
await Promise.in(0.1), $p;
$_
}
};
sleep 0.15;
ok [==](flat Planned, @proms>>.status), 'await of multiple Promises suspends until all ready';
$p.keep;
is ([+] await @proms), 5050, 'Hundred of await on time/manual Promise completes corectly';
}
{
my $p = Promise.new;
my @proms = (1..100).map: {
start {
await Promise.in(0.1), $p;
$_
}
};
sleep 0.15;
ok [==](flat Planned, @proms>>.status), 'await of multiple Promises suspends until all ready';
$p.break('bust');
#?rakudo.jvm skip 'hangs on JVM (sometimes)'
throws-like { await @proms }, Exception, message => 'bust',
'Multiple await also conveys errors correctly';
}
{
my @proms = (1..100).map: -> $i {
start {
await supply { whenever Supply.interval(0.001 * $i) { emit $i; done } }
}
};
#?rakudo.jvm skip 'hangs on JVM'
is ([+] await @proms), 5050, 'Hundred of outstanding awaits on supplies works';
}
{
my @proms = (1..100).map: -> $i {
start {
await supply {
whenever Supply.interval(0.001 * $i) {
$i > 50
?? die 'strewth'
!! done
}
}
}
};
#?rakudo.jvm skip 'hangs on JVM'
throws-like { await @proms }, Exception, message => 'strewth',
'Hundred of outstanding awaits on supplies that die works';
}
{
my $c = Channel.new;
my @proms = (1..100).map: -> $i {
start {
await($c) + await($c)
}
};
for 1..200 {
$c.send($_);
}
#?rakudo.jvm skip 'hangs on JVM'
is ([+] await @proms), 20100, 'Hundred of outstanding awaits on channels works';
}
{
my $c = Channel.new;
my @proms = (1..100).map: -> $i {
start {
await($c) + await($c)
}
};
for 1..100 {
$c.send($_);
}
$c.close;
#?rakudo.jvm skip 'hangs on JVM'
throws-like { await @proms }, X::Channel::ReceiveOnClosed,
'Hundred of outstanding awaits on channels that gets closed works';
}
#?rakudo.jvm skip 'hangs on JVM'
{
sub death-bar() {
die "golly!";
}
sub foo() {
start { death-bar() }
}
{
sub waiting-for-it() {
await foo()
}
waiting-for-it;
CATCH {
default {
ok .does(X::Await::Died),
'Exception from awaiting broken Promise does X::Await::Died';
like .gist, /'golly!'/,
'Exception contains original exception message';
like .gist, /'death-bar'/,
'Exception contains stack location where Promise code died';
like .gist, /'waiting-for-it'/,
'Exception contains stack location where we awaited';
}
}
}
{
my $p = Promise.new;
$p.keep(42);
sub waiting-for-them() {
await $p, foo();
}
waiting-for-them;
CATCH {
default {
ok .does(X::Await::Died),
'Exception from awaiting many things, where one dies, does X::Await::Died';
like .gist, /'golly!'/,
'Exception contains original exception message';
like .gist, /'death-bar'/,
'Exception contains stack location where Promise code died';
like .gist, /'waiting-for-them'/,
'Exception contains stack location where we awaited';
}
}
}
}
# Should not attempt non-blocking await when a lock is held.
{
# Try to create good chance of resume on a different thread.
my $l = Lock.new;
my $p1 = start {
sleep 0.5;
}
my $p2 = start {
$l.protect: {
sleep 0.1;
await $p1;
}
}
my @ps = start { } xx 20;
#?rakudo.jvm todo 'UnwindException'
lives-ok { await $p1, $p2, @ps },
'No error due to trying to do non-blocking await when lock held (protect)';
}
{
# Same test as above except without .protect
my $l = Lock.new;
my $p1 = start {
sleep 0.5;
}
my $p2 = start {
$l.lock();
sleep 0.1;
await $p1;
$l.unlock();
}
my @ps = start { } xx 20;
#?rakudo.jvm todo 'NullPointerException'
lives-ok { await $p1, $p2, @ps },
'No error due to trying to do non-blocking await when lock held (lock/unlock)';
}
# RT #130692
#?rakudo.jvm skip 'UnwindException'
{
my $kill = Promise.new;
my $started = Promise.new;
my $s-address = '0.0.0.0';
my $c-address = '127.0.0.1';
my $port = 4893;
my $ok = start react {
whenever IO::Socket::Async.listen($s-address, $port) -> $conn {
whenever $conn.Supply(:bin) -> $buf {
await $conn.write: $buf;
$conn.close;
}
}
whenever $kill { done }
$started.keep;
}
await $started;
my @responses;
for ^20 {
react {
whenever IO::Socket::Async.connect($c-address, $port) -> $client {
await $client.write('is this thing on?'.encode('ascii'));
whenever $client.Supply(:bin) {
push @responses, .decode('ascii');
$client.close;
}
}
}
}
$kill.keep;
lives-ok { await $ok }, 'Server survived';
ok @responses == 20, 'Got 20 responses from async socket server that does non-blocking await';
is @responses[0], 'is this thing on?', 'First response correct';
ok [eq](@responses), 'Rest of responses also correct';
}
# RT #132091
#?rakudo.jvm skip 'NullPointerException'
{
my @foo = do {
await start { do for ^2 { my uint64 @ = 9, 9; }.Slip },
start { do for ^2 { my uint64 @ = 1, 2; }.Slip };
}
is @foo.elems, 4, "slips awaited over get flattened out";
}
#?rakudo.jvm skip 'UnwindException'
{
await start {
my @foo = do {
await start { do for ^2 { my uint64 @ = 9, 9; }.Slip },
start { do for ^2 { my uint64 @ = 1, 2; }.Slip };
}
is @foo.elems, 4, "slips awaited over get flattened out";
}
}
#?rakudo.jvm skip 'atomicint NYI'
{ # https://github.com/rakudo/rakudo/issues/1323
# Await must be called in sink context here to trigger the covered bug.
# Gymnastics with atomic ints are just to reduce test runtime; the point
# of the test is that await awaits in this case for all Promises to
# complete instead of just returning immediately.
# (same test exists in 6.c tests on purpose; to cover Rakudo impl)
my atomicint $x;
sub p { start { sleep .3; $x⚛++ } }
await (((p(), (p(), (p(),))), (p(), p(), p())), (p(), p(), p()));
is-deeply $x, 9, '&await awaits in sink context, with nested iterables';
}
# vim: ft=perl6 sw=4 ts=4 sts=4 expandtab