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

(def: injection
  (Injection /.STM)
  (\ /.monad wrap))

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

(def: #export test
  Test
  (<| (_.covering /._)
      (do {! random.monad}
        [dummy random.nat
         expected random.nat
         iterations-per-process (|> random.nat (\ ! map (n.% 100)))]
        ($_ _.and
            (_.with-cover [/.functor]
              ($functor.spec ..injection ..comparison /.functor))
            (_.with-cover [/.apply]
              ($apply.spec ..injection ..comparison /.apply))
            (_.with-cover [/.monad]
              ($monad.spec ..injection ..comparison /.monad))

            (wrap (do promise.monad
                    [actual (/.commit (\ /.monad wrap expected))]
                    (_.cover' [/.commit]
                              (n.= expected actual))))
            (wrap (do promise.monad
                    [actual (/.commit (/.read (/.var expected)))]
                    (_.cover' [/.Var /.var /.read]
                              (n.= expected actual))))
            (wrap (do promise.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)]
                                           (wrap (n.= expected actual)))))]
                    (_.cover' [/.write]
                              (and (n.= expected actual)
                                   verdict))))
            (wrap (do promise.monad
                    [#let [box (/.var dummy)]
                     output (/.commit (do /.monad
                                        [_ (/.update (n.+ expected) box)]
                                        (/.read box)))]
                    (_.cover' [/.update]
                              (n.= (n.+ expected dummy)
                                   output))))
            (wrap (do promise.monad
                    [#let [box (/.var dummy)
                           [follower sink] (io.run (/.follow box))]
                     _ (/.commit (/.write expected box))
                     _ (/.commit (/.update (n.* 2) box))
                     _ (promise.future (\ sink close))
                     _ (/.commit (/.update (n.* 3) box))
                     changes (frp.consume follower)]
                    (_.cover' [/.follow]
                              (\ (list.equivalence n.equivalence) =
                                 (list expected (n.* 2 expected))
                                 changes))))
            (wrap (let [var (/.var 0)]
                    (do {! promise.monad}
                      [_ (|> (list.repeat iterations-per-process [])
                             (list\map (function (_ _) (/.commit (/.update inc var))))
                             (monad.seq !))
                       cummulative (/.commit (/.read var))]
                      (_.cover' [/.STM]
                                (n.= iterations-per-process
                                     cummulative)))))
            ))))