aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/control/concurrency/async.lux
blob: 063d3a9e0ef4058e35b5c90042c57ca4e01dda90 (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
(.module:
  [library
   [lux #*
    ["_" test (#+ Test)]
    ["@" target]
    [abstract
     [monad (#+ do)]
     [\\specification
      ["$." functor (#+ Injection Comparison)]
      ["$." apply]
      ["$." monad]]]
    [control
     [pipe (#+ case>)]
     ["." io]]
    [time
     ["." instant]
     ["." duration]]
    [math
     ["." random]
     [number
      ["n" nat]
      ["i" int]
      ["." i64]]]]]
  [\\library
   ["." /
    [//
     ["." atom (#+ Atom)]]]])

(def: injection
  (Injection /.Async)
  /.resolved)

(def: comparison
  (Comparison /.Async)
  (function (_ == left right)
    (io.run
     (do io.monad
       [?left (/.poll left)
        ?right (/.poll right)]
       (in (case [?left ?right]
             [(#.Some left)
              (#.Some right)]
             (== left right)
             
             _
             false))))))

(def: delay
  (for {@.js
        (i64.left_shifted 4 1)}
       (i64.left_shifted 3 1)))

(def: #export test
  Test
  (<| (_.covering /._)
      (do {! random.monad}
        [waiting_time (|> random.nat (\ ! map (|>> (n.% ..delay) (n.+ ..delay))))
         expected random.nat
         dummy random.nat
         .let [not_dummy (|> random.nat (random.only (|>> (n.= dummy) not)))]
         leftE not_dummy
         rightE not_dummy]
        ($_ _.and
            (_.for [/.functor]
                   ($functor.spec ..injection ..comparison /.functor))
            (_.for [/.apply]
                   ($apply.spec ..injection ..comparison /.apply))
            (_.for [/.monad]
                   ($monad.spec ..injection ..comparison /.monad))
            
            (in (do /.monad
                  [.let [[async resolver] (: [(/.Async Nat) (/.Resolver Nat)]
                                             (/.async []))]
                   resolved? (/.future (resolver expected))
                   actual async]
                  (_.cover' [/.Async /.Resolver /.async]
                            (and resolved?
                                 (n.= expected actual)))))
            (in (do /.monad
                  [actual (/.resolved expected)]
                  (_.cover' [/.resolved]
                            (n.= expected actual))))
            (in (do /.monad
                  [actual (/.future (io.io expected))]
                  (_.cover' [/.future]
                            (n.= expected actual))))
            (in (do /.monad
                  [pre (/.future instant.now)
                   actual (/.schedule waiting_time (io.io expected))
                   post (/.future instant.now)]
                  (_.cover' [/.schedule]
                            (and (n.= expected actual)
                                 (i.>= (.int waiting_time)
                                       (duration.millis (instant.span pre post)))))))
            (in (do /.monad
                  [pre (/.future instant.now)
                   _ (/.wait waiting_time)
                   post (/.future instant.now)]
                  (_.cover' [/.wait]
                            (i.>= (.int waiting_time)
                                  (duration.millis (instant.span pre post))))))
            (in (do /.monad
                  [[leftA rightA] (/.and (/.future (io.io leftE))
                                         (/.future (io.io rightE)))]
                  (_.cover' [/.and]
                            (n.= (n.+ leftE rightE)
                                 (n.+ leftA rightA)))))
            (in (do /.monad
                  [pre (/.future instant.now)
                   actual (/.delay waiting_time expected)
                   post (/.future instant.now)]
                  (_.cover' [/.delay]
                            (and (n.= expected actual)
                                 (i.>= (.int waiting_time)
                                       (duration.millis (instant.span pre post)))))))
            (in (do /.monad
                  [?left (/.or (in leftE)
                               (/.delay waiting_time dummy))
                   ?right (/.or (/.delay waiting_time dummy)
                                (in rightE))]
                  (_.cover' [/.or]
                            (case [?left ?right]
                              [(#.Left leftA) (#.Right rightA)]
                              (n.= (n.+ leftE rightE)
                                   (n.+ leftA rightA))

                              _
                              false))))
            (in (do /.monad
                  [leftA (/.either (in leftE)
                                   (/.delay waiting_time dummy))
                   rightA (/.either (/.delay waiting_time dummy)
                                    (in rightE))]
                  (_.cover' [/.either]
                            (n.= (n.+ leftE rightE)
                                 (n.+ leftA rightA)))))
            (in (do /.monad
                  [?actual (/.future (/.poll (/.resolved expected)))
                   .let [[async resolver] (: [(/.Async Nat) (/.Resolver Nat)]
                                             (/.async []))]
                   ?never (/.future (/.poll async))]
                  (_.cover' [/.poll]
                            (case [?actual ?never]
                              [(#.Some actual) #.None]
                              (n.= expected actual)

                              _
                              false))))
            (in (do /.monad
                  [yep (/.future (/.resolved? (/.resolved expected)))
                   .let [[async resolver] (: [(/.Async Nat) (/.Resolver Nat)]
                                             (/.async []))]
                   nope (/.future (/.resolved? async))]
                  (_.cover' [/.resolved?]
                            (and yep
                                 (not nope)))))
            (in (do /.monad
                  [?none (/.time_out 0 (/.delay waiting_time dummy))
                   ?actual (/.time_out waiting_time (in expected))]
                  (_.cover' [/.time_out]
                            (case [?none ?actual]
                              [#.None (#.Some actual)]
                              (n.= expected actual)

                              _
                              false))))
            (in (do /.monad
                  [.let [box (: (Atom Nat)
                                (atom.atom dummy))]
                   _ (/.future (/.await (function (_ value)
                                          (atom.write value box))
                                        (/.resolved expected)))
                   actual (/.future (atom.read box))]
                  (_.cover' [/.await]
                            (n.= expected actual))))
            ))))