File: read_write_lock_spec.rb

package info (click to toggle)
ruby-concurrent 1.1.6%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 30,284 kB
  • sloc: ruby: 30,875; java: 6,117; javascript: 1,114; ansic: 288; makefile: 10; sh: 6
file content (492 lines) | stat: -rw-r--r-- 14,539 bytes parent folder | download | duplicates (2)
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
module Concurrent

  RSpec.describe ReadWriteLock do

    context '#write_locked?' do

      it 'returns true when the write lock is held' do
        latch_1 = Concurrent::CountDownLatch.new(1)
        latch_2 = Concurrent::CountDownLatch.new(1)

        in_thread do
          subject.with_write_lock do
            latch_1.count_down
            latch_2.wait(1)
          end
        end

        latch_1.wait(1)
        expect(subject).to be_write_locked
        latch_2.count_down
      end

      it 'returns false when the write lock is not held' do
        expect(subject).to_not be_write_locked
      end

      it 'returns false when the write lock is not held but there are readers' do
        latch = Concurrent::CountDownLatch.new(1)

        in_thread do
          subject.with_read_lock do
            latch.wait(1)
          end
        end

        expect(subject).to_not be_write_locked
        latch.count_down
      end
    end

    context '#has_waiters?' do

      it 'returns false when no locks are held' do
        expect(subject).to_not have_waiters
      end

      it 'returns false when there are readers but no writers' do
        latch = Concurrent::CountDownLatch.new(1)

        in_thread do
          subject.with_read_lock do
            latch.wait(1)
          end
        end

        expect(subject).to_not have_waiters
        latch.count_down
      end

      it 'returns true when the write lock is held and there are waiting readers' do
        latch_1 = Concurrent::CountDownLatch.new(1)
        latch_2 = Concurrent::CountDownLatch.new(1)
        latch_3 = Concurrent::CountDownLatch.new(1)

        in_thread do
          latch_1.wait(1)
          subject.acquire_write_lock
          latch_2.count_down
          latch_3.wait(1)
          subject.release_write_lock
        end

        in_thread do
          latch_2.wait(1)
          subject.acquire_read_lock
          subject.release_read_lock
        end

        latch_1.count_down
        latch_2.wait(1)

        expect(subject).to have_waiters

        latch_3.count_down
      end

      it 'returns true when the write lock is held and there are waiting writers' do
        latch_1 = Concurrent::CountDownLatch.new(1)
        latch_2 = Concurrent::CountDownLatch.new(1)
        latch_3 = Concurrent::CountDownLatch.new(1)

        t1 = in_thread do
          latch_1.wait(1)
          subject.acquire_write_lock
          latch_2.count_down
          latch_3.wait(1)
          subject.release_write_lock
        end

        t2 = in_thread do
          latch_2.wait(1)
          subject.acquire_write_lock
          subject.release_write_lock
        end

        latch_1.count_down
        latch_2.wait(1)

        expect(subject).to have_waiters

        latch_3.count_down

        join_with [t1, t2]
      end
    end

    context '#with_read_lock' do

      it 'acquires the lock' do
        expect(subject).to receive(:acquire_read_lock).with(no_args)
        subject.with_read_lock { nil }
      end

      it 'returns the value of the block operation' do
        expected = 100
        actual = subject.with_read_lock { expected }
        expect(actual).to eq expected
      end

      it 'releases the lock' do
        expect(subject).to receive(:release_read_lock).with(no_args)
        subject.with_read_lock { nil }
      end

      it 'raises an exception if no block is given' do
        expect {
          subject.with_read_lock
        }.to raise_error(ArgumentError)
      end

      it 'raises an exception if maximum lock limit is exceeded' do
        counter = Concurrent::AtomicFixnum.new(ReadWriteLock::MAX_READERS)
        allow(Concurrent::AtomicFixnum).to receive(:new).with(anything).and_return(counter)
        expect {
          subject.with_read_lock { nil }
        }.to raise_error(Concurrent::ResourceLimitError)
      end

      it 'releases the lock when an exception is raised' do
        expect(subject).to receive(:release_read_lock).with(any_args)
        begin
          subject.release_read_lock { raise StandardError }
        rescue
        end
      end
    end

    context '#with_write_lock' do

      it 'acquires the lock' do
        expect(subject).to receive(:acquire_write_lock).with(no_args)
        subject.with_write_lock { nil }
      end

      it 'returns the value of the block operation' do
        expected = 100
        actual = subject.with_write_lock { expected }
        expect(actual).to eq expected
      end

      it 'releases the lock' do
        expect(subject).to receive(:release_write_lock).with(no_args)
        subject.with_write_lock { nil }
      end

      it 'raises an exception if no block is given' do
        expect {
          subject.with_write_lock
        }.to raise_error(ArgumentError)
      end

      it 'raises an exception if maximum lock limit is exceeded' do
        counter = Concurrent::AtomicFixnum.new(ReadWriteLock::MAX_WRITERS)
        allow(Concurrent::AtomicFixnum).to receive(:new).with(anything).and_return(counter)
        expect {
          subject.with_write_lock { nil }
        }.to raise_error(Concurrent::ResourceLimitError)
      end

      it 'releases the lock when an exception is raised' do
        expect(subject).to receive(:release_write_lock).with(any_args)
        begin
          subject.with_write_lock { raise StandardError }
        rescue
        end
      end
    end

    context '#acquire_read_lock' do

      it 'increments the lock count' do
        counter = Concurrent::AtomicFixnum.new(0)
        allow(Concurrent::AtomicFixnum).to receive(:new).with(anything).and_return(counter)
        subject.acquire_read_lock
        expect(counter.value).to eq 1
      end

      it 'waits for a running writer to finish' do
        latch_1 = Concurrent::CountDownLatch.new(1)
        latch_2 = Concurrent::CountDownLatch.new(1)
        latch_3 = Concurrent::CountDownLatch.new(1)

        write_flag = Concurrent::AtomicBoolean.new(false)
        read_flag = Concurrent::AtomicBoolean.new(false)

        thread_1 = in_thread do
          latch_1.wait(1)
          subject.acquire_write_lock
          latch_2.count_down
          latch_3.wait(1)
          write_flag.make_true
          subject.release_write_lock
        end

        thread_2 = in_thread do
          latch_2.wait(1)
          expect(write_flag.value).to be false
          latch_3.count_down
          subject.acquire_read_lock
          expect(write_flag.value).to be true
          read_flag.make_true
          subject.release_read_lock
        end

        latch_1.count_down
        [thread_1, thread_2].each(&:join)

        expect(write_flag.value).to be true
        expect(read_flag.value).to be true
      end

      it 'does not wait for any running readers' do
        counter = Concurrent::AtomicFixnum.new(0)
        allow(Concurrent::AtomicFixnum).to receive(:new).with(anything).and_return(counter)

        latch_1 = Concurrent::CountDownLatch.new(1)
        latch_2 = Concurrent::CountDownLatch.new(1)
        latch_3 = Concurrent::CountDownLatch.new(1)

        read_flag_1 = Concurrent::AtomicBoolean.new(false)
        read_flag_2 = Concurrent::AtomicBoolean.new(false)

        thread_1 = in_thread do
          latch_1.wait(1)
          subject.acquire_read_lock
          expect(counter.value).to eq 1
          latch_2.count_down
          latch_3.wait(1)
          read_flag_1.make_true
          subject.release_read_lock
        end

        thread_2 = in_thread do
          latch_2.wait(1)
          expect(read_flag_1.value).to be false
          subject.acquire_read_lock
          expect(counter.value).to eq 2
          latch_3.count_down
          read_flag_2.make_true
          subject.release_read_lock
        end

        latch_1.count_down
        [thread_1, thread_2].each(&:join)

        expect(read_flag_1.value).to be true
        expect(read_flag_2.value).to be true
        expect(counter.value).to eq 0
      end

      it 'raises an exception if maximum lock limit is exceeded' do
        counter = Concurrent::AtomicFixnum.new(ReadWriteLock::MAX_WRITERS)
        allow(Concurrent::AtomicFixnum).to receive(:new).with(anything).and_return(counter)
        expect {
          subject.acquire_write_lock { nil }
        }.to raise_error(Concurrent::ResourceLimitError)
      end

      it 'returns true if the lock is acquired' do
        expect(subject.acquire_read_lock).to be true
      end
    end

    context '#release_read_lock' do

      it 'decrements the counter' do
        counter = Concurrent::AtomicFixnum.new(0)
        allow(Concurrent::AtomicFixnum).to receive(:new).with(anything).and_return(counter)
        subject.acquire_read_lock
        expect(counter.value).to eq 1
        subject.release_read_lock
        expect(counter.value).to eq 0
      end

      it 'unblocks waiting writers' do
        latch_1 = Concurrent::CountDownLatch.new(1)
        latch_2 = Concurrent::CountDownLatch.new(1)
        write_flag = Concurrent::AtomicBoolean.new(false)

        thread = in_thread do
          latch_1.wait(1)
          latch_2.count_down
          subject.acquire_write_lock
          write_flag.make_true
          subject.release_write_lock
        end

        subject.acquire_read_lock
        latch_1.count_down
        latch_2.wait(1)
        expect(write_flag.value).to be false
        subject.release_read_lock
        thread.join
        expect(write_flag.value).to be true
      end

      it 'returns true if the lock is released' do
        subject.acquire_read_lock
        expect(subject.release_read_lock).to be true
      end

      it 'returns true if the lock was never set' do
        expect(subject.release_read_lock).to be true
      end
    end

    context '#acquire_write_lock' do

      it 'increments the lock count' do
        counter = Concurrent::AtomicFixnum.new(0)
        allow(Concurrent::AtomicFixnum).to receive(:new).with(anything).and_return(counter)
        subject.acquire_write_lock
        expect(counter.value).to be > 1
      end

      it 'waits for a running writer to finish' do
        latch_1 = Concurrent::CountDownLatch.new(1)
        latch_2 = Concurrent::CountDownLatch.new(1)
        latch_3 = Concurrent::CountDownLatch.new(1)

        write_flag_1 = Concurrent::AtomicBoolean.new(false)
        write_flag_2 = Concurrent::AtomicBoolean.new(false)

        thread_1 = in_thread do
          latch_1.wait(1)
          subject.acquire_write_lock
          latch_2.count_down
          latch_3.wait(1)
          write_flag_1.make_true
          subject.release_write_lock
        end

        thread_2 = in_thread do
          latch_2.wait(1)
          expect(write_flag_1.value).to be false
          latch_3.count_down
          subject.acquire_write_lock
          expect(write_flag_1.value).to be true
          write_flag_2.make_true
          subject.release_write_lock
        end

        latch_1.count_down
        [thread_1, thread_2].each(&:join)

        expect(write_flag_1.value).to be true
        expect(write_flag_2.value).to be true
      end

      it 'waits for a running reader to finish' do
        latch_1 = Concurrent::CountDownLatch.new(1)
        latch_2 = Concurrent::CountDownLatch.new(1)
        latch_3 = Concurrent::CountDownLatch.new(1)

        read_flag = Concurrent::AtomicBoolean.new(false)
        write_flag = Concurrent::AtomicBoolean.new(false)

        thread_1 = in_thread do
          latch_1.wait(1)
          subject.acquire_read_lock
          latch_2.count_down
          latch_3.wait(1)
          read_flag.make_true
          subject.release_read_lock
        end

        thread_2 = in_thread do
          latch_2.wait(1)
          expect(read_flag.value).to be false
          latch_3.count_down
          subject.acquire_write_lock
          expect(read_flag.value).to be true
          write_flag.make_true
          subject.release_write_lock
        end

        latch_1.count_down
        [thread_1, thread_2].each(&:join)

        expect(read_flag.value).to be true
        expect(write_flag.value).to be true
      end

      it 'raises an exception if maximum lock limit is exceeded' do
        counter = Concurrent::AtomicFixnum.new(ReadWriteLock::MAX_WRITERS)
        allow(Concurrent::AtomicFixnum).to receive(:new).with(anything).and_return(counter)
        expect {
          subject.acquire_write_lock { nil }
        }.to raise_error(Concurrent::ResourceLimitError)
      end

      it 'returns true if the lock is acquired' do
        expect(subject.acquire_write_lock).to be true
      end
    end

    context '#release_write_lock' do

      it 'decrements the counter' do
        counter = Concurrent::AtomicFixnum.new(0)
        allow(Concurrent::AtomicFixnum).to receive(:new).with(anything).and_return(counter)
        subject.acquire_write_lock
        expect(counter.value).to be > 1
        subject.release_write_lock
        expect(counter.value).to eq 0
      end

      it 'unblocks waiting readers' do
        latch_1 = Concurrent::CountDownLatch.new(1)
        latch_2 = Concurrent::CountDownLatch.new(1)
        read_flag = Concurrent::AtomicBoolean.new(false)

        thread = in_thread do
          latch_1.wait(1)
          latch_2.count_down
          subject.acquire_read_lock
          read_flag.make_true
          subject.release_read_lock
        end

        subject.acquire_write_lock
        latch_1.count_down
        latch_2.wait(1)
        expect(read_flag.value).to be false
        subject.release_write_lock
        thread.join
        expect(read_flag.value).to be true
      end

      it 'unblocks waiting writers' do
        latch_1 = Concurrent::CountDownLatch.new(1)
        latch_2 = Concurrent::CountDownLatch.new(1)
        write_flag = Concurrent::AtomicBoolean.new(false)

        thread = in_thread do
          latch_1.wait(1)
          latch_2.count_down
          subject.acquire_write_lock
          write_flag.make_true
          subject.release_write_lock
        end

        subject.acquire_write_lock
        latch_1.count_down
        latch_2.wait(1)
        expect(write_flag.value).to be false
        subject.release_write_lock
        thread.join
        expect(write_flag.value).to be true
      end

      it 'returns true if the lock is released' do
        subject.acquire_write_lock
        expect(subject.release_write_lock).to be true
      end

      it 'returns true if the lock was never set' do
        expect(subject.release_write_lock).to be true
      end
    end
  end
end