aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/control/concurrency/frp.lux
blob: edfcc24aa4dad90b8bb6163b31ac2e33b38569ac (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
(.require
 [library
  [lux (.except)
   [abstract
    [monad (.only do)]
    [\\specification
     ["$[0]" functor (.only Injection Comparison)]
     ["$[0]" apply]
     ["$[0]" monad]]]
   [control
    ["[0]" try]
    ["[0]" exception]
    ["[0]" io (.only IO io)]]
   [data
    [collection
     ["[0]" list (.use "[1]#[0]" mix monoid)]
     ["[0]" sequence (.only Sequence)]]]
   [math
    ["[0]" random]
    [number
     ["n" nat]]]
   [test
    ["_" property (.only Test)]
    ["[0]" unit]]]]
 [\\library
  ["[0]" / (.only)
   [//
    ["[0]" async (.only Async) (.use "[1]#[0]" monad)]
    ["[0]" atom (.only Atom atom)]]]])

(def injection
  (Injection /.Channel)
  (|>> async.resolved
       /.of_async))

(def comparison
  (Comparison /.Channel)
  (function (_ == left right)
    (io.run!
     (do io.monad
       [?left (async.value left)
        ?right (async.value right)]
       (in (when [?left ?right]
             [{.#Some {.#Some [left _]}}
              {.#Some {.#Some [right _]}}]
             (== left right)
             
             _
             false))))))

(def (take_amount amount_of_polls [channel sink])
  (All (_ a) (-> Nat [(/.Channel a) (/.Sink a)] (Async (List a))))
  (when amount_of_polls
    0 (do async.monad
        [_ (async.future (of sink close))]
        (in {.#End}))
    _ (do [! async.monad]
        [event channel]
        (when event
          {.#None}
          (in {.#End})

          {.#Some [head tail]}
          (of ! each (|>> {.#Item head})
              (take_amount (-- amount_of_polls) [channel sink]))))))

(def .public test
  Test
  (<| (_.covering /._)
      (let [(open "list#[0]") (list.equivalence n.equivalence)]
        (do [! random.monad]
          [inputs (random.list 5 random.nat)
           sample random.nat
           distinct/0 random.nat
           distinct/1 (|> random.nat (random.only (|>> (n.= distinct/0) not)))
           distinct/2 (|> random.nat (random.only (function (_ value)
                                                    (not (or (n.= distinct/0 value)
                                                             (n.= distinct/1 value))))))
           shift random.nat]
          (all _.and
               (_.for [/.functor]
                      ($functor.spec ..injection ..comparison /.functor))
               (_.for [/.apply]
                      ($apply.spec ..injection ..comparison /.apply))
               (_.for [/.monad]
                      ($monad.spec ..injection ..comparison /.monad))

               (_.coverage [/.Channel /.Sink /.channel]
                 (when (io.run!
                        (do (try.with io.monad)
                          [.let [[channel sink] (is [(/.Channel Nat) (/.Sink Nat)]
                                                    (/.channel []))]
                           _ (of sink feed sample)
                           _ (of sink close)]
                          (in channel)))
                   {try.#Success channel}
                   (io.run!
                    (do io.monad
                      [?actual (async.value channel)]
                      (in (when ?actual
                            {.#Some {.#Some [actual _]}}
                            (n.= sample actual)
                            
                            _
                            false))))
                   
                   {try.#Failure error}
                   false))
               (_.coverage [/.already_closed]
                 (when (io.run!
                        (do (try.with io.monad)
                          [.let [[channel sink] (is [(/.Channel Nat) (/.Sink Nat)]
                                                    (/.channel []))]
                           _ (of sink close)]
                          (of sink feed sample)))
                   {try.#Success _}
                   false
                   
                   {try.#Failure error}
                   (exception.match? /.already_closed error)))
               (in (do async.monad
                     [output (|> sample
                                 async.resolved
                                 /.of_async
                                 /.list)]
                     (unit.coverage [/.of_async /.list]
                       (list#= (list sample)
                               output))))
               (in (do async.monad
                     [output (|> inputs
                                 (/.sequential 0)
                                 /.list)]
                     (unit.coverage [/.sequential]
                       (list#= inputs
                               output))))
               (in (do async.monad
                     [output (|> inputs
                                 (/.sequential 0)
                                 (/.only n.even?)
                                 /.list)]
                     (unit.coverage [/.only]
                       (list#= (list.only n.even? inputs)
                               output))))
               (in (do [! async.monad]
                     [.let [[?signal !signal] (is [(async.Async Any) (async.Resolver Any)]
                                                  (async.async []))
                            sink (is (Atom (Sequence Nat))
                                     (atom.atom sequence.empty))]
                      _ (async.future (/.subscribe! (function (_ value)
                                                      (do [! io.monad]
                                                        [current (atom.read! sink)
                                                         _ (atom.update! (sequence.suffix value) sink)]
                                                        (if (n.< (list.size inputs)
                                                                 (++ (sequence.size current)))
                                                          (in {.#Some []})
                                                          (do !
                                                            [_ (!signal [])]
                                                            (in {.#None})))))
                                                    (/.sequential 0 (list#composite inputs inputs))))
                      _ ?signal
                      listened (|> sink
                                   atom.read!
                                   async.future
                                   (of ! each sequence.list))]
                     (unit.coverage [/.Subscriber /.subscribe!]
                       (list#= inputs listened))))
               (in (do async.monad
                     [actual (/.mix (function (_ input total)
                                      (async.resolved (n.+ input total)))
                                    0
                                    (/.sequential 0 inputs))]
                     (unit.coverage [/.mix]
                       (n.= (list#mix n.+ 0 inputs)
                            actual))))
               (in (do async.monad
                     [actual (|> inputs
                                 (/.sequential 0)
                                 (/.mixes (function (_ input total)
                                            (async.resolved (n.+ input total)))
                                          0)
                                 /.list)]
                     (unit.coverage [/.mixes]
                       (list#= (list.mixes n.+ 0 inputs)
                               actual))))
               (in (do async.monad
                     [actual (|> (list distinct/0 distinct/0 distinct/0
                                       distinct/1
                                       distinct/2 distinct/2)
                                 (/.sequential 0)
                                 (/.distinct n.equivalence)
                                 /.list)]
                     (unit.coverage [/.distinct]
                       (list#= (list distinct/0 distinct/1 distinct/2)
                               actual))))
               (do !
                 [polling_delay (of ! each (|>> (n.% 10) ++) random.nat)
                  amount_of_polls (of ! each (|>> (n.% 10) ++) random.nat)]
                 (all _.and
                      (in (do [! async.monad]
                            [actual (..take_amount amount_of_polls (/.poll polling_delay (is (IO Nat) (io.io sample))))
                             .let [correct_values!
                                   (list.every? (n.= sample) actual)
                                   
                                   enough_polls!
                                   (n.= amount_of_polls (list.size actual))]]
                            (unit.coverage [/.poll]
                              (and correct_values!
                                   enough_polls!))))
                      (in (do [! async.monad]
                            [actual (..take_amount amount_of_polls (/.periodic polling_delay))]
                            (unit.coverage [/.periodic]
                              (n.= amount_of_polls (list.size actual)))))))
               (in (do async.monad
                     [.let [max_iterations 10]
                      actual (|> [0 sample]
                                 (/.iterations (function (_ [iterations current])
                                                 (async.resolved
                                                  (if (n.< max_iterations iterations)
                                                    {.#Some [[(++ iterations) (n.+ shift current)]
                                                             current]}
                                                    {.#None}))))
                                 /.list)]
                     (unit.coverage [/.iterations]
                       (and (n.= max_iterations (list.size actual))
                            (list#= (list.mixes n.+ sample (list.repeated (-- max_iterations) shift))
                                    actual)))))
               )))))