aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/control/concurrency/semaphore.lux
blob: 8924cf66ffca35aedd41cfe8d230b812e4333d84 (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
(.module:
  [lux #*
   ["_" test (#+ Test)]
   ["@" target]
   [abstract
    ["." monad (#+ do)]
    ["." enum]]
   [control
    ["." io]
    ["." try]
    ["." exception (#+ exception:)]
    [concurrency
     ["." promise (#+ Promise)]
     ["." atom (#+ Atom)]]]
   [data
    ["." maybe]
    ["." text ("#\." equivalence)
     ["%" format (#+ format)]]
    [collection
     ["." list ("#\." functor)]]]
   [math
    ["." random]
    [number
     ["n" nat]
     ["." i64]]]
   [type
    ["." refinement]]]
  [\\
   ["." /]])

(def: delay
  (for {@.js
        (i64.left_shift 4 1)}
       (i64.left_shift 3 1)))

(def: semaphore
  Test
  (_.for [/.Semaphore]
         ($_ _.and
             (do {! random.monad}
               [initial_open_positions (|> random.nat (\ ! map (|>> (n.% 10) (n.max 1))))
                #let [semaphore (/.semaphore initial_open_positions)]]
               (wrap (do promise.monad
                       [result (promise.time_out ..delay (/.wait semaphore))]
                       (_.cover' [/.semaphore]
                                 (case result
                                   (#.Some _)
                                   true

                                   #.None
                                   false)))))
             (do {! random.monad}
               [initial_open_positions (|> random.nat (\ ! map (|>> (n.% 10) (n.max 1))))
                #let [semaphore (/.semaphore initial_open_positions)]]
               (wrap (do {! promise.monad}
                       [_ (monad.map ! /.wait (list.repeat initial_open_positions semaphore))
                        result (promise.time_out ..delay (/.wait semaphore))]
                       (_.cover' [/.wait]
                                 (case result
                                   (#.Some _)
                                   false

                                   #.None
                                   true)))))
             (do {! random.monad}
               [initial_open_positions (|> random.nat (\ ! map (|>> (n.% 10) (n.max 1))))
                #let [semaphore (/.semaphore initial_open_positions)]]
               (wrap (do {! promise.monad}
                       [_ (monad.map ! /.wait (list.repeat initial_open_positions semaphore))
                        #let [block (/.wait semaphore)]
                        result/0 (promise.time_out ..delay block)
                        open_positions (/.signal semaphore)
                        result/1 (promise.time_out ..delay block)]
                       (_.cover' [/.signal]
                                 (case [result/0 result/1 open_positions]
                                   [#.None (#.Some _) (#try.Success +0)]
                                   true

                                   _
                                   false)))))
             (do {! random.monad}
               [initial_open_positions (|> random.nat (\ ! map (|>> (n.% 10) (n.max 1))))
                #let [semaphore (/.semaphore initial_open_positions)]]
               (wrap (do promise.monad
                       [outcome (/.signal semaphore)]
                       (_.cover' [/.semaphore_is_maxed_out]
                                 (case outcome
                                   (#try.Failure error)
                                   (exception.match? /.semaphore_is_maxed_out error)

                                   _
                                   false)))))
             )))

(def: mutex
  Test
  (_.for [/.Mutex]
         ($_ _.and
             (do {! random.monad}
               [repetitions (|> random.nat (\ ! map (|>> (n.% 100) (n.max 10))))
                #let [resource (atom.atom "")
                      expected_As (text.join_with "" (list.repeat repetitions "A"))
                      expected_Bs (text.join_with "" (list.repeat repetitions "B"))
                      mutex (/.mutex [])
                      processA (<| (/.synchronize mutex)
                                   io.io
                                   promise.future
                                   (do {! io.monad}
                                     [_ (<| (monad.seq !)
                                            (list.repeat repetitions)
                                            (atom.update (|>> (format "A")) resource))]
                                     (wrap [])))
                      processB (<| (/.synchronize mutex)
                                   io.io
                                   promise.future
                                   (do {! io.monad}
                                     [_ (<| (monad.seq !)
                                            (list.repeat repetitions)
                                            (atom.update (|>> (format "B")) resource))]
                                     (wrap [])))]]
               (wrap (do promise.monad
                       [_ processA
                        _ processB
                        #let [outcome (io.run (atom.read resource))]]
                       (_.cover' [/.mutex /.synchronize]
                                 (or (text\= (format expected_As expected_Bs)
                                             outcome)
                                     (text\= (format expected_Bs expected_As)
                                             outcome))))))
             )))

(def: (waiter resource barrier id)
  (-> (Atom Text) /.Barrier Nat (Promise Any))
  (do promise.monad
    [_ (/.block barrier)
     _ (promise.future (atom.update (|>> (format (%.nat id))) resource))]
    (wrap [])))

(def: barrier
  Test
  (_.for [/.Barrier]
         ($_ _.and
             (do random.monad
               [raw random.nat]
               (_.cover [/.Limit /.limit]
                        (case [raw (/.limit raw)]
                          [0 #.None]
                          true
                          
                          [_ (#.Some limit)]
                          (and (n.> 0 raw)
                               (n.= raw (refinement.un_refine limit)))

                          _
                          false)))
             (do {! random.monad}
               [limit (\ ! map (|>> (n.% 9) inc) random.nat)
                #let [barrier (/.barrier (maybe.assume (/.limit limit)))
                      resource (atom.atom "")]]
               (wrap (do {! promise.monad}
                       [#let [suffix "_"
                              expected_ending (|> suffix
                                                  (list.repeat limit)
                                                  (text.join_with ""))
                              expected_ids (enum.range n.enum 0 (dec limit))]
                        _ (|> expected_ids
                              (list\map (function (_ id)
                                          (exec (io.run (atom.update (|>> (format suffix)) resource))
                                            (waiter resource barrier id))))
                              (monad.seq !))
                        #let [outcome (io.run (atom.read resource))]]
                       (_.cover' [/.barrier /.block]
                                 (and (text.ends_with? expected_ending outcome)
                                      (list.every? (function (_ id)
                                                     (text.contains? (%.nat id) outcome))
                                                   expected_ids))))))
             )))

(def: #export test
  Test
  (<| (_.covering /._)
      ($_ _.and
          ..semaphore
          ..mutex
          ..barrier
          )))