File tree Expand file tree Collapse file tree 2 files changed +32
-7
lines changed Expand file tree Collapse file tree 2 files changed +32
-7
lines changed Original file line number Diff line number Diff line change @@ -5,7 +5,11 @@ def initialize
5
5
@mutex = Mutex . new
6
6
@condition = Condition . new
7
7
8
- @wait_set = [ ]
8
+ @probe_set = [ ]
9
+ end
10
+
11
+ def probe_set_size
12
+ @mutex . synchronize { @probe_set . size }
9
13
end
10
14
11
15
def push ( value )
@@ -21,19 +25,20 @@ def pop
21
25
22
26
def select ( probe )
23
27
@mutex . synchronize do
24
- @wait_set << probe
28
+ @probe_set << probe
25
29
@condition . signal
26
30
end
27
31
end
28
32
29
33
def remove_probe ( probe )
34
+ @mutex . synchronize { @probe_set . delete ( probe ) }
30
35
end
31
36
32
37
private
33
38
def first_waiting_probe
34
39
@mutex . synchronize do
35
- @condition . wait ( @mutex ) while @wait_set . empty?
36
- @wait_set . shift
40
+ @condition . wait ( @mutex ) while @probe_set . empty?
41
+ @probe_set . shift
37
42
end
38
43
end
39
44
Original file line number Diff line number Diff line change @@ -4,7 +4,8 @@ module Concurrent
4
4
5
5
describe UnbufferedChannel do
6
6
7
- let! ( :channel ) { subject } # let is not thread safe, let! creates the object before ensuring uniqueness
7
+ let ( :channel ) { subject }
8
+ let ( :probe ) { Probe . new }
8
9
9
10
context 'with one thread' do
10
11
@@ -58,8 +59,6 @@ module Concurrent
58
59
59
60
describe 'select' do
60
61
61
- let ( :probe ) { Probe . new }
62
-
63
62
it 'does not block' do
64
63
t = Thread . new { channel . select ( probe ) }
65
64
@@ -97,5 +96,26 @@ module Concurrent
97
96
end
98
97
99
98
end
99
+
100
+ describe 'probe set' do
101
+
102
+ it 'has size zero after creation' do
103
+ channel . probe_set_size . should eq 0
104
+ end
105
+
106
+ it 'increases size after a select' do
107
+ channel . select ( probe )
108
+ channel . probe_set_size . should eq 1
109
+ end
110
+
111
+ it 'decreases size after a removal' do
112
+ channel . select ( probe )
113
+ channel . remove_probe ( probe )
114
+ channel . probe_set_size . should eq 0
115
+ end
116
+
117
+ end
118
+
119
+
100
120
end
101
121
end
You can’t perform that action at this time.
0 commit comments