summaryrefslogtreecommitdiff
path: root/spec/ruby/optional/thread_safety/hash_spec.rb
blob: 53127fc97342b20ff02e9e2e20ad2efa9e662288 (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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
require_relative '../../spec_helper'
require_relative 'fixtures/classes'

describe "Hash thread safety" do
  it "supports concurrent #[]=" do
    n_threads = ThreadSafetySpecs.processors

    n = 1_000
    operations = n * n_threads

    h = {}
    barrier = ThreadSafetySpecs::Barrier.new(n_threads + 1)

    threads = n_threads.times.map { |t|
      Thread.new {
        barrier.wait
        base = t * n
        n.times do |j|
          h[base+j] = t
        end
      }
    }

    barrier.wait
    threads.each(&:join)

    h.size.should == operations
    h.each_pair { |key, value|
      (key / n).should == value
    }
  end

  # can't add a new key into hash during iteration (RuntimeError) on CRuby.
  # Yet it's good to test this for implementations that support it.
  guard_not -> { PlatformGuard.standard? } do
    it "supports concurrent #[]= and #delete and iteration" do
      n_threads = ThreadSafetySpecs.processors

      n = 1_000
      operations = n * n_threads / 2

      h = {}
      barrier1 = ThreadSafetySpecs::Barrier.new(n_threads + 2)
      barrier2 = ThreadSafetySpecs::Barrier.new(n_threads + 1)

      threads = n_threads.times.map { |t|
        Thread.new {
          barrier1.wait
          base = t * n
          n.times do |j|
            h[base+j] = t
          end

          barrier2.wait
          n.times do |j|
            # delete only even keys
            h.delete(base+j).should == t if (base+j).even?
          end
        }
      }

      read = true
      reader = Thread.new {
        barrier1.wait
        while read
          h.each_pair { |k,v|
            k.should.is_a?(Integer)
            v.should.is_a?(Integer)
            (k / n).should == v
          }
        end
      }

      barrier1.wait
      barrier2.wait
      threads.each(&:join)
      read = false
      reader.join

      # odd keys are left
      h.size.should == operations
      h.each_pair { |key, value|
        key.should.odd?
        (key / n).should == value
      }
    end
  end

  it "supports concurrent #[]= and #[]" do
    n_threads = ThreadSafetySpecs.processors

    n = 1_000
    operations = n * n_threads / 2

    h = {}
    barrier = ThreadSafetySpecs::Barrier.new(n_threads + 1)

    threads = n_threads.times.map { |t|
      Thread.new {
        barrier.wait
        base = (t / 2) * n

        if t.even?
          n.times do |j|
            k = base + j
            h[k] = k
          end
        else
          n.times do |j|
            k = base + j
            Thread.pass until v = h[k]
            v.should == k
          end
        end
      }
    }

    barrier.wait
    threads.each(&:join)

    h.size.should == operations
    h.each_pair { |key, value|
      key.should == value
    }
  end

  it "supports concurrent #[]= and #[] with change to #compare_by_identity in the middle" do
    n_threads = ThreadSafetySpecs.processors

    n = 1_000
    operations = n * n_threads / 2

    h = {}
    barrier = ThreadSafetySpecs::Barrier.new(n_threads + 1)

    threads = n_threads.times.map { |t|
      Thread.new {
        barrier.wait
        base = (t / 2) * n

        if t.even?
          n.times do |j|
            k = base + j
            h[k] = k
          end
        else
          n.times do |j|
            k = base + j
            Thread.pass until v = h[k]
            v.should == k
          end
        end
      }
    }

    changer = Thread.new {
      Thread.pass until h.size >= operations / 2
      h.should_not.compare_by_identity?
      h.compare_by_identity
      h.should.compare_by_identity?
    }

    barrier.wait
    threads.each(&:join)
    changer.join

    h.size.should == operations
    h.each_pair { |key, value|
      key.should == value
    }
  end

  it "supports repeated concurrent #[]= and #delete and always returns a #size >= 0" do
    n_threads = ThreadSafetySpecs.processors

    n = 1_000
    operations = n * n_threads / 2

    h = {}
    barrier = ThreadSafetySpecs::Barrier.new(n_threads + 1)
    deleted = ThreadSafetySpecs::Counter.new

    threads = n_threads.times.map { |t|
      Thread.new {
        barrier.wait
        key = t / 2

        if t.even?
          n.times {
            Thread.pass until h.delete(key)
            h.size.should >= 0
            deleted.increment
          }
        else
          n.times {
            h[key] = key
            Thread.pass while h.key?(key)
          }
        end
      }
    }

    barrier.wait
    threads.each(&:join)

    deleted.get.should == operations
    h.size.should == 0
    h.should.empty?
  end
end