-
Notifications
You must be signed in to change notification settings - Fork 11
/
ActorsTesting.ns
841 lines (749 loc) · 21.2 KB
/
ActorsTesting.ns
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
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
Newspeak3
'Actors'
class ActorsTesting usingPlatform: p actors: a testActor: ta minitest: m = (|
private TestActor = ta.
private TestContext = m TestContext.
private MessageNotUnderstood = p kernel MessageNotUnderstood.
private List = p collections List.
private Resolver = a Resolver.
private Timer = a Timer.
private Stopwatch = p kernel Stopwatch.
private Actor = a Actor.
private Promise = a Promise.
|) (
class FooError = Error () (
) : (
)
class HappensAfterChecker eventCount: n = (|
events = List new.
eventCount = n.
resolver = Resolver new.
public whenDone = resolver promise.
|) (
public check: event happensAfter: predecessor = (
nil = predecessor ifFalse:
[(events includes: predecessor) ifFalse:
[resolver break: event, ' happened, but ', predecessor, ' has not yet happened'. ^self]].
events add: event.
events size = eventCount ifTrue: [resolver fulfill: events size].
)
) : (
)
public class MultiActorTests = TestBase () (
public testPipeliningImmediateLocalResolution1 = (
| a1 a2 p p1 p2 |
a1:: ((Actor named: 'otherX') seed: TestActor) <-: new.
a2:: ((Actor named: 'otherX') seed: TestActor) <-: new.
a1 <-: link: a2.
p1:: a1 <-: forwardReturn42.
p2:: a1 <-: forwardReturn42.
p:: a1 <-: forwardNoWaitAdd: p1 and: p2.
^assert: p resolvesTo: 84.
)
public testPipeliningImmediateLocalResolution1Forking = (
| a1 a2 p p1 p2 |
a1:: ((Actor named: 'otherX') seed: TestActor) <-: new.
a2:: ((Actor named: 'otherX') seed: TestActor) <-: new.
a1 <-: link: a2.
p1:: a1 <-: forwardReturn42.
p1 <-: yourself.
p2:: a1 <-: forwardReturn42.
p2 <-: yourself.
p:: a1 <-: forwardNoWaitAdd: p1 and: p2. (* Questionable. *)
^assert: p resolvesTo: 84.
)
public testTokenRingPassByRef = (
| a1 a2 a3 p |
a1:: ((Actor named: 'A1') seed: TestActor) <-: new.
a2:: ((Actor named: 'A2') seed: TestActor) <-: new.
a3:: ((Actor named: 'A3') seed: TestActor) <-: new.
a1 <-: link: a2.
a3 <-: link: a3.
p:: a1 <-: pass: self.
^assert: p resolvesTo: self.
)
public testTokenRingPromisePostResolved = (
| a1 a2 a3 r p |
a1:: ((Actor named: 'A1') seed: TestActor) <-: new.
a2:: ((Actor named: 'A2') seed: TestActor) <-: new.
a3:: ((Actor named: 'A3') seed: TestActor) <-: new.
a1 <-: link: a2.
a3 <-: link: a3.
r:: Resolver new.
p:: a1 <-: pass: r promise.
r fulfill: #after.
^assert: p resolvesTo: #after.
)
public testTokenRingPromisePreResolved = (
| a1 a2 a3 r p |
a1:: ((Actor named: 'A1') seed: TestActor) <-: new.
a2:: ((Actor named: 'A2') seed: TestActor) <-: new.
a3:: ((Actor named: 'A3') seed: TestActor) <-: new.
a1 <-: link: a2.
a3 <-: link: a3.
r:: Resolver new.
r fulfill: #before.
p:: a1 <-: pass: r promise.
^assert: p resolvesTo: #before.
)
public testTokenRingValue = (
| a1 a2 a3 p |
a1:: ((Actor named: 'A1') seed: TestActor) <-: new.
a2:: ((Actor named: 'A2') seed: TestActor) <-: new.
a3:: ((Actor named: 'A3') seed: TestActor) <-: new.
a1 <-: link: a2.
a3 <-: link: a3.
p:: a1 <-: pass: #token.
^assert: p resolvesTo: #token.
)
) : (
TEST_CONTEXT = ()
)
public class SingleActorTests = TestBase () (
public factorial: n = (
^n > 1
ifTrue: [(self <-: factorial: n - 1) <-: * n]
ifFalse: [1]
)
public returnBrokenPromise = (
^self <-: doesNotExist
)
public returnPromise = (
^self <-: yourself
)
public returnPromise: n = (
^n = 0
ifTrue: [self]
ifFalse: [self <-: returnPromise: n - 1].
)
public run: block = (
^block value
)
public signalError = (
Error signal: 'some error'.
)
public testAddPromises = (
(* :todo: Do we want this property? What this tests is really an issue with the Number implementation, not the Promise implementation, namely it uses a double-dispatching the works with proxies. *)
(* | p1 p2 |
p1:: Resolver new fulfill: 3; promise.
p2:: Resolver new fulfill: 4; promise.
assert: p1 + p2 equals: 7. *)
)
public testBrokenPromiseContagion1 = (
| p |
p:: (self <-: signalError) <-: doesNotExist.
^assert: p smashedWith: Error. (* Not MessageNotUnderstood *)
)
public testBrokenPromiseContagion2 = (
| p |
p:: (self <-: signalError) <-: yourself.
^assert: p smashedWith: Error.
)
public testBrokenPromiseContagion3 = (
| p |
p:: when: (self <-: signalError) fulfilled: [halt].
^assert: p smashedWith: Error.
)
public testChainAfterBroken = (
| r p r2 p2 |
r:: Resolver new.
p:: r promise.
r2:: Resolver new.
p2:: r2 promise.
when: p fulfilled: [halt] broken: [when: p fulfilled: [halt] broken: [r2 fulfill: 42]].
r break: 21.
^assert: p2 resolvesTo: 42.
)
public testChainAfterForwarded = (
| r p r2 p2 p3 |
r:: Resolver new.
p:: r promise.
r2:: Resolver new.
p2:: r2 promise.
when: p fulfilled: [:res | deny: res isPromise].
p3:: [ (*A*) [ when: p (*B*) fulfilled: [r2 fulfill: 42]] <-: value. 21] <-: value.
assert: [p3 isPromise].
r fulfill: p3.
(* No way to guarantee B happens after p is resolved to p3, but this happens in the current implementation. *)
^assert: p2 resolvesTo: 42.
)
public testChainAfterResolved = (
| r p r2 p2 |
r:: Resolver new.
p:: r promise.
r2:: Resolver new.
p2:: r2 promise.
when: p fulfilled: [when: p fulfilled: [r2 fulfill: 42]].
r fulfill: 21.
^assert: p2 resolvesTo: 42.
)
public testChainPromiseToError = (
| p |
p:: self <-: returnBrokenPromise.
assert: [p isPromise].
^assert: p smashedWith: MessageNotUnderstood.
)
public testChainPromiseToValue = (
| p |
p:: self <-: returnPromise.
assert: [p isPromise].
^assert: p resolvesTo: self.
)
public testDatalock = (
| weHaveAlwaysBeenAtWarWithEurasia epimenides |
weHaveAlwaysBeenAtWarWithEurasia:: true.
epimenides:: [weHaveAlwaysBeenAtWarWithEurasia <-: not].
weHaveAlwaysBeenAtWarWithEurasia:: epimenides <-: value.
when: weHaveAlwaysBeenAtWarWithEurasia fulfilled: [halt] broken: [halt].
)
public testDeeplyChainedPromise = (
| p |
p:: self <-: returnPromise: 20.
assert: [p isPromise].
^assert: p resolvesTo: self.
)
public testDoubleResolution = (
| r p |
p:: Resolver new promise.
(* Initially settled. *)
r:: Resolver new.
r fulfill: 1.
should: [r fulfill: 1] signal: Error.
r:: Resolver new.
r fulfill: 1.
should: [r fulfill: p] signal: Error.
r:: Resolver new.
r fulfill: 1.
should: [r break: 1] signal: Error.
(* Initially forwarded. *)
r:: Resolver new.
r fulfill: p.
should: [r fulfill: 1] signal: Error.
r:: Resolver new.
r fulfill: p.
should: [r fulfill: p] signal: Error.
r:: Resolver new.
r fulfill: p.
should: [r break: 1] signal: Error.
(* Initially broken. *)
r:: Resolver new.
r break: 1.
should: [r fulfill: 1] signal: Error.
r:: Resolver new.
r break: 1.
should: [r fulfill: p] signal: Error.
r:: Resolver new.
r break: 1.
should: [r break: 1] signal: Error.
)
public testEventualSendToNearPromiseError = (
| p |
p:: (self <-: yourself) <-: doesNotExist.
assert: [p isPromise].
^assert: p smashedWith: MessageNotUnderstood.
)
public testEventualSendToNearPromiseValue = (
| p |
p:: (self <-: yourself) <-: yourself.
assert: [p isPromise].
^assert: p resolvesTo: self.
)
public testEventualSendToNearRefError = (
| p |
p:: self <-: doesNotExist.
assert: [p isPromise].
^assert: p smashedWith: MessageNotUnderstood.
)
public testEventualSendToNearRefValue = (
| p |
p:: self <-: yourself.
assert: [p isPromise].
^assert: p resolvesTo: self.
)
public testFactorial = (
^assert: (factorial: 10) resolvesTo: 3628800.
)
public testImmediateResolution = (
| p p2 |
p:: self <-: run: [3].
p2:: self <-: run: [p + 4].
^assert: p2 resolvesTo: 7.
)
public testImmediateSendToBroken = (
| p |
p:: Resolver new break: FooError new; promise.
should: [p size] signal: Error. (* Not FooError. *)
)
public testImmediateSendToResolved = (
| p |
p:: Resolver new fulfill: {1. 2. 3}; promise.
assert: p size equals: 3.
)
public testImmediateSendToUnresolved = (
| p |
p:: Resolver new promise.
should: [p size] signal: Error.
)
public testMissingWhenBroken = (
| r p |
r:: Resolver new.
p:: when: r promise fulfilled: [:v | halt].
assert: [p isPromise].
r break: Error new.
^assert: p smashedWith: Error.
)
public testNearReferenceWhen = (
(* Does when need to be a message to some object other than the reference being observed? *)
(* If this doesn't work, how can one write a method that accepts input from another actor that might sometimes be a near ref because the argument was a Value but otherwise a far ref? *)
| o p |
o:: 42.
p:: when: o fulfilled: [:v | v * 2] broken: [:e | fail].
^assert: p resolvesTo: 84.
)
public testPromiseStopContagion = (
| p |
p:: when: (self <-: signalError)
fulfilled: [halt]
broken: [:e | 'Return without exception'].
assert: [p isPromise].
^assert: p resolvesTo: 'Return without exception'.
)
public testResolveAfterWhen = (
| r p |
r:: Resolver new.
p:: r promise.
p:: when: p fulfilled: [:v | assert: v equals: #foo. 1].
r fulfill: #foo.
^assert: p resolvesTo: 1.
)
public testResolveBeforeWhen = (
| r p |
r:: Resolver new.
p:: r promise.
r fulfill: #foo.
p:: when: p fulfilled: [:v | assert: v equals: #foo. 1].
^assert: p resolvesTo: 1.
)
public testUnresolved = (
| r p |
r:: Resolver new.
p:: r promise.
deny: [p isResolved].
)
public testViciousResolutionDirect = (
| r p |
r:: Resolver new.
p:: r promise.
r fulfill: p.
^assert: p smashedWith: Error. (* CyclicResolutionError *)
)
public testViciousResolutionIndirect = (
| r1 p1 r2 p2 joinResolver outstanding |
r1:: Resolver new.
p1:: r1 promise.
r2:: Resolver new.
p2:: r2 promise.
r1 fulfill: p2.
r2 fulfill: p1.
outstanding:: 2.
joinResolver:: Resolver new.
when: p1
fulfilled: [:resolution | joinResolver break: (Error new messageText: 'p1 was not smashed')]
broken: [:error | outstanding:: outstanding - 1. 0 = outstanding ifTrue: [joinResolver fulfill: nil]].
when: p2
fulfilled: [:resolution | joinResolver break: (Error new messageText: 'p2 was not smashed')]
broken: [:error | outstanding:: outstanding - 1. 0 = outstanding ifTrue: [joinResolver fulfill: nil]].
^joinResolver promise
)
public testWhenBrokenError = (
| r p |
r:: Resolver new.
p:: when: r promise
fulfilled: [:v | halt]
broken: [:e | doesNotExist].
assert: [p isPromise].
r break: Error new.
^assert: p smashedWith: MessageNotUnderstood.
)
public testWhenBrokenValue = (
| r p |
r:: Resolver new.
p:: when: r promise
fulfilled: [:v | halt]
broken: [:e | 42].
assert: [p isPromise].
r break: Error new.
^assert: p resolvesTo: 42.
)
public testWhenResolvedError = (
| r p |
r:: Resolver new.
p:: when: r promise
fulfilled: [:v | Error signal: 'Something']
broken: [:e | fail].
assert: [p isPromise].
r fulfill: 21.
^assert: p smashedWith: Error.
)
public testWhenResolvedValue = (
| r p |
r:: Resolver new.
p:: when: r promise
fulfilled: [:v | v * 2]
broken: [:e | fail].
assert: [p isPromise].
r fulfill: 21.
^assert: p resolvesTo: 42.
)
) : (
TEST_CONTEXT = ()
)
class TestBase = TestContext () (
assert: promise resolvesTo: expectedValue = (
^Promise
when: promise
fulfilled: [:value | assert: value equals: expectedValue]
broken: [:error | ^failWithMessage: 'Expected resolution of ', expectedValue printString, ' but broken with ', error printString]
)
assert: promise smashedWith: expectedErrorClass = (
^Promise
when: promise
fulfilled: [:value | ^failWithMessage: 'Expected broken with ', expectedErrorClass printString, ' but resolved with ', value printString]
broken: [:error | (* assert: resolution class equals: expectedErrorClass. *)]
)
) : (
)
public class TimerTests = TestBase () (
busyMilliseconds: millis = (
| stopwatch = Stopwatch new. |
stopwatch start.
[stopwatch elapsedMilliseconds < millis] whileTrue.
)
public testCoTimers = (
| r ticks t1 t2 |
r:: Resolver new.
ticks:: 0.
t1:: Timer every: 20 do: [:t | ticks:: ticks + 1].
t2:: Timer after: 30 do: [t1 cancel. r fulfill: ticks].
^assert: r promise resolvesTo: 1
)
public testOneShotTimer = (
| r t |
r:: Resolver new.
t:: Timer after: 100 do: [deny: [t isActive]. r fulfill: 42].
assert: [t isActive].
^assert: r promise resolvesTo: 42
)
public testOneShotTimerInvalidCallback = (
| fired t r |
fired:: false.
t:: Timer after: 4 do: [:timer | fired:: true].
r:: Resolver new.
Timer after: 8 do: [t cancel. r fulfill: 42].
^when: r promise fulfilled:
[:result |
assert: result equals: 42.
deny: fired]
)
public testRepeatingTimer = (
| count ::= 0. r t |
r:: Resolver new.
t:: Timer every: 10 do:
[:timer |
assert: timer equals: t.
count:: count + 1.
assert: [timer isActive].
count = 5 ifTrue: [timer cancel. deny: [timer isActive]. r fulfill: 42].
count > 5 ifTrue: [failWithMessage: 'Cancel failed']].
^assert: r promise resolvesTo: 42
)
public testRepeatingTimerContinuesWithExceptions = (
| count ::= 0. r t |
r:: Resolver new.
t:: Timer every: 4 do:
[:timer |
count:: count + 1.
count = 5
ifTrue: [timer cancel. r fulfill: 42]
ifFalse: [causeException]].
^when: r promise fulfilled:
[:result |
assert: result equals: 42.
assert: count equals: 5]
)
public testRepeatingTimerDoesNotFireMidTurn = (
| fired t r |
fired:: false.
t:: Timer every: 4 do: [:timer | fired:: true. timer cancel].
busyMilliseconds: 8.
deny: fired.
t cancel.
[yieldMilliseconds: 8] ensure: [t cancel].
deny: fired.
)
public testRepeatingTimerInvalidCallback = (
| fired t r |
fired:: false.
t:: Timer every: 4 do: [fired:: true].
r:: Resolver new.
Timer after: 16 do: [t cancel. r fulfill: 42].
^when: r promise fulfilled:
[:result |
assert: result equals: 42.
deny: fired]
)
public testTimerDoesNotFireEarlyMicros = (
| r s t elapsedMicros |
r:: Resolver new.
s:: Stopwatch new start.
t:: Timer after: 16 do: [elapsedMicros:: s elapsedMicroseconds. r fulfill: 42].
assert: [t isActive].
^when: r promise fulfilled:
[:result |
assert: result equals: 42.
assert: elapsedMicros >= 16000]
)
public testTimerDoesNotFireEarlyMillis = (
| r s t elapsedMillis |
r:: Resolver new.
s:: Stopwatch new start.
t:: Timer after: 16 do: [elapsedMillis:: s elapsedMilliseconds. r fulfill: 42].
assert: [t isActive].
^when: r promise fulfilled:
[:result |
assert: result equals: 42.
assert: elapsedMillis >= 16]
)
public testTimerDoesNotFireMidTurn = (
| fired t r |
fired:: false.
t:: Timer after: 4 do: [fired:: true].
busyMilliseconds: 8.
deny: fired.
t cancel.
[yieldMilliseconds: 8] ensure: [t cancel].
deny: fired.
)
public testTimerHeapGrowth = (
| r inOrder nextExpected |
r:: Resolver new.
inOrder:: true.
nextExpected:: 1.
1 to: 129 do: [:i |
Timer after: 1 do:
[nextExpected = i ifFalse: [inOrder:: false].
nextExpected:: nextExpected + 1.
nextExpected = 130 ifTrue: [r fulfill: #done]]].
^when: r promise fulfilled:
[:resolution |
assert: resolution equals: #done.
assert: inOrder]
)
public testTimerHeapGrowthWithCancellation = (
| r timers inOrder nextExpected |
r:: Resolver new.
timers:: Array new: 129.
inOrder:: true.
nextExpected:: 1.
1 to: 129 do: [:i | | timer |
timer:: Timer after: 1 do:
[nextExpected = i ifFalse: [inOrder:: false].
nextExpected:: nextExpected + 1.
i \\ 5 = 0 ifTrue:
[(timers at: i + 1) cancel.
nextExpected:: nextExpected + 1.].
nextExpected = 130 ifTrue: [r fulfill: #done]].
timers at: i put: timer].
^when: r promise fulfilled:
[:result |
assert: result equals: #done.
assert: inOrder]
)
yieldMilliseconds: millis = (
| r |
r:: Resolver new.
Timer after: millis do: [r fulfill: 42].
assert: r promise resolvesTo: 42.
)
) : (
TEST_CONTEXT = ()
)
public class TwoActorTests = TestBase () (
public echoItself: o = (
^o <-: yourself
)
public testFarRefImmediateSend = (
| testActorP r |
testActorP:: ((Actor named: 'other') seed: TestActor) <-: new.
r:: Resolver new.
when: testActorP fulfilled:
[:testActorRef |
should: [testActorRef return42] signal: Error.
r fulfill: true].
^assert: r promise resolvesTo: true.
)
public testFarRefWhen = (
| testActorP p |
testActorP:: ((Actor named: 'other') seed: TestActor) <-: new.
p:: when: testActorP fulfilled:
[:testActorRef | when: testActorRef fulfilled: [42] broken: [21]].
^assert: p resolvesTo: 42.
)
public testFarSendEcho = (
| testActor p |
testActor:: ((Actor named: 'other') seed: TestActor) <-: new.
p:: testActor <-: echo: self.
^assert: p resolvesTo: self.
)
public testFarSendError1 = (
| testActor p |
testActor:: ((Actor named: 'other') seed: TestActor) <-: new.
p:: testActor <-: signalError.
^assert: p smashedWith: Error.
)
public testFarSendError2 = (
| testActor p |
testActor:: ((Actor named: 'other') seed: TestActor) <-: new.
p:: testActor <-: doesNotExist.
^assert: p smashedWith: MessageNotUnderstood.
)
public testFarSendPromiseValue1 = (
| testActor p |
testActor:: ((Actor named: 'other') seed: TestActor) <-: new.
p:: testActor <-: returnPromise42.
^assert: p resolvesTo: 42.
)
public testFarSendPromiseValue2 = (
| testActor p |
testActor:: ((Actor named: 'other') seed: TestActor) <-: new.
p:: testActor <-: returnPromiseYourself.
^assert: (when: p fulfilled: [:testActorRef | testActor <-: = testActorRef]) resolvesTo: true.
)
public testFarSendValue = (
| testActor p |
testActor:: ((Actor named: 'other') seed: TestActor) <-: new.
p:: testActor <-: return42.
^assert: p resolvesTo: 42.
)
public testPipelining = (
| testActor p |
testActor:: ((Actor named: 'other') seed: TestActor) <-: new.
testActor <-: Promise: Promise.
(* add:and: should not be waiting on the 42 promises in this actor to be resolved. *)
p:: testActor <-: waitAdd: (testActor <-: returnPromise42) and: (testActor <-: returnPromise42).
^assert: p resolvesTo: 84.
)
public testPipeliningImmediateLocalResolution1 = (
| testActor p p1 p2 |
testActor:: ((Actor named: 'otherX') seed: TestActor) <-: new.
p1:: testActor <-: return42.
p2:: testActor <-: return42.
p:: testActor <-: noWaitAdd: p1 and: p2.
^assert: p resolvesTo: 84.
)
public testPipeliningImmediateLocalResolution1Forking = (
| testActor p p1 p2 |
testActor:: ((Actor named: 'otherX') seed: TestActor) <-: new.
p1:: testActor <-: return42.
p1 <-: yourself.
p2:: testActor <-: return42.
p2 <-: yourself.
p:: testActor <-: noWaitAdd: p1 and: p2. (* Questionable. *)
^assert: p resolvesTo: 84.
)
public testPipeliningImmediateLocalResolution2 = (
| testActor r p1 p2 |
testActor:: ((Actor named: 'other') seed: TestActor) <-: new.
r:: Resolver new.
p1:: 42 <-: yourself.
p2:: 42 <-: yourself.
[r fulfill: (testActor <-: noWaitAdd: p1 and: p2)] <-: value.
^assert: r promise resolvesTo: 84.
)
public testPipeliningImmediateLocalResolution2Forking = (
| testActor r p1 p2 |
testActor:: ((Actor named: 'other') seed: TestActor) <-: new.
r:: Resolver new.
p1:: 42 <-: yourself.
p1 <-: yourself.
p2:: 42 <-: yourself.
p2 <-: yourself.
[r fulfill: (testActor <-: noWaitAdd: p1 and: p2)] <-: value. (* Questionable. *)
^assert: r promise resolvesTo: 84.
)
public testPromiseForking1 = (
| testActor p1 p2 p3 |
testActor:: ((Actor named: 'other') seed: TestActor) <-: new.
p1:: HappensAfterChecker <-: eventCount: 15.
p1 <-: check: 'A' happensAfter: nil.
p2:: testActor <-: on: p1 check: 'B' happensAfter: 'A'.
p2 <-: check: 'C' happensAfter: 'B'.
p2 <-: check: 'D' happensAfter: 'C'.
testActor <-: on: p2 check: 'E' happensAfter: 'D'.
p2 <-: check: 'F' happensAfter: 'D'.
p1 <-: check: 'G' happensAfter: 'A'.
p1 <-: check: 'H' happensAfter: 'G'.
p3:: testActor <-: on: p1 check: 'I' happensAfter: 'H'.
p3 <-: check: 'J' happensAfter: 'I'.
p3 <-: check: 'K' happensAfter: 'J'.
testActor <-: on: p3 check: 'L' happensAfter: 'K'.
p3 <-: check: 'M' happensAfter: 'K'.
p1 <-: check: 'N' happensAfter: 'H'.
p1 <-: check: 'O' happensAfter: 'N'.
^assert: p1 <-: whenDone resolvesTo: 15.
)
public testPromiseForking2 = (
| testActor r p1 p2 p3 |
testActor:: ((Actor named: 'other') seed: TestActor) <-: new.
r:: Resolver new.
p1:: r promise.
p1 <-: check: 'A' happensAfter: nil.
p2:: testActor <-: on: p1 check: 'B' happensAfter: 'A'.
p2 <-: check: 'C' happensAfter: 'B'.
p2 <-: check: 'D' happensAfter: 'C'.
testActor <-: on: p2 check: 'E' happensAfter: 'D'.
p2 <-: check: 'F' happensAfter: 'D'.
p1 <-: check: 'G' happensAfter: 'A'.
p1 <-: check: 'H' happensAfter: 'G'.
p3:: testActor <-: on: p1 check: 'I' happensAfter: 'H'.
p3 <-: check: 'J' happensAfter: 'I'.
p3 <-: check: 'K' happensAfter: 'J'.
testActor <-: on: p3 check: 'L' happensAfter: 'K'.
p3 <-: check: 'M' happensAfter: 'K'.
p1 <-: check: 'N' happensAfter: 'H'.
p1 <-: check: 'O' happensAfter: 'N'.
when: (testActor <-: yourself) fulfilled: [r fulfill: (HappensAfterChecker <-: eventCount: 15)].
^assert: p1 <-: whenDone resolvesTo: 15.
)
public testPromiseLocalRemoteFlipFlop = (
| testActor r p |
testActor:: ((Actor named: 'other') seed: TestActor) <-: new.
r:: Resolver new.
p:: testActor <-: echoItself: r promise.
p:: self <-: echoItself: p.
p:: testActor <-: echoItself: r promise.
p:: self <-: echoItself: p.
p:: testActor <-: echoItself: r promise.
p:: self <-: echoItself: p.
r fulfill: self.
^assert: p resolvesTo: self.
)
public testViciousResolutionIndirectWithRemotePromise = (
| testActor r p |
testActor:: ((Actor named: 'other') seed: TestActor) <-: new.
r:: Resolver new.
p:: r promise.
r fulfill: (testActor <-: echo: p).
^assert: p smashedWith: Error. (* CyclicResolutionError *)
)
) : (
TEST_CONTEXT = ()
)
when: ref fulfilled: onValue = (
^Promise when: ref fulfilled: onValue
)
when: ref fulfilled: onValue broken: onError = (
^Promise when: ref fulfilled: onValue broken: onError
)
) : (
)