aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/control/concurrency/actor.lux
blob: 2767082b2dff9134f92790452a4677cc586a9458 (plain)
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
(.module:
  [library
   [lux "*"
    ["_" test {"+" [Test]}]
    [abstract
     [monad {"+" [do]}]]
    [control
     ["[0]" try {"+" [Try]}]
     ["[0]" exception {"+" [exception:]}]
     ["[0]" io {"+" [IO io]}]]
    [data
     [text
      ["%" format {"+" [format]}]]
     [collection
      ["[0]" list]
      ["[0]" row {"+" [Row]}]]]
    [math
     ["[0]" random]
     [number
      ["n" nat]]]]]
  [\\library
   ["[0]" / {"+" [actor: message:]}
    [//
     ["[0]" atom {"+" [Atom]}]
     ["[0]" async {"+" [Async Resolver]} ("[1]\[0]" monad)]
     ["[0]" frp]]]])

(exception: got_wrecked)

(actor: counter
  Nat

  ((on_mail message state self)
   (message state self))

  (message: (count! [increment Nat] state self)
    Nat
    (let [state' (n.+ increment state)]
      (async\in {#try.Success [state' state']})))
  )

(def: (mailed? outcome)
  (-> (Try Any) Bit)
  (case outcome
    {#try.Success _} true
    {#try.Failure _} false))

(def: .public test
  Test
  (do [! random.monad]
    [initial_state random.nat
     .let [as_mail (: (All (_ a) (-> (-> a a) (/.Mail a)))
                      (function (_ transform)
                        (function (_ state actor)
                          (|> state transform #try.Success async\in))))
           ++! (: (/.Mail Nat) (as_mail ++))
           --! (: (/.Mail Nat) (as_mail --))]]
    (<| (_.covering /._)
        (_.for [/.Actor])
        ($_ _.and
            (_.cover [/.alive?]
                     (io.run! (do io.monad
                                [actor (/.spawn! /.default 0)]
                                (/.alive? actor))))
            
            (_.cover [/.poison!]
                     (let [poisoned_actors_die!
                           (io.run! (do io.monad
                                      [actor (/.spawn! /.default 0)
                                       poisoned? (/.poison! actor)
                                       alive? (/.alive? actor)]
                                      (in (and (..mailed? poisoned?)
                                               (not alive?)))))

                           cannot_poison_more_than_once!
                           (io.run! (do io.monad
                                      [actor (/.spawn! /.default 0)
                                       first_time? (/.poison! actor)
                                       second_time? (/.poison! actor)]
                                      (in (and (..mailed? first_time?)
                                               (not (..mailed? second_time?))))))]
                       (and poisoned_actors_die!
                            cannot_poison_more_than_once!)))

            (let [[read write] (: [(Async Text) (Resolver Text)]
                                  (async.async []))]
              (in (do async.monad
                    [_ (async.future (do io.monad
                                       [actor (/.spawn! (: (/.Behavior Any Any)
                                                           [#/.on_init (|>>)
                                                            #/.on_mail (function (_ message state self)
                                                                         (do [! async.monad]
                                                                           [outcome (message state self)]
                                                                           (case outcome
                                                                             {#try.Failure cause}
                                                                             (do !
                                                                               [_ (async.future (write cause))]
                                                                               (in outcome))
                                                                             
                                                                             {#try.Success _}
                                                                             (in outcome))))])
                                                        [])]
                                       (/.poison! actor)))
                     _ (async.delay 100)
                     result (async.future (async.value read))]
                    (_.cover' [/.poisoned]
                              (case result
                                {#.Some error}
                                (exception.match? /.poisoned error)

                                #.None
                                false)))))

            (in (do async.monad
                  [sent? (async.future (do io.monad
                                         [actor (/.spawn! /.default 0)
                                          sent? (/.mail! ++! actor)]
                                         (in (..mailed? sent?))))]
                  (_.cover' [/.Behavior /.Mail
                             /.default /.spawn! /.mail!]
                            sent?)))

            (in (do async.monad
                  [result (async.future (do io.monad
                                          [counter (/.spawn! /.default 0)
                                           _ (/.poison! counter)]
                                          (/.mail! ++! counter)))]
                  (_.cover' [/.dead]
                            (case result
                              {#try.Success outcome}
                              false

                              {#try.Failure error}
                              (exception.match? /.dead error)))))

            (let [die! (: (/.Mail Nat)
                          (function (_ state actor)
                            (async\in (exception.except ..got_wrecked []))))]
              (in (do async.monad
                    [result (async.future (do io.monad
                                            [actor (/.spawn! /.default initial_state)
                                             sent? (/.mail! die! actor)
                                             alive? (/.alive? actor)
                                             obituary (/.obituary' actor)]
                                            (in {#try.Success [actor sent? alive? obituary]})))]
                    (_.cover' [/.Obituary /.obituary']
                              (case result
                                (^ {#try.Success [actor sent? alive? {#.Some [error state (list single_pending_message)]}]})
                                (and (..mailed? sent?)
                                     (not alive?)
                                     (exception.match? ..got_wrecked error)
                                     (n.= initial_state state)
                                     (same? die! single_pending_message))

                                _
                                false)))))

            (in (do async.monad
                  [counter (async.future (/.spawn! ..counter 0))
                   result (do (try.with async.monad)
                            [output_1 (/.tell! (count! 1) counter)
                             output_2 (/.tell! (count! 1) counter)
                             output_3 (/.tell! (count! 1) counter)]
                            (in (and (n.= 1 output_1)
                                     (n.= 2 output_2)
                                     (n.= 3 output_3))))]
                  (_.cover' [/.Message /.actor: /.message: /.tell!]
                            (case result
                              {#try.Success outcome}
                              outcome

                              {#try.Failure error}
                              false))))

            (in (do async.monad
                  [verdict (async.future
                            (do io.monad
                              [anonymous (/.actor [Nat
                                                   initial_state]
                                                  ((on_mail message state self)
                                                   (message (++ state) self)))
                               sent/++? (/.mail! ++! anonymous)
                               sent/--? (/.mail! --! anonymous)
                               poisoned? (/.poison! anonymous)
                               obituary (/.obituary' anonymous)]
                              (in (and (..mailed? sent/++?)
                                       (..mailed? sent/--?)
                                       (..mailed? poisoned?)
                                       (case obituary
                                         (^ {#.Some [error final_state (list poison_pill)]})
                                         (and (exception.match? /.poisoned error)
                                              (n.= (++ (++ initial_state))
                                                   final_state))
                                         
                                         _
                                         false)))))]
                  (_.cover' [/.actor]
                            verdict)))
            
            (do !
              [num_events (\ ! each (|>> (n.% 10) ++) random.nat)
               events (random.list num_events random.nat)
               num_observations (\ ! each (n.% num_events) random.nat)
               .let [expected (list.first num_observations events)
                     sink (: (Atom (Row Nat))
                             (atom.atom row.empty))]]
              (in (do async.monad
                    [agent (async.future
                            (do [! io.monad]
                              [agent (/.actor [Nat 0])
                               _ (/.observe! (function (_ event stop)
                                               (function (_ events_seen self)
                                                 (async.future
                                                  (if (n.< num_observations events_seen)
                                                    (do !
                                                      [_ (atom.update! (row.suffix event) sink)]
                                                      (in {#try.Success (++ events_seen)}))
                                                    (do !
                                                      [_ stop]
                                                      (in {#try.Failure "YOLO"}))))))
                                             (frp.sequential 0 events)
                                             agent)]
                              (in agent)))
                     _ (/.obituary agent)
                     actual (async.future (atom.read! sink))]
                    (_.cover' [/.Stop /.observe! /.obituary]
                              (\ (list.equivalence n.equivalence) = expected (row.list actual))))))
            ))))