aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/control/concurrency/semaphore.lux
blob: 710084670296da5b112522172766b9903387dde0 (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
187
(.using
 [library
  [lux "*"
   ["_" test (.only Test)]
   ["@" target]
   [abstract
    ["[0]" monad (.only do)]
    ["[0]" enum]]
   [control
    ["[0]" io]
    ["[0]" maybe]
    ["[0]" try]
    ["[0]" exception (.only exception:)]
    [concurrency
     ["[0]" async (.only Async)]
     ["[0]" atom (.only Atom)]]]
   [data
    ["[0]" text ("[1]#[0]" equivalence)
     ["%" format (.only format)]]
    [collection
     ["[0]" list ("[1]#[0]" functor)]]]
   [math
    ["[0]" random]
    [number
     ["n" nat]
     ["[0]" i64]]]
   [type
    ["[0]" refinement]]]]
 [\\library
  ["[0]" /]])

(def: delay
  (for @.js (i64.left_shifted 4 1)
       (i64.left_shifted 3 1)))

(def: semaphore
  Test
  (_.for [/.Semaphore]
         (all _.and
              (do [! random.monad]
                [initial_open_positions (|> random.nat (# ! each (|>> (n.% 10) (n.max 1))))
                 .let [semaphore (/.semaphore initial_open_positions)]]
                (in (do async.monad
                      [result (async.within ..delay (/.wait! semaphore))]
                      (_.coverage' [/.semaphore]
                        (case result
                          {.#Some _}
                          true

                          {.#None}
                          false)))))
              (do [! random.monad]
                [initial_open_positions (|> random.nat (# ! each (|>> (n.% 10) (n.max 1))))
                 .let [semaphore (/.semaphore initial_open_positions)]]
                (in (do [! async.monad]
                      [_ (monad.each ! /.wait! (list.repeated initial_open_positions semaphore))
                       result (async.within ..delay (/.wait! semaphore))]
                      (_.coverage' [/.wait!]
                        (case result
                          {.#Some _}
                          false

                          {.#None}
                          true)))))
              (do [! random.monad]
                [initial_open_positions (|> random.nat (# ! each (|>> (n.% 10) (n.max 1))))
                 .let [semaphore (/.semaphore initial_open_positions)]]
                (in (do [! async.monad]
                      [_ (monad.each ! /.wait! (list.repeated initial_open_positions semaphore))
                       .let [block (/.wait! semaphore)]
                       result/0 (async.within ..delay block)
                       open_positions (/.signal! semaphore)
                       result/1 (async.within ..delay block)]
                      (_.coverage' [/.signal!]
                        (case [result/0 result/1 open_positions]
                          [{.#None} {.#Some _} {try.#Success +0}]
                          true

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

                          _
                          false)))))
              )))

(def: mutex
  Test
  (_.for [/.Mutex]
         (all _.and
              (do [! random.monad]
                [repetitions (|> random.nat (# ! each (|>> (n.% 100) (n.max 10))))
                 .let [resource (atom.atom "")
                       expected_As (text.together (list.repeated repetitions "A"))
                       expected_Bs (text.together (list.repeated repetitions "B"))
                       mutex (/.mutex [])
                       processA (<| (/.synchronize! mutex)
                                    io.io
                                    async.future
                                    (do [! io.monad]
                                      [_ (<| (monad.all !)
                                             (list.repeated repetitions)
                                             (atom.update! (|>> (format "A")) resource))]
                                      (in [])))
                       processB (<| (/.synchronize! mutex)
                                    io.io
                                    async.future
                                    (do [! io.monad]
                                      [_ (<| (monad.all !)
                                             (list.repeated repetitions)
                                             (atom.update! (|>> (format "B")) resource))]
                                      (in [])))]]
                (in (do async.monad
                      [_ processA
                       _ processB
                       .let [outcome (io.run! (atom.read! resource))]]
                      (_.coverage' [/.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 (Async Any))
  (do async.monad
    [_ (/.block! barrier)
     _ (async.future (atom.update! (|>> (format (%.nat id))) resource))]
    (in [])))

(def: barrier
  Test
  (_.for [/.Barrier]
         (all _.and
              (do random.monad
                [raw random.nat]
                (_.coverage [/.Limit /.limit]
                  (case [raw (/.limit raw)]
                    [0 {.#None}]
                    true
                    
                    [_ {.#Some limit}]
                    (and (n.> 0 raw)
                         (n.= raw (refinement.value limit)))

                    _
                    false)))
              (do [! random.monad]
                [limit (# ! each (|>> (n.% 9) ++) random.nat)
                 .let [barrier (/.barrier (maybe.trusted (/.limit limit)))
                       resource (atom.atom "")]]
                (in (do [! async.monad]
                      [.let [suffix "_"
                             expected_ending (|> suffix
                                                 (list.repeated limit)
                                                 text.together)
                             expected_ids (enum.range n.enum 0 (-- limit))]
                       _ (|> expected_ids
                             (list#each (function (_ id)
                                          (exec
                                            (io.run! (atom.update! (|>> (format suffix)) resource))
                                            (waiter resource barrier id))))
                             (monad.all !))
                       .let [outcome (io.run! (atom.read! resource))]]
                      (_.coverage' [/.barrier /.block!]
                        (and (text.ends_with? expected_ending outcome)
                             (list.every? (function (_ id)
                                            (text.contains? (%.nat id) outcome))
                                          expected_ids))))))
              )))

(def: .public test
  Test
  (<| (_.covering /._)
      (all _.and
           ..semaphore
           ..mutex
           ..barrier
           )))