aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/control/concurrency/thread.lux
blob: f8abf6a848d45d8c04434e0fb3489cbebe7022d1 (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
(.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 (\ ! map (|>> (n.% 5) (n.+ 5))
                  random.nat)]
        ($_ _.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 (n.* 2 delay))
                     [execution-time actual] (promise.future (atom.read box))]
                    (_.cover' [/.schedule]
                              (let [expected-delay!
                                    (i.>= (.int delay)
                                          (duration.to-millis (instant.span reference-time execution-time)))

                                    correct-value!
                                    (n.= expected actual)]
                                (and expected-delay!
                                     correct-value!)))))
            ))))