File: synchronized_delegator_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 (85 lines) | stat: -rw-r--r-- 1,863 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
require 'concurrent/thread_safe/synchronized_delegator.rb'

module Concurrent
  RSpec.describe SynchronizedDelegator do
    it 'wraps array' do
      array = ::Array.new
      sync_array = described_class.new(array)

      array << 1
      expect(1).to eq sync_array[0]

      sync_array << 2
      expect(2).to eq array[1]
    end

    it 'synchronizes access' do
      t1_continue, t2_continue = false, false

      hash = ::Hash.new do |the_hash, key|
        t2_continue = true
        unless the_hash.find { |e| e[1] == key.to_s } # just to do something
          the_hash[key] = key.to_s
          Thread.pass until t1_continue
        end
      end
      sync_hash = described_class.new(hash)
      sync_hash[1] = 'egy'

      t1 = in_thread do
        sync_hash[2] = 'dva'
        sync_hash[3] # triggers t2_continue
      end

      t2 = in_thread do
        Thread.pass until t2_continue
        sync_hash[4] = '42'
      end

      sleep(0.05) # sleep some to allow threads to boot

      until t2.status == 'sleep' do
        Thread.pass
      end

      expect(3).to eq hash.keys.size

      t1_continue = true
      t1.join; t2.join

      expect(4).to eq sync_hash.size
    end

    it 'synchronizes access with block' do
      t1_continue, t2_continue = false, false

      array = ::Array.new
      sync_array = described_class.new(array)

      t1 = in_thread do
        sync_array << 1
        sync_array.each do
          t2_continue = true
          Thread.pass until t1_continue
        end
      end

      t2 = in_thread do
        # sleep(0.01)
        Thread.pass until t2_continue
        sync_array << 2
      end

      until t2.status == 'sleep' || t2.status == false
        Thread.pass
      end

      expect(1).to eq array.size

      t1_continue = true
      t1.join; t2.join

      expect([1, 2]).to eq array
    end
  end
end