aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/control/concurrency/promise.lux
blob: 04dd1c22071b775dc680455469dd372a0e717865 (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
(.module:
  [lux #*
   ["_" test (#+ Test)]
   [abstract
    [monad (#+ do)]
    {[0 #spec]
     [/
      ["$." functor (#+ Injection Comparison)]
      ["$." apply]
      ["$." monad]]}]
   [control
    [pipe (#+ case>)]
    ["." io]]
   [data
    [number
     ["n" nat]
     ["i" int]]]
   [time
    ["." instant]
    ["." duration]]
   [math
    ["." random]]]
  {1
   ["." /
    [//
     ["." 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: #export test
  Test
  (<| (_.covering /._)
      (do {! random.monad}
        [to-wait (|> random.nat (\ ! map (|>> (n.% 100) (n.max 10))))
         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 (/.delay 100 leftE)
                                 (/.delay 200 dummy))
                     ?right (/.or (/.delay 200 dummy)
                                  (/.delay 100 rightE))]
                    (_.cover' [/.or]
                              (case [?left ?right]
                                [(#.Left leftA) (#.Right rightA)]
                                (n.= (n.+ leftE rightE)
                                     (n.+ leftA rightA))

                                _
                                false))))
            (wrap (do /.monad
                    [leftA (/.either (/.delay 100 leftE)
                                     (/.delay 200 dummy))
                     rightA (/.either (/.delay 200 dummy)
                                      (/.delay 100 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))))
            ))))