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

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

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

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

(def: #export test
  Test
  (<| (_.covering /._)
      (do {! random.monad}
        [to_wait (|> random.nat (\ ! map (|>> (n.% ..delay) (n.+ ..delay))))
         expected random.nat
         dummy random.nat
         #let [not_dummy (|> random.nat (random.filter (|>> (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))
            
            (wrap (do /.monad
                    [#let [[promise resolver] (: [(/.Promise Nat) (/.Resolver Nat)]
                                                 (/.promise []))]
                     resolved? (/.future (resolver expected))
                     actual promise]
                    (_.cover' [/.Promise /.Resolver /.promise]
                              (and resolved?
                                   (n.= expected actual)))))
            (wrap (do /.monad
                    [actual (/.resolved expected)]
                    (_.cover' [/.resolved]
                              (n.= expected actual))))
            (wrap (do /.monad
                    [actual (/.future (io.io expected))]
                    (_.cover' [/.future]
                              (n.= expected actual))))
            (wrap (do /.monad
                    [pre (/.future instant.now)
                     actual (/.schedule to_wait (io.io expected))
                     post (/.future instant.now)]
                    (_.cover' [/.schedule]
                              (and (n.= expected actual)
                                   (i.>= (.int to_wait)
                                         (duration.to_millis (instant.span pre post)))))))
            (wrap (do /.monad
                    [pre (/.future instant.now)
                     _ (/.wait to_wait)
                     post (/.future instant.now)]
                    (_.cover' [/.wait]
                              (i.>= (.int to_wait)
                                    (duration.to_millis (instant.span pre post))))))
            (wrap (do /.monad
                    [[leftA rightA] (/.and (/.future (io.io leftE))
                                           (/.future (io.io rightE)))]
                    (_.cover' [/.and]
                              (n.= (n.+ leftE rightE)
                                   (n.+ leftA rightA)))))
            (wrap (do /.monad
                    [pre (/.future instant.now)
                     actual (/.delay to_wait expected)
                     post (/.future instant.now)]
                    (_.cover' [/.delay]
                              (and (n.= expected actual)
                                   (i.>= (.int to_wait)
                                         (duration.to_millis (instant.span pre post)))))))
            (wrap (do /.monad
                    [?left (/.or (wrap leftE)
                                 (/.delay to_wait dummy))
                     ?right (/.or (/.delay to_wait dummy)
                                  (wrap rightE))]
                    (_.cover' [/.or]
                              (case [?left ?right]
                                [(#.Left leftA) (#.Right rightA)]
                                (n.= (n.+ leftE rightE)
                                     (n.+ leftA rightA))

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

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

                                _
                                false))))
            (wrap (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))))
            ))))