File: erlang_actor_spec.rb

package info (click to toggle)
ruby-concurrent 1.3.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,136 kB
  • sloc: ruby: 30,875; java: 6,128; ansic: 265; makefile: 26; sh: 19
file content (1031 lines) | stat: -rw-r--r-- 40,857 bytes parent folder | download
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
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
require 'concurrent/edge/erlang_actor'

RSpec.describe 'Concurrent' do
  describe 'ErlangActor', edge: true do
    # TODO (pitr-ch 06-Feb-2019): include constants instead
    ANY      ||= Concurrent::ErlangActor::ANY
    TIMEOUT  ||= Concurrent::ErlangActor::TIMEOUT
    And      ||= Concurrent::ErlangActor::And
    identity = -> v { v }

    shared_examples 'erlang actor' do

      specify "run to termination" do
        expect(Concurrent::ErlangActor.spawn(type: type) do
          :v
        end.terminated.value!).to eq :v
      end

      specify "run to termination with arguments" do
        expect(Concurrent::ErlangActor.
            spawn(1, 2, type: type) { |a, b| a + b }.terminated.value!).
            to eq 3
      end

      specify '#receive' do
        succ = -> v { v.succ }

        [[[:v], -> { receive }, :v],
         [[:v], -> { receive on(ANY, &identity) }, :v],
         [[:v, 1], -> { receive Numeric }, 1],
         [[:v, 1], -> { receive(Numeric, &succ) }, 2],

         [[:v], -> { receive Numeric, timeout: 0 }, nil],
         [[:v], -> { receive(Numeric, timeout: 0, &succ) }, nil],
         [[:v], -> { receive Numeric, timeout: 0, timeout_value: :timeout }, :timeout],
         [[:v], -> { receive(Numeric, timeout: 0, timeout_value: :timeout, &succ) }, :timeout],

         [[:v, 1], -> { receive Numeric, timeout: 1 }, 1],
         [[:v, 1], -> { receive(Numeric, timeout: 1, &succ) }, 2],
         [[:v, 1], -> { receive Numeric, timeout: 1, timeout_value: :timeout }, 1],
         [[:v, 1], -> { receive(Numeric, timeout: 1, timeout_value: :timeout, &succ) }, 2],

         [[:v], -> { receive on(Numeric, &identity), on(TIMEOUT, nil), timeout: 0 }, nil],
         [[:v], -> { receive on(Numeric, &succ), on(TIMEOUT, nil), timeout: 0 }, nil],
         [[:v], -> { receive on(Numeric, &identity), on(TIMEOUT, :timeout), timeout: 0 }, :timeout],
         [[:v], -> { receive on(Numeric, &succ), on(TIMEOUT, :timeout), timeout: 0 }, :timeout],

         [[:v, 1], -> { receive on(Numeric, &identity), on(TIMEOUT, nil), timeout: 1 }, 1],
         [[:v, 1], -> { receive on(Numeric, &succ), on(TIMEOUT, nil), timeout: 1 }, 2],
         [[:v, 1], -> { receive on(Numeric, &identity), on(TIMEOUT, :timeout), timeout: 1 }, 1],
         [[:v, 1], -> { receive on(Numeric, &succ), on(TIMEOUT, :timeout), timeout: 1 }, 2],
        ].each_with_index do |(messages, body, result), i|
          a = Concurrent::ErlangActor.spawn(type: type, &body)
          messages.each { |m| a.tell m }
          expect(a.terminated.value!).to eq(result), "body: #{body}"
        end
      end

      specify 'pid has name' do
        actor = Concurrent::ErlangActor.spawn(type: type, name: 'test') {}
        expect(actor.to_s).to match(/test/)
        expect(actor.inspect).to match(/test/)
      end

      specify "receives message" do
        actor = Concurrent::ErlangActor.spawn(type: type,
                                              &{ on_thread: -> { receive },
                                                 on_pool:   -> { receive on(ANY, &identity) } }.fetch(type))
        actor.tell :v
        expect(actor.terminated.value!).to eq :v
      end

      specify "receives message with matchers" do
        body  = { on_thread:
                      -> do
                        [receive(on(Symbol, &identity)),
                         receive(on(Numeric, &:succ)),
                         receive(on(Numeric, :got_it), timeout: 0, timeout_value: :nothing)]
                      end,
                  on_pool:
                      -> do
                        @arr = []
                        receive(on(Symbol) do |v1|
                          @arr.push v1
                          receive(on(Numeric) do |v2|
                            @arr << v2.succ
                            receive(on(Numeric, :got_it), on(TIMEOUT) { @arr << :nothing; @arr }, timeout: 0)
                          end)
                        end)
                      end }
        actor = Concurrent::ErlangActor.spawn(type: type, &body.fetch(type))
        actor.tell 'junk'
        actor.tell 1
        actor.tell :v
        expect(actor.terminated.value!).to eq [:v, 2, :nothing]
      end

      describe "monitoring" do
        specify "(de)monitor" do
          body_receive = { on_thread:
                               -> { receive },
                           on_pool:
                               -> { receive { |v| v } } }

          body = { on_thread:
                       -> do
                         actor     = receive
                         reference = monitor actor
                         monitored = monitoring? reference
                         demonitor reference
                         result = [monitored, monitoring?(reference)]
                         actor.tell :finish
                         result
                       end,
                   on_pool:
                       -> do
                         receive do |actor|
                           reference = monitor actor
                           monitored = monitoring? reference
                           demonitor reference
                           result = [monitored, monitoring?(reference)]
                           actor.tell :finish
                           result
                         end
                       end }
          a1   = Concurrent::ErlangActor.spawn(type: type, &body.fetch(type))
          a2   = Concurrent::ErlangActor.spawn(type: type, &body_receive.fetch(type))
          a1.tell a2
          expect(a1.terminated.value!).to eq [true, false]
          expect(a2.terminated.value!).to eq :finish
        end

        specify "demonitor" do
          body = { on_thread:
                       -> do
                         actor     = receive
                         reference = monitor actor
                         monitored = monitoring? reference
                         actor.tell :done
                         actor.terminated.wait
                         demonitor = demonitor reference, :flush, :info
                         [monitored, monitoring?(reference), demonitor, receive(timeout: 0)]
                       end,
                   on_pool:
                       -> do
                         receive do |actor|
                           reference = monitor actor
                           monitored = monitoring? reference
                           actor.tell :done
                           actor.terminated.wait
                           demonitor = demonitor reference, :flush, :info
                           results   = [monitored, monitoring?(reference), demonitor]
                           receive(on(ANY) { |v| [*results, v] },
                                   on(TIMEOUT) { [*results, nil] },
                                   timeout: 0)
                         end
                       end }

          a1   = Concurrent::ErlangActor.spawn(type: type, &body.fetch(type))
          body = { on_thread: -> { receive },
                   on_pool:   -> { receive(&identity) } }
          a2   = Concurrent::ErlangActor.spawn(type: type, &body.fetch(type))
          a1.tell a2

          a1.terminated.wait
          expect(a1.terminated.value!).to eq [true, false, false, nil]
          expect(a2.terminated.value!).to eq :done
        end

        specify "demonitor should leave the down message in the inbox if it's already there" do
          body = { on_thread:
                       -> do
                         actor     = receive
                         reference = monitor actor
                         monitored = monitoring? reference
                         actor.tell :done
                         actor.terminated.wait
                         demonitor = demonitor reference, :info
                         [reference, monitored, monitoring?(reference), demonitor, receive(timeout: 0)]
                       end,
                   on_pool:
                       -> do
                         receive do |actor|
                           reference = monitor actor
                           monitored = monitoring? reference
                           actor.tell :done
                           actor.terminated.wait
                           demonitor = demonitor reference, :info
                           results   = [reference, monitored, monitoring?(reference), demonitor]
                           receive(on(ANY) { |v| [*results, v] },
                                   on(TIMEOUT) { [*results, nil] },
                                   timeout: 0)
                         end
                       end }

          a1   = Concurrent::ErlangActor.spawn(type: type, &body.fetch(type))
          body = { on_thread: -> { receive },
                   on_pool:   -> { receive(&identity) } }
          a2   = Concurrent::ErlangActor.spawn(type: type, &body.fetch(type))
          a1.tell a2

          reference, monitored, monitoring, demonitor, message = a1.terminated.value!
          expect(monitored).to eq true
          expect(monitoring).to eq false
          expect(demonitor).to eq false
          expect(message).to eq Concurrent::ErlangActor::Down.new(a2, reference, :normal)
          expect(a2.terminated.value!).to eq :done
        end

        specify "notifications 1" do
          body = { on_thread:
                       -> do
                         b   = spawn { [:done, receive] }
                         ref = monitor b
                         b.tell 42
                         [b, ref, receive]
                       end,
                   on_pool:
                       -> do
                         b   = spawn { receive on(ANY) { |v| [:done, v] } }
                         ref = monitor b
                         b.tell 42
                         receive on(ANY) { |v| [b, ref, v] }
                       end }

          a = Concurrent::ErlangActor.spawn(type: type, &body.fetch(type))

          b, ref, down = a.terminated.value!
          expect(down).to eq Concurrent::ErlangActor::Down.new(b, ref, :normal)
          expect(b.terminated.value!).to eq [:done, 42]
        end

        specify "notifications 2" do
          body = { on_thread:
                       -> do
                         b = spawn { :done }
                         b.terminated.wait
                         ref = monitor b
                         [b, ref, receive(timeout: 1, timeout_value: :timeout)]
                       end,
                   on_pool:
                       -> do
                         b = spawn { :done }
                         b.terminated.wait
                         ref = monitor b
                         receive(timeout: 1) { |v| [b, ref, v] }
                       end }

          a = Concurrent::ErlangActor.spawn(type: type, &body.fetch(type))

          b, ref, down = a.terminated.value!
          expect(down).to eq Concurrent::ErlangActor::Down.new(b, ref, Concurrent::ErlangActor::NoActor.new(b))
          expect(b.terminated.value!).to eq :done
        end

        # FIXME (pitr-ch 20-Jan-2019): test concurrent exit and monitor(), same for link
      end

      describe 'linking' do
        body_receive_test_linked = { on_thread:
                                         -> { linked?(receive) },
                                     on_pool:
                                         -> { receive { |a| linked? a } } }

        specify 'links' do
          body1 = { on_thread:
                        -> do
                          actor = receive
                          link actor
                          linked = linked? actor
                          actor.tell pid
                          linked
                        end,
                    on_pool:
                        -> do
                          receive do |actor|
                            link actor
                            linked = linked? actor
                            actor.tell pid
                            linked
                          end
                        end }

          a1 = Concurrent::ErlangActor.spawn(type: type, &body1.fetch(type))
          a2 = Concurrent::ErlangActor.spawn(type: type, &body_receive_test_linked.fetch(type))

          a1.tell a2
          expect(a1.terminated.value!).to be_truthy
          expect(a2.terminated.value!).to be_truthy
        end

        specify 'unlinks' do
          body1 = { on_thread:
                        -> do
                          actor = receive
                          link actor
                          unlink actor
                          linked = linked? actor
                          actor.tell pid
                          linked
                        end,
                    on_pool:
                        -> do
                          receive do |actor|
                            link actor
                            unlink actor
                            linked = linked? actor
                            actor.tell pid
                            linked
                          end
                        end }

          a1 = Concurrent::ErlangActor.spawn(type: type, &body1.fetch(type))
          a2 = Concurrent::ErlangActor.spawn(type: type, &body_receive_test_linked.fetch(type))
          a1.tell a2
          expect(a1.terminated.value!).to be_falsey
          expect(a2.terminated.value!).to be_falsey
        end

        specify 'link dead' do
          a = Concurrent::ErlangActor.spawn(type: type) do
            b = spawn { :done }
            b.terminated.wait
            link b
          end
          expect { a.terminated.value! }.to raise_error Concurrent::ErlangActor::NoActor
        end

        specify 'link dead when trapping' do
          body1 = { on_thread:
                        -> do
                          b = spawn { :done }
                          b.terminated.wait
                          sleep 0.1
                          trap
                          link b
                          [b, receive]
                        end,
                    on_pool:
                        -> do
                          b = spawn { :done }
                          b.terminated.wait
                          sleep 0.1
                          trap
                          link b
                          receive { |v| [b, v] }
                        end }

          a = Concurrent::ErlangActor.spawn(type: type, &body1.fetch(type))

          b, captured = a.terminated.value!
          expect(captured).to eq Concurrent::ErlangActor::Terminated.new(b, Concurrent::ErlangActor::NoActor.new(b))
        end


        describe 'exit/1 when linked' do
          # https://learnyousomeerlang.com/errors-and-processes#links
          specify 1 do
            body = { on_thread:
                         -> do
                           b = spawn(link: true) { :ok }
                           [receive(timeout: 0.01), b]
                         end,
                     on_pool:
                         -> do
                           b = spawn(link: true) { :ok }
                           receive(on(ANY) { |v| [v, b] },
                                   on(TIMEOUT) { |v| [nil, b] },
                                   timeout: 0.01)
                         end }

            a = Concurrent::ErlangActor.spawn(type: type, &body.fetch(type))

            message, b = a.terminated.value!
            expect(message).to eq nil
            expect(b.terminated.value!).to eq :ok
          end

          specify 2 do
            body = { on_thread:
                         -> do
                           b = spawn(link: true) { :ok }
                           trap
                           [receive(timeout: 1), b]
                         end,
                     on_pool:
                         -> do
                           b = spawn(link: true) { :ok }
                           trap
                           receive(on(ANY) { |v| [v, b] },
                                   on(TIMEOUT) { |v| [nil, b] },
                                   timeout: 1)
                         end }

            a = Concurrent::ErlangActor.spawn(type: type, &body.fetch(type))

            message, b = a.terminated.value!
            expect(message).to eq Concurrent::ErlangActor::Terminated.new(b, :normal)
            expect(b.terminated.value!).to eq :ok
          end

          specify 3 do
            body = { on_thread:
                         -> do
                           spawn(link: true) { terminate :boom }
                           receive(timeout: 1)
                         end,
                     on_pool:
                         -> do
                           spawn(link: true) { terminate :boom }
                           receive(on(ANY) { |v| [v, b] },
                                   on(TIMEOUT) { |v| [nil, b] },
                                   timeout: 1)
                         end }

            a = Concurrent::ErlangActor.spawn(type: type, &body.fetch(type))

            expect(a.terminated.reason).to eq :boom
          end

          specify 4 do
            body = { on_thread:
                         -> do
                           b = spawn(link: true) { terminate :boom }
                           trap
                           [receive(timeout: 1), b]
                         end,
                     on_pool:
                         -> do
                           b = spawn(link: true) { terminate :boom }
                           trap
                           receive(on(ANY) { |v| [v, b] },
                                   on(TIMEOUT) { |v| [nil, b] },
                                   timeout: 1)
                         end }

            a = Concurrent::ErlangActor.spawn(type: type, &body.fetch(type))

            trapped_exit, b = a.terminated.value!
            expect(trapped_exit).to eq Concurrent::ErlangActor::Terminated.new(b, :boom)
            expect(b.terminated.reason).to eq :boom
          end

          specify 5 do
            body = { on_thread:
                         -> do
                           b = spawn(link: true) { terminate :normal, value: :ok }
                           [receive(timeout: 0.01), b]
                         end,
                     on_pool:
                         -> do
                           b = spawn(link: true) { terminate :normal, value: :ok }
                           receive(on(ANY) { |v| [v, b] },
                                   on(TIMEOUT) { |v| [nil, b] },
                                   timeout: 0.01)
                         end }

            a = Concurrent::ErlangActor.spawn(type: type, &body.fetch(type))

            message, b = a.terminated.value!
            expect(message).to eq nil
            expect(b.terminated.value!).to eq :ok
          end

          specify 6 do
            body = { on_thread:
                         -> do
                           b = spawn(link: true) { terminate :normal, value: :ok }
                           trap
                           [receive(timeout: 1), b]
                         end,
                     on_pool:
                         -> do
                           b = spawn(link: true) { terminate :normal, value: :ok }
                           trap
                           receive(on(ANY) { |v| [v, b] },
                                   on(TIMEOUT) { |v| [nil, b] },
                                   timeout: 1)
                         end }

            a = Concurrent::ErlangActor.spawn(type: type, &body.fetch(type))

            message, b = a.terminated.value!
            expect(message).to eq Concurrent::ErlangActor::Terminated.new(b, :normal)
            expect(b.terminated.value!).to eq :ok
          end

          specify 7 do
            body = { on_thread:
                         -> do
                           spawn(link: true) { raise 'err' }
                           receive(timeout: 1)
                         end,
                     on_pool:
                         -> do
                           spawn(link: true) { raise 'err' }
                           receive(timeout: 1) { |v| v }
                         end }

            a = Concurrent::ErlangActor.spawn(type: type, &body.fetch(type))

            expect { a.terminated.value! }.to raise_error(RuntimeError, 'err')
          end

          specify 8 do
            body = { on_thread:
                         -> do
                           b = spawn(link: true) { raise 'err' }
                           trap
                           [receive(timeout: 1), b]
                         end,
                     on_pool:
                         -> do
                           b = spawn(link: true) { raise 'err' }
                           trap
                           receive(on(ANY) { |v| [v, b] },
                                   on(TIMEOUT) { |v| [nil, b] },
                                   timeout: 1)
                         end }

            a = Concurrent::ErlangActor.spawn(type: type, &body.fetch(type))

            trapped_exit, b = a.terminated.value!
            expect(trapped_exit).to be_a Concurrent::ErlangActor::Terminated
            expect(trapped_exit.from).to eq b
            expect(trapped_exit.reason).to eq b.terminated.reason
            expect(trapped_exit.reason).to be_a RuntimeError
            expect(trapped_exit.reason.message).to eq 'err'
          end

          specify 9 do
            body = { on_thread:
                         -> do
                           b = spawn(link: true) { throw :uncaught }
                           trap
                           [receive(timeout: 1), b]
                         end,
                     on_pool:
                         -> do
                           b = spawn(link: true) { throw :uncaught }
                           trap
                           receive(on(ANY) { |v| [v, b] },
                                   on(TIMEOUT) { |v| [nil, b] },
                                   timeout: 1)
                         end }

            a = Concurrent::ErlangActor.spawn(type: type, &body.fetch(type))

            trapped_exit, b = a.terminated.value!
            expect(trapped_exit).to be_a Concurrent::ErlangActor::Terminated
            expect(trapped_exit.from).to eq b
            expect(trapped_exit.reason).to eq b.terminated.reason
            expect(trapped_exit.reason).to be_a ArgumentError
            expect(trapped_exit.reason.message).to match(/uncaught throw :uncaught/)
          end
        end

        describe 'exit/2 when linked' do
          # https://learnyousomeerlang.com/errors-and-processes#links
          specify 1 do
            body = { on_thread:
                         -> do
                           terminate pid, :normal # sends the signal to mailbox
                           # TODO (pitr-ch 17-Jan-2019): does erlang require receive to process signals?
                           receive(timeout: 0.01)
                           :continued
                         end,
                     on_pool:
                         -> do
                           terminate pid, :normal # sends the signal to mailbox
                           receive(on(ANY, :continued),
                                   on(TIMEOUT, :timeout),
                                   timeout: 0.01)
                         end }

            a = Concurrent::ErlangActor.spawn(type: type, &body.fetch(type))

            expect(a.terminated.value!).to eq nil
          end

          specify 2 do
            body = { on_thread:
                         -> do
                           terminate pid, :normal
                           trap
                           receive(timeout: 0)
                         end,
                     on_pool:
                         -> do
                           terminate pid, :normal
                           trap
                           receive(on(ANY, &identity), on(TIMEOUT, nil), timeout: 0)
                         end }

            a = Concurrent::ErlangActor.spawn(type: type, &body.fetch(type))

            captured_exit = a.terminated.value!
            expect(captured_exit).to eq Concurrent::ErlangActor::Terminated.new(a, :normal)
          end

          specify 3 do
            body = { on_thread:
                         -> do
                           b = spawn(link: true) { receive timeout: 0.01, timeout_value: :timeout }
                           terminate b, :normal
                           b
                         end,
                     on_pool:
                         -> do
                           b = spawn(link: true) do
                             receive(on(ANY, :not_happening),
                                     on(TIMEOUT, :timeout),
                                     timeout: 0.01)
                           end

                           terminate b, :normal
                           b
                         end }
            a    = Concurrent::ErlangActor.spawn(type: type, &body.fetch(type))

            b = a.terminated.value!
            expect(b.terminated.value!).to eq :timeout
          end

          specify 4 do
            body = { on_thread:
                         -> do
                           b = spawn(link: true) { trap; receive timeout: 1, timeout_value: :timeout }
                           terminate b, :normal
                           b
                         end,
                     on_pool:
                         -> do
                           b = spawn(link: true) do
                             trap
                             receive(on(ANY, &identity),
                                     on(TIMEOUT, :timeout),
                                     timeout: 1)
                           end

                           terminate b, :normal
                           b
                         end }
            a    = Concurrent::ErlangActor.spawn(type: type, &body.fetch(type))

            b = a.terminated.value!
            expect(b.terminated.value!).to eq Concurrent::ErlangActor::Terminated.new(a, :normal)
          end

          specify 5 do
            body = { on_thread:
                         -> do
                           b = spawn(link: true) { receive timeout: 0.01; terminate :continued }
                           terminate b, :normal
                           trap
                           [b, receive(timeout: 1)]
                         end,
                     on_pool:
                         -> do
                           b = spawn(link: true) do
                             receive(on(ANY, :not_happening),
                                     on(TIMEOUT) { terminate :continued },
                                     timeout: 0.01)
                           end

                           terminate b, :normal
                           trap
                           receive(on(ANY) { |v| [b, v] }, on(TIMEOUT, :timeout), timeout: 1)
                         end }

            a = Concurrent::ErlangActor.spawn(type: type, &body.fetch(type))

            b, captured = a.terminated.value!
            expect(b.terminated.reason).to eq :continued
            # normal is never send from b to a back
            expect(captured).to eq Concurrent::ErlangActor::Terminated.new(b, :continued)
          end

          specify 6 do
            body = { on_thread:
                         -> do
                           b = spawn(link: true) { receive timeout: 1; :done }
                           terminate b, :remote_err
                           receive timeout: 1
                         end,
                     on_pool:
                         -> do
                           b = spawn(link: true) { receive(on(ANY, :done), on(TIMEOUT, :timeout), timeout: 1) }
                           terminate b, :remote_err
                           receive(on(ANY) { |v| [b, v] },
                                   on(TIMEOUT, :timeout),
                                   timeout: 1)
                         end }

            a = Concurrent::ErlangActor.spawn(type: type, &body.fetch(type))
            a.terminated.wait
            expect(a.terminated.reason).to eq :remote_err
          end

          specify 7 do
            body = { on_thread:
                         -> do
                           b = spawn(link: true) { receive timeout: 1; :done }
                           terminate b, :remote_err
                           trap
                           [b, receive(timeout: 1)]
                         end,
                     on_pool:
                         -> do
                           b = spawn(link: true) { receive(on(ANY, :done), on(TIMEOUT, :timeout), timeout: 1) }
                           terminate b, :remote_err
                           trap
                           receive(on(ANY) { |v| [b, v] },
                                   on(TIMEOUT, :timeout),
                                   timeout: 1)
                         end }

            a = Concurrent::ErlangActor.spawn(type: type, &body.fetch(type))

            b, captured = a.terminated.value!
            expect(b.terminated.reason).to eq :remote_err
            expect(captured.reason).to eq :remote_err
          end

          specify 8 do
            body = { on_thread:
                         -> do
                           b = spawn(link: true) { receive timeout: 1; :done }
                           terminate b, :kill
                           receive timeout: 1
                         end,
                     on_pool:
                         -> do
                           b = spawn(link: true) { receive(on(ANY, :done), on(TIMEOUT, :done), timeout: 1) }
                           terminate b, :kill
                           receive(on(ANY, &identity), on(TIMEOUT, :timeout), timeout: 1)
                         end }

            a = Concurrent::ErlangActor.spawn(type: type, &body.fetch(type))

            expect(a.terminated.reason).to eq :killed
          end

          specify 9 do
            body = { on_thread:
                         -> do
                           b = spawn(link: true) { receive timeout: 1; :done }
                           terminate b, :kill
                           trap
                           [b, receive(timeout: 1)]
                         end,
                     on_pool:
                         -> do
                           b = spawn(link: true) { receive(on(ANY, :done), on(TIMEOUT, :done), timeout: 1) }
                           terminate b, :kill
                           trap
                           receive(on(ANY) { |v| [b, v] }, on(TIMEOUT, :timeout), timeout: 1)
                         end }

            a           = Concurrent::ErlangActor.spawn(type: type, &body.fetch(type))
            b, captured = a.terminated.value!
            expect(b.terminated.reason).to eq :killed
            expect(captured.reason).to eq :killed
          end

          specify 10 do
            body = { on_thread:
                         -> do
                           terminate pid, :kill
                           receive timeout: 0
                         end,
                     on_pool:
                         -> do
                           terminate pid, :kill
                           receive(on(ANY, :continued), on(TIMEOUT, :timeout), timeout: 0)
                         end }

            a = Concurrent::ErlangActor.spawn(type: type, &body.fetch(type))

            expect(a.terminated.reason).to eq :killed
          end

          specify 11 do
            body = { on_thread:
                         -> do
                           terminate pid, :kill
                           trap
                           receive timeout: 0
                         end,
                     on_pool:
                         -> do
                           terminate pid, :kill
                           trap
                           receive(on(ANY, &identity), on(TIMEOUT, :timeout), timeout: 0)
                         end }

            a = Concurrent::ErlangActor.spawn(type: type, &body.fetch(type))

            expect(a.terminated.reason).to eq :killed
          end

          # explained in
          # http://erlang.org/pipermail/erlang-questions/2009-October/047241.html

          specify 12 do
            body = { on_thread:
                         -> do
                           spawn(link: true) { terminate :kill }
                           receive timeout: 1
                         end,
                     on_pool:
                         -> do
                           spawn(link: true) { terminate :kill }
                           receive(on(ANY, :continued),
                                   on(TIMEOUT, :timeout),
                                   timeout: 1)
                         end }

            a = Concurrent::ErlangActor.spawn(type: type, &body.fetch(type))

            expect(a.terminated.reason).to eq :kill
          end

          specify 13 do
            body = { on_thread:
                         -> do
                           b = spawn(link: true) { terminate :kill }
                           trap
                           [b, receive(timeout: 1)]
                         end,
                     on_pool:
                         -> do
                           b = spawn(link: true) { terminate :kill }
                           trap
                           receive(on(ANY) { |v| [b, v] },
                                   on(TIMEOUT, :timeout),
                                   timeout: 1)
                         end }

            a = Concurrent::ErlangActor.spawn(type: type, &body.fetch(type))

            b, captured = a.terminated.value!

            expect(b.terminated.reason).to eq :kill
            expect(captured).to eq Concurrent::ErlangActor::Terminated.new(b, :kill)
          end

        end
      end

      specify 'spawn(link: true)' do
        a = Concurrent::ErlangActor.spawn(type: type) do
          b = spawn(link: true) { :v }
          linked? b
        end
        expect(a.terminated.value!).to be_truthy

        a = Concurrent::ErlangActor.spawn(type: type) do
          b = spawn { :v }
          linked? b
        end
        expect(a.terminated.value!).to be_falsey
      end

      specify 'termination' do
        a = Concurrent::ErlangActor.spawn(type: type) { :v }
        expect(a.terminated.value!).to eq :v

        a = Concurrent::ErlangActor.spawn(type: type) { raise 'err' }
        expect { a.terminated.value! }.to raise_error(RuntimeError, 'err')

        a = Concurrent::ErlangActor.spawn(type: type) { terminate :normal, value: :val }
        expect(a.terminated.value!).to eq :val

        a = Concurrent::ErlangActor.spawn(type: type) { terminate :er }
        expect(a.terminated.reason).to eq :er
      end

      describe 'asking' do
        specify "replies" do
          body = { on_thread: -> { reply receive },
                   on_pool:   -> { receive { |v| reply v } } }
          a    = Concurrent::ErlangActor.spawn(type: type, &body.fetch(type))
          expect(a.ask(:v)).to eq :v

          body = { on_thread: -> { v = receive; reply v; reply v; },
                   on_pool:   -> { receive { |v| reply v; reply v } } }
          a    = Concurrent::ErlangActor.spawn(type: type, &body.fetch(type))
          expect(a.ask(:v)).to eq :v
          expect(a.terminated.value!).to be_falsey

          body = { on_thread:
                       -> do
                         v = receive
                         reply v
                         reply_resolution true, v.to_s, nil
                       end,
                   on_pool:
                       -> do
                         receive do |v|
                           reply v
                           reply_resolution true, v.to_s, nil
                         end
                       end }
          a    = Concurrent::ErlangActor.spawn(type: type, &body.fetch(type))
          expect(a.ask(:v)).to eq :v
          expect(a.terminated.value!).to be_falsey

          body = { on_thread: -> { reply_resolution false, nil, receive },
                   on_pool:   -> { receive { |v| reply_resolution false, nil, v } } }
          a    = Concurrent::ErlangActor.spawn(type: type, &body.fetch(type))
          expect { a.ask(:err) }.to raise_error StandardError, 'err'

          body = { on_thread: -> { reply_resolution false, nil, receive },
                   on_pool:   -> { receive { |v| reply_resolution false, nil, v } } }
          a    = Concurrent::ErlangActor.spawn(type: type, &body.fetch(type))
          expect(a.ask_op(:err).reason).to eq :err
        end

        specify "timing out" do
          skip('flaky on truffleruby and jruby') if Concurrent.on_truffleruby? || Concurrent.on_jruby?

          count_down = Concurrent::CountDownLatch.new
          body = { on_thread: -> { m = receive; count_down.wait; reply m },
                   on_pool:   -> { receive { |m| count_down.wait; reply m } } }
          a    = Concurrent::ErlangActor.spawn(type: type, &body.fetch(type))
          expect(a.ask(:err, 0, 42)).to eq 42
          count_down.count_down
          expect(a.terminated.value!).to eq false

          body = { on_thread: -> { reply receive },
                   on_pool:   -> { receive { |m| reply m } } }
          b    = Concurrent::ErlangActor.spawn(type: type, &body.fetch(type))
          expect(b.ask(:v, 1, 42)).to eq :v
          expect(b.terminated.value!).to eq true
        end

        specify "rejects on no reply" do
          body = { on_thread: -> { receive; receive },
                   on_pool:   -> { receive { receive {} } } }

          a = Concurrent::ErlangActor.spawn(type: type, &body.fetch(type))
          expect(a.ask_op(:v).reason).to eq Concurrent::ErlangActor::NoReply
          expect { raise a.ask_op(:v).wait }.to raise_error Concurrent::ErlangActor::NoActor
          expect { raise a.ask(:v) }.to raise_error Concurrent::ErlangActor::NoActor
        end

      end
    end

    describe 'on thread' do
      let(:type) { :on_thread }
      it_behaves_like 'erlang actor'

      specify do
        actor = Concurrent::ErlangActor.spawn(type: :on_thread) do
          Thread.abort_on_exception = true
          while true
            receive on(Symbol) { |s| reply s.to_s },
                    on(And[Numeric, -> v { v >= 0 }]) { |v| reply v.succ },
                    # put last works as else
                    on(ANY) { |v| reply :bad_message; terminate [:bad_message, v] }
          end
        end
        expect(actor.ask(1)).to eq 2
        expect(actor.ask(:value)).to eq 'value'
        expect(actor.ask(-1)).to eq :bad_message
        expect { actor.ask 'junk' }.to raise_error Concurrent::ErlangActor::NoActor
        expect(actor.terminated.reason).to eq [:bad_message, -1]
      end
    end

    describe 'on pool' do
      let(:type) { :on_pool }
      it_behaves_like 'erlang actor'

      include Concurrent::ErlangActor::EnvironmentConstants

      specify "receives message repeatedly with keep" do
        actor = Concurrent::ErlangActor.spawn(type: :on_pool) do
          receive on(ANY) { |v| v == :done ? terminate(:normal, value: 42) : reply(v) },
                  keep: true
        end
        expect(actor.ask(1)).to eq 1
        expect(actor.ask(2)).to eq 2
        actor.tell :done
        expect(actor.terminated.value!).to eq 42
      end

      specify "class defined" do
        definition_module = Module.new do
          def start
            @sum = 0
            receive on(Numeric, &method(:count)),
                    on(:done, &method(:stop)),
                    on(TIMEOUT, &method(:fail)),
                    keep:    true,
                    timeout: 0.1
          end

          def count(message)
            reply @sum += message
          end

          def stop(_message)
            terminate :normal, value: @sum
          end

          def fail(_message)
            terminate :timeout
          end
        end
        definition_class  = Class.new Concurrent::ErlangActor::Environment do
          include definition_module
        end

        actor = Concurrent::ErlangActor.spawn(type: :on_pool, environment: definition_class) { start }
        actor.tell 1
        expect(actor.ask(2)).to eq 3
        actor.tell :done
        expect(actor.terminated.value!).to eq 3

        actor = Concurrent::ErlangActor.spawn(type: :on_pool, environment: definition_module)
        actor.tell 1
        expect(actor.ask(2)).to eq 3
        expect(actor.terminated.reason).to eq :timeout
      end

    end
  end
end