aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/control/concurrency/thread.lux
blob: 210ff4b1dc944fb2a409f9ea624871f6105b7c2f (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
(.module:
  [lux #*
   ["_" test (#+ Test)]
   [abstract
    [monad (#+ do)]]
   [control
    ["." io]]
   [data
    [number
     ["n" nat]
     ["i" int]]]
   [time
    ["." instant (#+ Instant)]
    ["." duration]]
   [math
    ["." random]]]
  {1
   ["." /
    [//
     ["." atom (#+ Atom)]
     ["." promise]]]})

(def: #export test
  Test
  (<| (_.covering /._)
      (do {! random.monad}
        [dummy random.nat
         expected random.nat
         delay (|> random.nat (\ ! map (n.% 100)))]
        ($_ _.and
            (_.cover [/.parallelism]
                     (n.> 0 /.parallelism))
            (wrap (do promise.monad
                    [reference-time (promise.future instant.now)
                     #let [box (atom.atom [reference-time dummy])]
                     _ (promise.future
                        (/.schedule delay (do io.monad
                                            [execution-time instant.now]
                                            (atom.write [execution-time expected] box))))
                     _ (promise.wait delay)
                     [execution-time actual] (promise.future (atom.read box))]
                    (_.cover' [/.schedule]
                              (and (i.>= (.int delay)
                                         (duration.to-millis (instant.span reference-time execution-time)))
                                   (n.= expected actual)))))
            ))))