aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/control/concurrency/async.lux
blob: 64bc1e119794f0b55a0d43030424f276541af8d3 (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
(.require
 [library
  [lux (.except)
   [abstract
    [monad (.only do)]
    ["[0]" functor
     ["[1]T" \\test (.only Injection Comparison)]]
    ["[0]" apply
     ["[1]T" \\test]]
    [\\specification
     ["$[0]" monad]]]
   [control
    ["[0]" io]]
   [math
    ["[0]" random]
    [number
     ["n" nat]
     ["i" int]
     ["[0]" i64]]]
   [meta
    ["@" target]]
   [world
    [time
     ["[0]" instant]
     ["[0]" duration]]]
   [test
    ["_" property (.only Test)]
    ["[0]" unit]]]]
 [\\library
  ["[0]" / (.only)
   [//
    ["[0]" atom (.only Atom)]]]])

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

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

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

(def .public test
  Test
  (<| (_.covering /._)
      (do [! random.monad]
        [waiting_time (|> random.nat (of ! each (|>> (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]
        (all _.and
             (_.for [/.functor]
                    (functorT.spec ..injection ..comparison /.functor))
             (_.for [/.apply]
                    (applyT.spec ..injection ..comparison /.apply))
             (_.for [/.monad]
                    ($monad.spec ..injection ..comparison /.monad))
             
             (in (do /.monad
                   [.let [[async resolver] (is [(/.Async Nat) (/.Resolver Nat)]
                                               (/.async []))]
                    resolved? (/.future (resolver expected))
                    actual async]
                   (unit.coverage [/.Async /.Resolver /.async]
                     (and resolved?
                          (n.= expected actual)))))
             (in (do /.monad
                   [actual (/.resolved expected)]
                   (unit.coverage [/.resolved]
                     (n.= expected actual))))
             (in (do /.monad
                   [actual (/.future (io.io expected))]
                   (unit.coverage [/.future]
                     (n.= expected actual))))
             (in (do /.monad
                   [pre (/.future instant.now)
                    actual (/.schedule! waiting_time (io.io expected))
                    post (/.future instant.now)]
                   (unit.coverage [/.schedule!]
                     (and (n.= expected actual)
                          (i.>= (.int waiting_time)
                                (duration.millis (instant.span pre post)))))))
             (in (do /.monad
                   [pre (/.future instant.now)
                    _ (/.delay waiting_time)
                    post (/.future instant.now)]
                   (unit.coverage [/.delay]
                     (i.>= (.int waiting_time)
                           (duration.millis (instant.span pre post))))))
             (in (do /.monad
                   [[leftA rightA] (/.and (/.future (io.io leftE))
                                          (/.future (io.io rightE)))]
                   (unit.coverage [/.and]
                     (n.= (n.+ leftE rightE)
                          (n.+ leftA rightA)))))
             (in (do /.monad
                   [pre (/.future instant.now)
                    actual (/.after waiting_time expected)
                    post (/.future instant.now)]
                   (unit.coverage [/.after]
                     (and (n.= expected actual)
                          (i.>= (.int waiting_time)
                                (duration.millis (instant.span pre post)))))))
             (in (do /.monad
                   [?left (/.or (in leftE)
                                (/.after waiting_time dummy))
                    ?right (/.or (/.after waiting_time dummy)
                                 (in rightE))]
                   (unit.coverage [/.or]
                     (when [?left ?right]
                       [{.#Left leftA} {.#Right rightA}]
                       (n.= (n.+ leftE rightE)
                            (n.+ leftA rightA))

                       _
                       false))))
             (in (do /.monad
                   [leftA (/.either (in leftE)
                                    (/.after waiting_time dummy))
                    rightA (/.either (/.after waiting_time dummy)
                                     (in rightE))]
                   (unit.coverage [/.either]
                     (n.= (n.+ leftE rightE)
                          (n.+ leftA rightA)))))
             (in (do /.monad
                   [?actual (/.future (/.value (/.resolved expected)))
                    .let [[async resolver] (is [(/.Async Nat) (/.Resolver Nat)]
                                               (/.async []))]
                    ?never (/.future (/.value async))]
                   (unit.coverage [/.value]
                     (when [?actual ?never]
                       [{.#Some actual} {.#None}]
                       (n.= expected actual)

                       _
                       false))))
             (in (do /.monad
                   [yep (/.future (/.resolved? (/.resolved expected)))
                    .let [[async resolver] (is [(/.Async Nat) (/.Resolver Nat)]
                                               (/.async []))]
                    nope (/.future (/.resolved? async))]
                   (unit.coverage [/.resolved?]
                     (and yep
                          (not nope)))))
             (in (do /.monad
                   [?none (/.within 0 (/.after waiting_time dummy))
                    ?actual (/.within waiting_time (in expected))]
                   (unit.coverage [/.within]
                     (when [?none ?actual]
                       [{.#None} {.#Some actual}]
                       (n.= expected actual)

                       _
                       false))))
             (in (do /.monad
                   [.let [box (is (Atom Nat)
                                  (atom.atom dummy))]
                    _ (/.future (/.upon! (function (_ value)
                                           (atom.write! value box))
                                         (/.resolved expected)))
                    actual (/.future (atom.read! box))]
                   (unit.coverage [/.upon!]
                     (n.= expected actual))))
             ))))