-
Notifications
You must be signed in to change notification settings - Fork 65
/
Main.purs
792 lines (725 loc) · 21.7 KB
/
Main.purs
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
module Test.Main where
import Prelude
import Control.Alt ((<|>))
import Control.Lazy (fix)
import Control.Monad.Error.Class (throwError, catchError)
import Control.Parallel (parallel, sequential, parTraverse_)
import Data.Array as Array
import Data.Bifunctor (lmap)
import Data.Either (Either(..), either, isLeft, isRight)
import Data.Foldable (sum)
import Data.Maybe (Maybe(..))
import Data.Time.Duration (Milliseconds(..))
import Data.Traversable (traverse)
import Effect (Effect)
import Effect.Aff (Aff, Canceler(..), runAff, runAff_, launchAff, makeAff, try, bracket, generalBracket, delay, forkAff, suspendAff, joinFiber, killFiber, never, supervise, Error, error, message)
import Effect.Aff.Compat as AC
import Effect.Class (class MonadEffect, liftEffect)
import Effect.Console as Console
import Effect.Exception (throwException)
import Effect.Ref (Ref)
import Effect.Ref as Ref
import Effect.Unsafe (unsafePerformEffect)
import Test.Assert (assert')
newRef :: forall m a. MonadEffect m => a -> m (Ref a)
newRef = liftEffect <<< Ref.new
readRef :: forall m a. MonadEffect m => Ref a -> m a
readRef = liftEffect <<< Ref.read
writeRef :: forall m a. MonadEffect m => Ref a -> a -> m Unit
writeRef r = liftEffect <<< flip Ref.write r
modifyRef :: forall m a. MonadEffect m => Ref a -> (a -> a) -> m a
modifyRef r = liftEffect <<< flip Ref.modify r
assertEff :: String -> Either Error Boolean -> Effect Unit
assertEff s = case _ of
Left err -> do
Console.log ("[Error] " <> s)
throwException err
Right r -> do
assert' ("Assertion failure " <> s) r
Console.log ("[OK] " <> s)
runAssert :: String -> Aff Boolean -> Effect Unit
runAssert s = runAff_ (assertEff s)
runAssertEq :: forall a. Eq a => String -> a -> Aff a -> Effect Unit
runAssertEq s a = runAff_ (assertEff s <<< map (eq a))
assertEq :: forall a. Eq a => String -> a -> Aff a -> Aff Unit
assertEq s a aff = liftEffect <<< assertEff s <<< map (eq a) =<< try aff
assert :: String -> Aff Boolean -> Aff Unit
assert s aff = liftEffect <<< assertEff s =<< try aff
withTimeout :: forall a. Milliseconds -> Aff a -> Aff a
withTimeout ms aff =
either throwError pure =<< sequential do
parallel (try aff) <|> parallel (delay ms $> Left (error "Timed out"))
test_pure :: Effect Unit
test_pure = runAssertEq "pure" 42 (pure 42)
test_bind :: Effect Unit
test_bind = runAssertEq "bind" 44 do
n1 <- pure 42
n2 <- pure (n1 + 1)
n3 <- pure (n2 + 1)
pure n3
test_try :: Effect Unit
test_try = runAssert "try" do
n <- try (pure 42)
case n of
Right 42 -> pure true
_ -> pure false
test_throw :: Effect Unit
test_throw = runAssert "try/throw" do
n <- try (throwError (error "Nope."))
pure (isLeft n)
test_liftEffect :: Effect Unit
test_liftEffect = runAssertEq "liftEffect" 42 do
ref <- newRef 0
liftEffect do
writeRef ref 42
readRef ref
test_delay :: Aff Unit
test_delay = assert "delay" do
delay (Milliseconds 1000.0)
pure true
test_fork :: Aff Unit
test_fork = assert "fork" do
ref <- newRef ""
_ <- forkAff do
delay (Milliseconds 10.0)
modifyRef ref (_ <> "child")
_ <- modifyRef ref (_ <> "go")
delay (Milliseconds 20.0)
_ <- modifyRef ref (_ <> "parent")
eq "gochildparent" <$> readRef ref
test_join :: Aff Unit
test_join = assert "join" do
ref <- newRef ""
fiber <- forkAff do
delay (Milliseconds 10.0)
_ <- modifyRef ref (_ <> "child")
readRef ref
_ <- modifyRef ref (_ <> "parent")
eq "parentchild" <$> joinFiber fiber
test_join_throw :: Aff Unit
test_join_throw = assert "join/throw" do
fiber <- forkAff do
delay (Milliseconds 10.0)
throwError (error "Nope.")
isLeft <$> try (joinFiber fiber)
test_join_throw_sync :: Aff Unit
test_join_throw_sync = assert "join/throw/sync" do
fiber <- forkAff (throwError (error "Nope."))
isLeft <$> try (joinFiber fiber)
test_multi_join :: Aff Unit
test_multi_join = assert "join/multi" do
ref <- newRef 1
f1 <- forkAff do
delay (Milliseconds 10.0)
_ <- modifyRef ref (_ + 1)
pure 10
f2 <- forkAff do
delay (Milliseconds 20.0)
_ <- modifyRef ref (_ + 1)
pure 20
n1 <- traverse joinFiber
[ f1
, f1
, f1
, f2
]
n2 <- readRef ref
pure (sum n1 == 50 && n2 == 3)
test_suspend :: Aff Unit
test_suspend = assert "suspend" do
ref <- newRef ""
fiber <- suspendAff do
delay (Milliseconds 10.0)
modifyRef ref (_ <> "child")
_ <- modifyRef ref (_ <> "go")
delay (Milliseconds 20.0)
_ <- modifyRef ref (_ <> "parent")
_ <- joinFiber fiber
eq "goparentchild" <$> readRef ref
test_makeAff :: Aff Unit
test_makeAff = assert "makeAff" do
ref1 <- newRef Nothing
ref2 <- newRef 0
fiber <- forkAff do
n <- makeAff \cb -> do
writeRef ref1 (Just cb)
pure mempty
writeRef ref2 n
delay (Milliseconds 5.0)
cb <- readRef ref1
case cb of
Just k -> do
liftEffect $ k (Right 42)
_ <- joinFiber fiber
eq 42 <$> readRef ref2
Nothing -> pure false
test_bracket :: Aff Unit
test_bracket = assert "bracket" do
ref <- newRef []
let
action s = do
delay (Milliseconds 10.0)
_ <- modifyRef ref (_ <> [ s ])
pure s
fiber <- forkAff do
delay (Milliseconds 40.0)
readRef ref
_ <- bracket
(action "foo")
(\s -> void $ action (s <> "/release"))
(\s -> action (s <> "/run"))
joinFiber fiber <#> eq
[ "foo"
, "foo/run"
, "foo/release"
]
test_bracket_nested :: Aff Unit
test_bracket_nested = assert "bracket/nested" do
ref <- newRef []
let
action s = do
delay (Milliseconds 10.0)
_ <- modifyRef ref (_ <> [ s ])
pure s
bracketAction s =
bracket
(action (s <> "/bar"))
(\s' -> void $ action (s' <> "/release"))
(\s' -> action (s' <> "/run"))
_ <- bracket
(bracketAction "foo")
(\s -> void $ bracketAction (s <> "/release"))
(\s -> bracketAction (s <> "/run"))
readRef ref <#> eq
[ "foo/bar"
, "foo/bar/run"
, "foo/bar/release"
, "foo/bar/run/run/bar"
, "foo/bar/run/run/bar/run"
, "foo/bar/run/run/bar/release"
, "foo/bar/run/release/bar"
, "foo/bar/run/release/bar/run"
, "foo/bar/run/release/bar/release"
]
test_general_bracket :: Aff Unit
test_general_bracket = assert "bracket/general" do
ref <- newRef ""
let
action s = do
delay (Milliseconds 10.0)
_ <- modifyRef ref (_ <> s)
pure s
bracketAction s =
generalBracket (action s)
{ killed: \error s' -> void $ action (s' <> "/kill/" <> message error)
, failed: \error s' -> void $ action (s' <> "/throw/" <> message error)
, completed: \r s' -> void $ action (s' <> "/release/" <> r)
}
f1 <- forkAff $ bracketAction "foo" (const (action "a"))
delay (Milliseconds 5.0)
killFiber (error "z") f1
r1 <- try $ joinFiber f1
f2 <- forkAff $ bracketAction "bar" (const (throwError $ error "b"))
r2 <- try $ joinFiber f2
f3 <- forkAff $ bracketAction "baz" (const (action "c"))
r3 <- try $ joinFiber f3
r4 <- readRef ref
pure (isLeft r1 && isLeft r2 && isRight r3 && r4 == "foofoo/kill/zbarbar/throw/bbazcbaz/release/c")
test_supervise :: Aff Unit
test_supervise = assert "supervise" do
ref <- newRef ""
r1 <- supervise do
_ <- forkAff do
bracket
(modifyRef ref (_ <> "acquire"))
(\_ -> void $ modifyRef ref (_ <> "release"))
(\_ -> delay (Milliseconds 10.0))
_ <- forkAff do
delay (Milliseconds 11.0)
void $ modifyRef ref (_ <> "delay")
delay (Milliseconds 5.0)
_ <- modifyRef ref (_ <> "done")
pure "done"
delay (Milliseconds 20.0)
r2 <- readRef ref
pure (r1 == "done" && r2 == "acquiredonerelease")
test_kill :: Aff Unit
test_kill = assert "kill" do
fiber <- forkAff never
killFiber (error "Nope") fiber
isLeft <$> try (joinFiber fiber)
test_kill_canceler :: Aff Unit
test_kill_canceler = assert "kill/canceler" do
ref <- newRef ""
fiber <- forkAff do
_ <- makeAff \_ -> pure $ Canceler \_ -> do
delay (Milliseconds 20.0)
liftEffect (writeRef ref "cancel")
writeRef ref "done"
delay (Milliseconds 10.0)
killFiber (error "Nope") fiber
res <- try (joinFiber fiber)
n <- readRef ref
pure (n == "cancel" && (lmap message res) == Left "Nope")
test_kill_bracket :: Aff Unit
test_kill_bracket = assert "kill/bracket" do
ref <- newRef ""
let
action n = do
delay (Milliseconds 10.0)
void $ modifyRef ref (_ <> n)
fiber <-
forkAff $ bracket
(action "a")
(\_ -> action "b")
(\_ -> action "c")
delay (Milliseconds 5.0)
killFiber (error "Nope") fiber
_ <- try (joinFiber fiber)
eq "ab" <$> readRef ref
test_kill_bracket_nested :: Aff Unit
test_kill_bracket_nested = assert "kill/bracket/nested" do
ref <- newRef []
let
action s = do
delay (Milliseconds 10.0)
_ <- modifyRef ref (_ <> [ s ])
pure s
bracketAction s =
bracket
(action (s <> "/bar"))
(\s' -> void $ action (s' <> "/release"))
(\s' -> action (s' <> "/run"))
fiber <-
forkAff $ bracket
(bracketAction "foo")
(\s -> void $ bracketAction (s <> "/release"))
(\s -> bracketAction (s <> "/run"))
delay (Milliseconds 5.0)
killFiber (error "Nope") fiber
_ <- try (joinFiber fiber)
readRef ref <#> eq
[ "foo/bar"
, "foo/bar/run"
, "foo/bar/release"
, "foo/bar/run/release/bar"
, "foo/bar/run/release/bar/run"
, "foo/bar/run/release/bar/release"
]
test_kill_general_bracket_nested :: Aff Unit
test_kill_general_bracket_nested = assert "kill/bracket/general/nested" do
ref <- newRef []
let
action s = do
_ <- modifyRef ref (_ <> [ s ])
pure unit
bracketAction s acq =
generalBracket acq
{ killed: \_ _ -> action (s <> "/killed")
, failed: \_ _ -> action (s <> "/failed")
, completed: \_ _ -> action (s <> "/completed")
}
( \_ -> do
delay (Milliseconds 10.0)
action (s <> "/run")
)
fiber <- forkAff do
bracketAction "outer" do
action "outer/acquire"
bracketAction "inner" do
action "inner/acquire"
delay (Milliseconds 10.0)
delay (Milliseconds 5.0)
killFiber (error "nope") fiber
readRef ref <#> eq
[ "outer/acquire"
, "inner/acquire"
, "inner/run"
, "inner/completed"
, "outer/killed"
]
test_kill_supervise :: Aff Unit
test_kill_supervise = assert "kill/supervise" do
ref <- newRef ""
let
action s = generalBracket
(modifyRef ref (_ <> "acquire" <> s))
{ failed: \_ _ -> void $ modifyRef ref (_ <> "throw" <> s)
, killed: \_ _ -> void $ modifyRef ref (_ <> "kill" <> s)
, completed: \_ _ -> void $ modifyRef ref (_ <> "complete" <> s)
}
( \_ -> do
delay (Milliseconds 10.0)
void $ modifyRef ref (_ <> "child" <> s)
)
fiber <- forkAff $ supervise do
_ <- forkAff $ action "foo"
_ <- forkAff $ action "bar"
delay (Milliseconds 5.0)
modifyRef ref (_ <> "parent")
delay (Milliseconds 1.0)
killFiber (error "nope") fiber
delay (Milliseconds 20.0)
eq "acquirefooacquirebarkillfookillbar" <$> readRef ref
test_kill_finalizer_catch :: Aff Unit
test_kill_finalizer_catch = assert "kill/finalizer/catch" do
ref <- newRef ""
fiber <- forkAff $ bracket
(delay (Milliseconds 10.0))
(\_ -> throwError (error "Finalizer") `catchError` \_ -> writeRef ref "caught")
(\_ -> pure unit)
killFiber (error "Nope") fiber
eq "caught" <$> readRef ref
test_kill_finalizer_bracket :: Aff Unit
test_kill_finalizer_bracket = assert "kill/finalizer/bracket" do
ref <- newRef ""
fiber <- forkAff $ bracket
(delay (Milliseconds 10.0))
( \_ -> generalBracket (pure unit)
{ killed: \_ _ -> writeRef ref "killed"
, failed: \_ _ -> writeRef ref "failed"
, completed: \_ _ -> writeRef ref "completed"
}
(\_ -> pure unit)
)
(\_ -> pure unit)
killFiber (error "Nope") fiber
eq "completed" <$> readRef ref
test_parallel :: Aff Unit
test_parallel = assert "parallel" do
ref <- newRef ""
let
action s = do
delay (Milliseconds 10.0)
_ <- modifyRef ref (_ <> s)
pure s
f1 <- forkAff $ sequential $
{ a: _, b: _ }
<$> parallel (action "foo")
<*> parallel (action "bar")
delay (Milliseconds 15.0)
r1 <- readRef ref
r2 <- joinFiber f1
pure (r1 == "foobar" && r2.a == "foo" && r2.b == "bar")
test_parallel_throw :: Aff Unit
test_parallel_throw = assert "parallel/throw" $ withTimeout (Milliseconds 100.0) do
ref <- newRef ""
let
action n s = do
delay (Milliseconds n)
_ <- modifyRef ref (_ <> s)
pure s
r1 <- try $ sequential $
{ a: _, b: _ }
<$> parallel (action 10.0 "foo" *> throwError (error "Nope"))
<*> parallel never
r2 <- readRef ref
pure (isLeft r1 && r2 == "foo")
test_kill_parallel :: Aff Unit
test_kill_parallel = assert "kill/parallel" do
ref <- newRef ""
let
action s = do
bracket
(pure unit)
(\_ -> void $ modifyRef ref (_ <> "killed" <> s))
( \_ -> do
delay (Milliseconds 10.0)
void $ modifyRef ref (_ <> s)
)
f1 <- forkAff $ sequential $
parallel (action "foo") *> parallel (action "bar")
f2 <- forkAff do
delay (Milliseconds 5.0)
killFiber (error "Nope") f1
modifyRef ref (_ <> "done")
_ <- try $ joinFiber f1
_ <- try $ joinFiber f2
eq "killedfookilledbardone" <$> readRef ref
test_parallel_alt :: Aff Unit
test_parallel_alt = assert "parallel/alt" do
ref <- newRef ""
let
action n s = do
delay (Milliseconds n)
_ <- modifyRef ref (_ <> s)
pure s
f1 <- forkAff $ sequential $
parallel (action 10.0 "foo") <|> parallel (action 5.0 "bar")
delay (Milliseconds 10.0)
r1 <- readRef ref
r2 <- joinFiber f1
pure (r1 == "bar" && r2 == "bar")
test_parallel_alt_throw :: Aff Unit
test_parallel_alt_throw = assert "parallel/alt/throw" do
r1 <- sequential $
parallel (delay (Milliseconds 10.0) *> throwError (error "Nope."))
<|> parallel (delay (Milliseconds 11.0) $> "foo")
<|> parallel (delay (Milliseconds 12.0) $> "bar")
pure (r1 == "foo")
test_parallel_alt_sync :: Aff Unit
test_parallel_alt_sync = assert "parallel/alt/sync" do
ref <- newRef ""
let
action s = do
bracket
(pure unit)
(\_ -> void $ modifyRef ref (_ <> "killed" <> s))
(\_ -> modifyRef ref (_ <> s) $> s)
r1 <- sequential $
parallel (action "foo")
<|> parallel (action "bar")
<|> parallel (action "baz")
r2 <- readRef ref
pure (r1 == "foo" && r2 == "fookilledfoo")
test_parallel_mixed :: Aff Unit
test_parallel_mixed = assert "parallel/mixed" do
ref <- newRef ""
let
action n s = parallel do
delay (Milliseconds n)
_ <- modifyRef ref (_ <> s)
pure s
{ r1, r2, r3 } <- sequential $
{ r1: _, r2: _, r3: _ }
<$> action 10.0 "a"
<*>
( action 15.0 "a"
<|> action 12.0 "b"
<|> action 16.0 "c"
)
<*>
( action 15.0 "a"
<|> ((<>) <$> action 13.0 "d" <*> action 14.0 "e")
<|> action 16.0 "f"
)
delay (Milliseconds 20.0)
r4 <- readRef ref
pure (r1 == "a" && r2 == "b" && r3 == "de" && r4 == "abde")
test_kill_parallel_alt :: Aff Unit
test_kill_parallel_alt = assert "kill/parallel/alt" do
ref <- newRef ""
let
action n s = do
bracket
(pure unit)
(\_ -> void $ modifyRef ref (_ <> "killed" <> s))
( \_ -> do
delay (Milliseconds n)
void $ modifyRef ref (_ <> s)
)
f1 <- forkAff $ sequential $
parallel (action 10.0 "foo") <|> parallel (action 20.0 "bar")
f2 <- forkAff do
delay (Milliseconds 5.0)
killFiber (error "Nope") f1
modifyRef ref (_ <> "done")
_ <- try $ joinFiber f1
_ <- try $ joinFiber f2
eq "killedfookilledbardone" <$> readRef ref
test_kill_parallel_alt_finalizer :: Aff Unit
test_kill_parallel_alt_finalizer = assert "kill/parallel/alt/finalizer" do
ref <- newRef ""
f1 <- forkAff $ sequential $
parallel (delay (Milliseconds 10.0)) <|> parallel do
bracket
(pure unit)
( \_ -> do
delay (Milliseconds 10.0)
void $ modifyRef ref (_ <> "killed")
)
(\_ -> delay (Milliseconds 20.0))
f2 <- forkAff do
delay (Milliseconds 15.0)
killFiber (error "Nope") f1
modifyRef ref (_ <> "done")
_ <- try $ joinFiber f1
_ <- try $ joinFiber f2
eq "killeddone" <$> readRef ref
test_fiber_map :: Aff Unit
test_fiber_map = assert "fiber/map" do
ref <- newRef 0
let
mapFn a = unsafePerformEffect do
_ <- Ref.modify (_ + 1) ref
pure (a + 1)
f1 <- forkAff do
delay (Milliseconds 10.0)
pure 10
let
f2 = mapFn <$> f1
a <- joinFiber f2
b <- joinFiber f2
n <- readRef ref
pure (a == 11 && b == 11 && n == 1)
test_fiber_apply :: Aff Unit
test_fiber_apply = assert "fiber/apply" do
ref <- newRef 0
let
applyFn a b = unsafePerformEffect do
_ <- Ref.modify (_ + 1) ref
pure (a + b)
f1 <- forkAff do
delay (Milliseconds 10.0)
pure 10
f2 <- forkAff do
delay (Milliseconds 15.0)
pure 12
let
f3 = applyFn <$> f1 <*> f2
a <- joinFiber f3
b <- joinFiber f3
n <- readRef ref
pure (a == 22 && b == 22 && n == 1)
test_efffn :: Aff Unit
test_efffn = assert "efffn" do
ref <- newRef ""
let
effectDelay ms = AC.fromEffectFnAff $ AC.EffectFnAff $ AC.mkEffectFn2 \ke kc -> do
fiber <- runAff (either (AC.runEffectFn1 ke) (AC.runEffectFn1 kc)) (delay ms)
pure $ AC.EffectFnCanceler $ AC.mkEffectFn3 \e cke ckc -> do
runAff_ (either (AC.runEffectFn1 cke) (AC.runEffectFn1 ckc)) (killFiber e fiber)
action = do
effectDelay (Milliseconds 10.0)
void $ modifyRef ref (_ <> "done")
_ <- forkAff action
f2 <- forkAff action
killFiber (error "Nope.") f2
delay (Milliseconds 20.0)
eq "done" <$> readRef ref
test_parallel_stack :: Aff Unit
test_parallel_stack = assert "parallel/stack" do
ref <- newRef 0
parTraverse_ (modifyRef ref <<< add) (Array.replicate 100000 1)
eq 100000 <$> readRef ref
test_scheduler_size :: Aff Unit
test_scheduler_size = assert "scheduler" do
ref <- newRef 0
_ <- traverse joinFiber =<< traverse forkAff (Array.replicate 100000 (modifyRef ref (add 1)))
eq 100000 <$> readRef ref
test_lazy :: Aff Unit
test_lazy = assert "Lazy Aff" do
ref <- newRef 0
fix \loop -> do
val <- readRef ref
if val < 10 then do
writeRef ref (val + 1)
loop
else
pure unit
eq 10 <$> readRef ref
test_regression_return_fork :: Aff Unit
test_regression_return_fork = assert "regression/return-fork" do
bracket
(forkAff (pure unit))
(const (pure unit))
(const (pure true))
test_regression_par_apply_async_canceler :: Aff Unit
test_regression_par_apply_async_canceler = assert "regression/par-apply-async-canceler" do
ref <- newRef ""
let
action1 = makeAff \_ ->
pure $ Canceler \_ -> do
delay (Milliseconds 10.0)
void $ modifyRef ref (_ <> "done")
action2 = do
delay (Milliseconds 5.0)
void $ modifyRef ref (_ <> "throw")
throwError (error "Nope.")
catchError
(sequential (parallel action1 *> parallel action2))
\err -> do
val <- readRef ref
pure (val == "throwdone" && message err == "Nope.")
test_regression_bracket_catch_cleanup :: Aff Unit
test_regression_bracket_catch_cleanup = assert "regression/bracket-catch-cleanup" do
res :: Either Error Unit <-
try $ bracket
(pure unit)
(\_ -> catchError (pure unit) (const (pure unit)))
(\_ -> throwError (error "Nope."))
pure $ lmap message res == Left "Nope."
test_regression_kill_sync_async :: Aff Unit
test_regression_kill_sync_async = assert "regression/kill-sync-async" do
_ <- newRef ""
f1 <- forkAff $ makeAff \k -> k (Left (error "Boom.")) *> mempty
killFiber (error "Nope.") f1
pure true
test_regression_bracket_kill_mask :: Aff Unit
test_regression_bracket_kill_mask = assert "regression/kill-bracket-mask" do
ref <- newRef ""
let
action s = do
_ <- modifyRef ref (_ <> s)
pure unit
fiber <- forkAff do
bracket
do
action "a"
bracket
(pure unit)
(const (pure unit))
(\_ -> delay (Milliseconds 10.0))
action "b"
(const (pure unit))
(\_ -> delay (Milliseconds 10.0))
delay (Milliseconds 5.0)
killFiber (error "nope") fiber
readRef ref <#> eq "ab"
test_regression_kill_empty_supervisor :: Aff Unit
test_regression_kill_empty_supervisor = assert "regression/kill-empty-supervisor" do
f1 <- forkAff $ supervise $ delay $ Milliseconds 10.0
let
a = parallel $ killFiber (error "Nope.") f1 $> true
b = parallel $ delay (Milliseconds 20.0) $> false
sequential (a <|> b)
main :: Effect Unit
main = do
test_pure
test_bind
test_try
test_throw
test_liftEffect
void $ launchAff do
test_delay
test_fork
test_join
test_join_throw
test_join_throw_sync
test_multi_join
test_suspend
test_makeAff
test_bracket
test_bracket_nested
test_general_bracket
test_supervise
test_kill
test_kill_canceler
test_kill_bracket
test_kill_bracket_nested
test_kill_general_bracket_nested
test_kill_supervise
test_kill_finalizer_catch
test_kill_finalizer_bracket
test_parallel
test_parallel_throw
test_kill_parallel
test_parallel_alt
test_parallel_alt_throw
test_parallel_alt_sync
test_parallel_mixed
test_kill_parallel_alt
test_kill_parallel_alt_finalizer
test_lazy
test_efffn
test_fiber_map
test_fiber_apply
-- Turn on if we decide to schedule forks
-- test_scheduler_size
test_parallel_stack
test_regression_return_fork
test_regression_par_apply_async_canceler
test_regression_bracket_catch_cleanup
test_regression_kill_sync_async
test_regression_bracket_kill_mask
test_regression_kill_empty_supervisor