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