aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/control/concurrency/stm.lux
blob: 8d56cbed2373c8da677eddc7be4bf85eeacf93d1 (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
(.module:
  [library
   [lux #*
    ["_" test (#+ Test)]
    [abstract
     ["." monad (#+ Monad do)]
     [\\specification
      ["$." functor (#+ Injection Comparison)]
      ["$." apply]
      ["$." monad]]]
    [control
     ["." io (#+ IO)]]
    [data
     ["." product]
     [collection
      ["." list ("#\." functor)]]]
    [math
     ["." random]
     [number
      ["n" nat]]]]]
  [\\library
   ["." /
    [//
     ["." atom (#+ Atom atom)]
     ["." async]
     ["." 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! (/.follow! box))]
                   _ (/.commit! (/.write expected box))
                   _ (/.commit! (/.update (n.* 2) box))
                   _ (async.future (\ sink close))
                   _ (/.commit! (/.update (n.* 3) box))
                   changes (frp.list follower)]
                  (_.cover' [/.follow!]
                            (\ (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)))))
            ))))