aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/control/concurrency/stm.lux
blob: ca55f2364914850876d66c2966ca3ef01d40fb21 (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
(.using
 [library
  [lux "*"
   ["_" test {"+" Test}]
   [abstract
    ["[0]" monad {"+" Monad do}]
    [\\specification
     ["$[0]" functor {"+" Injection Comparison}]
     ["$[0]" apply]
     ["$[0]" monad]]]
   [control
    ["[0]" io {"+" IO}]]
   [data
    ["[0]" product]
    [collection
     ["[0]" list ("[1]#[0]" functor)]]]
   [math
    ["[0]" random]
    [number
     ["n" nat]]]]]
 [\\library
  ["[0]" /
   [//
    ["[0]" atom {"+" Atom atom}]
    ["[0]" async]
    ["[0]" frp {"+" Channel}]]]])

(def: injection
  (Injection /.STM)
  (# /.monad in))

(def: comparison
  (Comparison /.STM)
  (function (_ == left right)
    (== (product.right (left (list)))
        (product.right (right (list))))))

(def: .public test
  Test
  (<| (_.covering /._)
      (do [! random.monad]
        [dummy random.nat
         expected random.nat
         iterations_per_process (|> random.nat (# ! each (n.% 100)))]
        ($_ _.and
            (_.for [/.functor]
                   ($functor.spec ..injection ..comparison /.functor))
            (_.for [/.apply]
                   ($apply.spec ..injection ..comparison /.apply))
            (_.for [/.monad]
                   ($monad.spec ..injection ..comparison /.monad))

            (in (do async.monad
                  [actual (/.commit! (# /.monad in expected))]
                  (_.cover' [/.commit!]
                            (n.= expected actual))))
            (in (do async.monad
                  [actual (/.commit! (/.read (/.var expected)))]
                  (_.cover' [/.Var /.var /.read]
                            (n.= expected actual))))
            (in (do async.monad
                  [actual (let [box (/.var dummy)]
                            (/.commit! (do /.monad
                                         [_ (/.write expected box)]
                                         (/.read box))))
                   verdict (let [box (/.var dummy)]
                             (/.commit! (do /.monad
                                          [_ (/.write expected box)
                                           actual (/.read box)]
                                          (in (n.= expected actual)))))]
                  (_.cover' [/.write]
                            (and (n.= expected actual)
                                 verdict))))
            (in (do async.monad
                  [.let [box (/.var dummy)]
                   output (/.commit! (do /.monad
                                       [_ (/.update (n.+ expected) box)]
                                       (/.read box)))]
                  (_.cover' [/.update]
                            (n.= (n.+ expected dummy)
                                 output))))
            (in (do async.monad
                  [.let [box (/.var dummy)
                         [follower sink] (io.run! (/.changes box))]
                   _ (/.commit! (/.write expected box))
                   _ (/.commit! (/.update (n.* 2) box))
                   _ (async.future (# sink close))
                   _ (/.commit! (/.update (n.* 3) box))
                   changes (frp.list follower)]
                  (_.cover' [/.changes]
                            (# (list.equivalence n.equivalence) =
                               (list expected (n.* 2 expected))
                               changes))))
            (in (let [var (/.var 0)]
                  (do [! async.monad]
                    [_ (|> (list.repeated iterations_per_process [])
                           (list#each (function (_ _) (/.commit! (/.update ++ var))))
                           (monad.all !))
                     cummulative (/.commit! (/.read var))]
                    (_.cover' [/.STM]
                              (n.= iterations_per_process
                                   cummulative)))))
            ))))