-
Notifications
You must be signed in to change notification settings - Fork 135
/
syntax.t
666 lines (581 loc) · 16.6 KB
/
syntax.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
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
use v6;
use Test;
plan 81;
{
my $s = supply {
emit 42;
emit 'answer';
}
my @collected;
$s.tap({ @collected.push($_) });
is @collected.elems, 2, 'Got two values emitted by supply';
is @collected[0], 42, 'First value correct';
is @collected[1], 'answer', 'Second value correct';
my @collected-again;
$s.tap({ @collected-again.push($_) });
is @collected-again.elems, 2, 'Tapping the supply another time works';
}
{
my $oops = False;
my $s = supply {
emit 101;
done;
$oops = True;
emit 'oh noes';
}
my @collected;
$s.tap({ @collected.push($_) });
is @collected.elems, 1, 'Only the one emit ran, due to done';
is @collected[0], 101, 'Got correct value emitted';
nok $oops, 'Did not continue running code after done';
}
{
my $s = supply {
emit 'beans';
done;
}
my $emits-run = 0;
my $dones-run = 0;
my $quits-run = 0;
$s.tap({ $emits-run++ }, done => { $dones-run++ }, quit => { $quits-run++ });
is $emits-run, 1, 'Block with emit then done runs emit subscription once';
is $dones-run, 1, 'Block with emit then done runs done subscription once';
is $quits-run, 0, 'Block with emit then done never runs quit subscription';
}
{
my $s = supply {
emit 'peas';
}
my $emits-run = 0;
my $dones-run = 0;
my $quits-run = 0;
$s.tap({ $emits-run++ }, done => { $dones-run++ }, quit => { $quits-run++ });
is $emits-run, 1, 'Block with emit runs emit subscription once';
is $dones-run, 1, 'Block with emit runs done subscription once (automatically)';
is $quits-run, 0, 'Block with emit never runs quit subscription';
}
{
my $s = supply {
emit 'onions';
die 'poop';
}
my $emits-run = 0;
my $dones-run = 0;
my $quits-run = 0;
$s.tap({ $emits-run++ }, done => { $dones-run++ }, quit => { $quits-run++ });
is $emits-run, 1, 'Block with emit then die runs emit subscription once';
is $dones-run, 0, 'Block with emit then die never runs done subscription once';
is $quits-run, 1, 'Block with emit then die runs quit subscription once';
}
{
my $trigger = Supplier.new;
my $s = supply {
whenever $trigger -> $value {
emit $value * 2;
}
}
my @collected;
my $done = False;
my $quit = False;
$s.tap({ @collected.push($_) }, done => { $done = True }, quit => { $quit = True });
$trigger.emit(21);
is @collected.elems, 1, 'whenever loop ran and emitted first event into supply...';
is @collected[0], 42, '...and emitted correct first value';
$trigger.emit(50);
is @collected.elems, 2, 'whenever loop ran and emitted second event into supply...';
is @collected[1], 100, '...and emitted correct second value';
nok $done, 'done not run automatically when there is a whenever';
nok $quit, 'no unexpected quit when there is a whenever';
$trigger.done();
ok $done, 'done is run if the whenever gets done';
nok $quit, 'still no unexpected quit';
}
{
my $trigger = Supplier.new;
my $s = supply {
whenever $trigger -> $value {
emit $value.subst('fruit', 'bacon');
LAST {
emit "The end.";
}
}
}
my @collected;
$s.tap({ @collected.push($_) });
$trigger.emit('I ate fruit');
$trigger.emit('The fruit was so tasty');
$trigger.done();
is @collected.elems, 3, 'whenever loop produced three values';
is @collected[0], 'I ate bacon', 'First value from loop iteration correct';
is @collected[1], 'The bacon was so tasty', 'Second value from loop iteration correct';
is @collected[2], 'The end.', 'Value from LAST block correct';
}
{
my $trigger1 = Supplier.new;
my $trigger2 = Supplier.new;
my $s = supply {
whenever $trigger1 -> $value {
emit "a $value";
}
whenever $trigger2 -> $value {
emit "the $value";
}
}
my @collected;
$s.tap({ @collected.push($_) });
$trigger1.emit('bike');
$trigger1.emit('train');
$trigger2.emit('plane');
$trigger1.emit('car');
$trigger2.emit('tram');
$trigger1.emit('trebuchet');
is @collected, ['a bike', 'a train', 'the plane', 'a car', 'the tram', 'a trebuchet'],
'Multiple whenevers run concurrently';
}
{
my $closed1 = False;
my $trigger1 = Supplier.new;
my $closed2 = False;
my $trigger2 = Supplier.new;
my $s = supply {
whenever $trigger1.Supply.on-close({ $closed1 = True; }) { }
whenever $trigger2.Supply.on-close({ $closed2 = True; }) { }
done;
}
my $done = False;
$s.tap(done => { $done = True });
ok $done, 'supply block with two whenevers then done runs its done';
ok $closed1, 'first whenever closes its supply due to the done';
ok $closed2, 'second whenever closes its supply due to the done';
}
{
my $closed1 = False;
my $trigger1 = Supplier.new;
my $closed2 = False;
my $trigger2 = Supplier.new;
my $s = supply {
whenever $trigger1.Supply.on-close({ $closed1 = True; }) {
emit $_;
}
whenever $trigger2.Supply.on-close({ $closed2 = True; }) {
done;
}
}
my @collected;
my $done = False;
$s.tap({ @collected.push($_) }, done => { $done = True });
$trigger1.emit('tea');
$trigger1.emit('coffee');
$trigger2.emit('beer');
$trigger1.emit('cocoa');
is @collected, ['tea', 'coffee'], 'take-until style supply emitted correct values';
ok $done, 'take-until style supply is done after second supply emits';
ok $closed1, 'first supply tapped was closed';
ok $closed2, 'second supply tapped was closed';
}
{
my $trigger1 = Supplier.new;
my $trigger2 = Supplier.new;
my $s = supply {
whenever $trigger1 { }
whenever $trigger2 { }
}
my $done = False;
$s.tap(done => { $done = True });
nok $done, 'supply block with two whenevers starts out not done';
$trigger2.done;
nok $done, 'still not done after one whenever gets done';
$trigger1.done;
ok $done, 'but done after all whenevers get done';
}
{
my $trigger = Supplier.new;
my $s = supply {
whenever $trigger {
}
}
my $emits-run = 0;
my $dones-run = 0;
my $quits-run = 0;
$s.tap({ $emits-run++ }, done => { $dones-run++ }, quit => { $quits-run++ });
$trigger.quit('OMG FIRE!');
is $emits-run, 0, 'supply block with whenever that gets a quit emits no values';
is $dones-run, 0, 'supply block with whenever that gets a quit is not done';
is $quits-run, 1, 'supply block with whenever that gets a quit does quit';
}
{
my class OMGBears is Exception { }
my $trigger = Supplier.new;
my $s = supply {
whenever $trigger {
emit $_;
QUIT {
when OMGBears {
emit 'Run you fools!';
done;
}
}
}
}
my @collected;
my $dones-run = 0;
my $quits-run = 0;
$s.tap({ @collected.push($_); }, done => { $dones-run++ }, quit => { $quits-run++ });
$trigger.emit('Something is coming');
$trigger.emit('Something big');
$trigger.quit(OMGBears.new);
is @collected, ['Something is coming', 'Something big', 'Run you fools!'],
'emits up to, and emit in a QUIT block, all came out OK';
is $dones-run, 1, 'done inside of matched QUIT block worked';
is $quits-run, 0, 'did not produce a quit, since exception handled';
}
{
my $trigger = Supplier.new;
my $s = supply {
whenever $trigger {
QUIT {
default {
# Handle anything
}
}
}
}
my $dones-run = 0;
my $quits-run = 0;
$s.tap(done => { $dones-run++ }, quit => { $quits-run++ });
$trigger.quit(Exception.new);
is $dones-run, 1, 'supply with one whenever that quits but handles it will be done';
is $quits-run, 0, '...and the handled exception will not cause a quit';
}
{
my class UselessException is Exception { }
my $trigger = Supplier.new;
my $s = supply {
whenever $trigger {
QUIT {
when UselessException {
# we never reach here
}
}
}
}
my $dones-run = 0;
my $quits-run = 0;
$s.tap(done => { $dones-run++ }, quit => { $quits-run++ });
$trigger.quit(Exception.new);
is $quits-run, 1, 'supply with one whenever that quits, and QUIT does not match, will still quit';
is $dones-run, 0, '...and done will not be run';
}
# RT #125987
{
my $i = 0;
react {
whenever supply { emit 'x'; emit 'y'; } {
$i++
}
}
is $i, 2, 'react/whenever with supply that immediately emits values works';
}
# RT #128717
{
my $i = 0;
react whenever Supply.interval: 0.01 { done() if $_ == 3; $i++ }
is $i, 3, 'blockless react/whenever works';
}
{
my $trigger1 = Supplier.new;
my $trigger2 = Supplier.new;
my $p1 = Promise.new;
my $p2 = Promise.new;
my $p3 = Promise.new;
my $s = supply {
whenever $trigger1 -> $value {
$p1.keep(True);
await $p2;
emit "a $value";
}
whenever $trigger2 -> $value {
emit "the $value";
$p3.keep(True);
}
}
my @collected;
$s.tap({ @collected.push($_) });
start { $trigger1.emit('bear'); }
await $p1;
start { $trigger2.emit('wolf'); }
$p2.keep(True);
await $p3;
is @collected, ['a bear', 'the wolf'], 'Can only be in one whatever block at a time';
}
# RT #126089
throws-like 'emit 42', X::ControlFlow, illegal => 'emit';
throws-like 'done', X::ControlFlow, illegal => 'done';
# whenever with channel
{
my $c = Channel.new;
start {
$c.send($_) for 1..10;
$c.close();
}
my $total = 0;
react {
whenever $c {
$total += $_;
}
}
is $total, 55, 'can use channels with whenever';
}
# multiple whenevers with channels
{
my $c1 = Channel.new;
my $c2 = Channel.new;
my $p1 = Promise.new;
my $p2 = Promise.new;
start {
$c1.send(1);
await $p2;
$c1.send(3);
$c1.close();
}
start {
await $p1;
$c2.send(2);
$c2.close();
}
my $order = '';
react {
whenever $c1 {
$order ~= $_;
$p1.keep(True) unless $p1;
}
whenever $c2 {
$order ~= $_;
$p2.keep(True);
}
}
is $order, '123', 'multiple channels in whenever blocks work';
}
{
my $closed = False;
my $input = Supplier.new;
my $s = supply {
whenever $input {
emit .uc;
}
CLOSE {
$closed = True;
}
}
my @got;
my $t = $s.tap({ @got.push: $_ });
$input.emit('dugong!');
is @got, 'DUGONG!', 'supply block with CLOSE phaser works as normal';
nok $closed, 'CLOSE phaser not run yet';
$t.close;
ok $closed, 'CLOSE phaser run on tap close';
$closed = False;
my $t2 = $s.tap();
$input.done();
ok $closed, 'CLOSE phaser runs also on normal termination';
$closed = False;
$t2.close;
nok $closed, 'CLOSE phasers do not run twice (normal termination then .close)';
}
{
sub foo($a) {
supply {
whenever Supply.from-list() {
LAST emit $a;
}
}
}
is await(foo(42)), 42, 'LAST in whenever triggered without iterations sees correct outer (1)';
#?rakudo.jvm todo "got: '42'"
is await(foo(69)), 69, 'LAST in whenever triggered without iterations sees correct outer (2)';
}
lives-ok {
react {
whenever Supply.from-list(gather { die }) {
QUIT { default { } }
}
}
}, 'QUIT properly handles exception even when dieing synchronously with the .tap';
{
sub foo($a) {
supply {
whenever Supply.from-list(gather { die }) {
QUIT {
default {
emit $a;
}
}
}
}
}
is await(foo(42)), 42, 'QUIT in whenever triggered without iterations sees correct outer (1)';
#?rakudo.jvm todo "got: '42'"
is await(foo(69)), 69, 'QUIT in whenever triggered without iterations sees correct outer (2)';
}
# RT #128991
lives-ok {
for ^5 {
my $p = Promise.new;
my $s = supply {
whenever Supply.interval(.001) {
done if $++ > 50;
}
}
$s.tap(done => { $p.keep(True) }); # Will die if keeping twice
await $p;
}
}, 'Never get done message twice from a supply';
lives-ok {
for ^5 {
react {
whenever Supply.interval(.001) {
done if $++ > 50;
}
}
}
}, 'No react guts crash in case that once spat out two done messages either';
lives-ok {
my $s = supply { whenever Supply.interval(0.001) { done } }
await do for ^4 {
start {
for ^500 {
react { whenever $s { } }
}
}
}
}, 'No races/crashes around interval that emits done (used to SEGV and various errors)';
lives-ok {
my $times-triggered;
react {
whenever Supply.from-list(^10) {
next if $_ %% 2;
$times-triggered++;
}
}
is $times-triggered, 5, "skipping every even number in ^10 with 'next' gives us 5";
}, 'calling "next" inside a whenever block will not die.';
subtest 'next in whenever' => {
plan 4;
my @res1;
react { whenever supply { .emit for ^10 } { next if $_ > 3; @res1.push: $_ } }
is-deeply @res1, [0, 1, 2, 3], 'skip elements at the end';
my @res2;
react { whenever supply { .emit for ^10 } { next if $_ < 6; @res2.push: $_ } }
is-deeply @res2, [6, 7, 8, 9], 'skip elements at the start';
my @res3;
react { whenever supply { .emit for ^10 } {
next unless 3 < $_ < 6; @res3.push: $_
}}
is-deeply @res3, [4, 5], 'skip elements in the middle';
my @res4;
react {
whenever supply { .emit for ^10 } {
next if $_ > 4; @res4.push: $_;
}
whenever supply { .emit for ^100+5 } {
next if $_ < 103; @res4.push: $_;
}
}
is-deeply @res4.sort, (0, 1, 2, 3, 4, 103, 104).sort,
'works when used in two whenevers';
}
# Golf of a react block with a TCP server, which failed to close taps of
# incoming data on the connection.
{
my $closed = 0;
my $sod = Supply.on-demand:
-> $s { start { $s.emit(42); $s.done; } },
closing => { $closed++ };
react {
whenever Supply.interval(0.01) {
whenever $sod { }
}
whenever Promise.in(1) {
done
}
}
ok $closed, 'Supply is closed by Supply block after it sends done';
}
# RT #126842
lives-ok {
for ^500 {
my $channel = Channel.new;
my $p1 = start {
react {
whenever $channel {
}
}
}
my $p2 = start {
$channel.send($_) for (1..10);
$channel.close;
}
await $p1,$p2;
}
}, 'No hang or crash using react to consume channels';
# RT #128717
{
my $i = 0;
react whenever Supply.from-list(1..5) { $i += $_ }
is $i, 15, 'react without block works';
}
# RT #130716
{
my @pre-emit;
my $ran-done = True;
sub make-supply() {
supply {
until my $done {
@pre-emit.push('x');
emit(++$);
}
CLOSE { $ran-done = True; $done = True }
}
}
my $s2 = make-supply;
my @received;
react {
whenever $s2 -> $n {
push @received, $n;
done if $n >= 5;
}
}
is @received, [1,2,3,4,5],
'whenever tapping supply that syncrhonously emits sees values';
ok $ran-done, 'done block in source supply was run';
is @pre-emit, ['x','x','x','x','x'],
'supply block loop did not run ahead of consumer';
}
{
my $pre-emits = 0;
my $post-emits = 0;
sub make-supply() {
supply {
loop {
$pre-emits++;
emit(++$);
$post-emits++;
}
}
}
my $s2 = make-supply;
my @received;
react {
whenever $s2 -> $n {
push @received, $n;
done if $n >= 5;
}
}
is @received, [1,2,3,4,5],
'whenever tapping infinitely emitting synchronous supply terminates';
is $pre-emits, 6,
'supply block loop is terminated on emit to dead consumer (1)';
is $post-emits, 5,
'supply block loop is terminated on emit to dead consumer (2)';
}
# vim: ft=perl6 expandtab sw=4