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

(def injection
  (Injection /.STM)
  (at /.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 (at ! each (n.% 100)))]
        (all _.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! (at /.monad in expected))]
                   (_.coverage' [/.commit!]
                     (n.= expected actual))))
             (in (do async.monad
                   [actual (/.commit! (/.read (/.var expected)))]
                   (_.coverage' [/.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)))))]
                   (_.coverage' [/.write]
                     (and (n.= expected actual)
                          verdict))))
             (in (do async.monad
                   [.let [box (/.var dummy)]
                    output (/.commit! (do /.monad
                                        [_ (/.update (n.+ expected) box)]
                                        (/.read box)))]
                   (_.coverage' [/.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 (at sink close))
                    _ (/.commit! (/.update (n.* 3) box))
                    changes (frp.list follower)]
                   (_.coverage' [/.changes]
                     (at (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))]
                     (_.coverage' [/.STM]
                       (n.= iterations_per_process
                            cummulative)))))
             ))))