Skip to content

Commit c521491

Browse files
committed
refactored channel specs.
1 parent bebbf28 commit c521491

File tree

2 files changed

+61
-72
lines changed

2 files changed

+61
-72
lines changed

spec/concurrent/channel/buffered_channel_spec.rb

Lines changed: 38 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,144 +4,139 @@ module Channel
44
describe BufferedChannel do
55

66
let(:size) { 2 }
7-
let!(:channel) { BufferedChannel.new(size) }
7+
subject { described_class.new(size) }
88
let(:probe) { Channel::Probe.new }
99

1010
context 'without timeout' do
1111

1212
describe '#push' do
1313
it 'adds elements to buffer' do
14-
expect(channel.buffer_queue_size).to be 0
14+
expect(subject.buffer_queue_size).to be 0
1515

16-
channel.push('a')
17-
channel.push('a')
16+
subject.push('a')
17+
subject.push('a')
1818

19-
expect(channel.buffer_queue_size).to be 2
19+
expect(subject.buffer_queue_size).to be 2
2020
end
2121

2222
it 'should block when buffer is full' do
23-
channel.push 1
24-
channel.push 2
23+
subject.push 1
24+
subject.push 2
2525

26-
t = Thread.new { channel.push 3 }
26+
t = Thread.new { subject.push 3 }
2727
t.join(0.1)
2828
expect(t.status).to eq 'sleep'
2929
end
3030

3131
it 'restarts thread when buffer is no more full' do
32-
channel.push 'hi'
33-
channel.push 'foo'
32+
subject.push 'hi'
33+
subject.push 'foo'
3434

3535
result = nil
3636

37-
t = Thread.new { channel.push 'bar'; result = 42 }
37+
t = Thread.new { subject.push 'bar'; result = 42 }
3838

3939
t.join(0.1)
40-
channel.pop
40+
subject.pop
4141
t.join(0.1)
4242

4343
expect(result).to eq 42
4444
end
4545

4646
it 'should assign value to a probe if probe set is not empty' do
47-
channel.select(probe)
48-
Thread.new { sleep(0.1); channel.push 3 }
47+
subject.select(probe)
48+
Thread.new { sleep(0.1); subject.push 3 }
4949
expect(probe.value.first).to eq 3
5050
end
5151
end
5252

5353
describe '#pop' do
5454
it 'should block if buffer is empty' do
55-
t = Thread.new { channel.pop }
55+
t = Thread.new { subject.pop }
5656
t.join(0.1)
5757
expect(t.status).to eq 'sleep'
5858
end
5959

6060
it 'returns value if buffer is not empty' do
61-
channel.push 1
62-
result = channel.pop
61+
subject.push 1
62+
result = subject.pop
6363

6464
expect(result.first).to eq 1
6565
end
6666

6767
it 'removes the first value from the buffer' do
68-
channel.push 'a'
69-
channel.push 'b'
68+
subject.push 'a'
69+
subject.push 'b'
7070

71-
expect(channel.pop.first).to eq 'a'
72-
expect(channel.buffer_queue_size).to eq 1
71+
expect(subject.pop.first).to eq 'a'
72+
expect(subject.buffer_queue_size).to eq 1
7373
end
7474
end
75-
7675
end
7776

7877
describe 'select' do
7978

8079
it 'does not block' do
81-
t = Thread.new { channel.select(probe) }
80+
t = Thread.new { subject.select(probe) }
8281
t.join(0.1)
8382
expect(t.status).to eq false
8483
end
8584

8685
it 'gets notified by writer thread' do
87-
channel.select(probe)
86+
subject.select(probe)
8887

89-
Thread.new { channel.push 82 }
88+
Thread.new { subject.push 82 }
9089

9190
expect(probe.value.first).to eq 82
9291
end
93-
9492
end
9593

9694
context 'already set probes' do
9795
context 'empty buffer' do
9896
it 'discards already set probes' do
9997
probe.set('set value')
10098

101-
channel.select(probe)
99+
subject.select(probe)
102100

103-
channel.push 27
101+
subject.push 27
104102

105-
expect(channel.buffer_queue_size).to eq 1
106-
expect(channel.probe_set_size).to eq 0
103+
expect(subject.buffer_queue_size).to eq 1
104+
expect(subject.probe_set_size).to eq 0
107105
end
108106
end
109107

110108
context 'empty probe set' do
111109
it 'discards set probe' do
112110
probe.set('set value')
113111

114-
channel.push 82
115-
116-
channel.select(probe)
112+
subject.push 82
117113

118-
expect(channel.buffer_queue_size).to eq 1
114+
subject.select(probe)
119115

120-
expect(channel.pop.first).to eq 82
116+
expect(subject.buffer_queue_size).to eq 1
121117

118+
expect(subject.pop.first).to eq 82
122119
end
123120
end
124121
end
125122

126123
describe 'probe set' do
127124

128125
it 'has size zero after creation' do
129-
expect(channel.probe_set_size).to eq 0
126+
expect(subject.probe_set_size).to eq 0
130127
end
131128

132129
it 'increases size after a select' do
133-
channel.select(probe)
134-
expect(channel.probe_set_size).to eq 1
130+
subject.select(probe)
131+
expect(subject.probe_set_size).to eq 1
135132
end
136133

137134
it 'decreases size after a removal' do
138-
channel.select(probe)
139-
channel.remove_probe(probe)
140-
expect(channel.probe_set_size).to eq 0
135+
subject.select(probe)
136+
subject.remove_probe(probe)
137+
expect(subject.probe_set_size).to eq 0
141138
end
142-
143139
end
144-
145140
end
146141
end
147142
end

spec/concurrent/channel/unbuffered_channel_spec.rb

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module Channel
33

44
describe UnbufferedChannel do
55

6-
let!(:channel) { subject }
6+
subject { described_class.new }
77
let(:probe) { Channel::Probe.new }
88

99
context 'with one thread' do
@@ -12,22 +12,20 @@ module Channel
1212

1313
describe '#push' do
1414
it 'should block' do
15-
t = Thread.new { channel.push 5 }
15+
t = Thread.new { subject.push 5 }
1616
t.join(0.1)
1717
expect(t.status).to eq 'sleep'
1818
end
1919
end
2020

2121
describe '#pop' do
2222
it 'should block' do
23-
t = Thread.new { channel.pop }
23+
t = Thread.new { subject.pop }
2424
t.join(0.1)
2525
expect(t.status).to eq 'sleep'
2626
end
2727
end
28-
2928
end
30-
3129
end
3230

3331
context 'cooperating threads' do
@@ -38,8 +36,8 @@ module Channel
3836

3937
result = nil
4038

41-
Thread.new { push_latch.wait(1); channel.push(42) }
42-
Thread.new { push_latch.count_down; result = channel.pop; pop_latch.count_down }
39+
Thread.new { push_latch.wait(1); subject.push(42) }
40+
Thread.new { push_latch.count_down; result = subject.pop; pop_latch.count_down }
4341

4442
pop_latch.wait(1)
4543
expect(result.first).to eq 42
@@ -49,10 +47,10 @@ module Channel
4947
result = Concurrent::AtomicFixnum.new(0)
5048

5149
threads = [
52-
Thread.new { channel.push 37 },
53-
Thread.new { channel.pop; result.increment },
54-
Thread.new { channel.pop; result.increment },
55-
Thread.new { channel.pop; result.increment }
50+
Thread.new { subject.push 37 },
51+
Thread.new { subject.pop; result.increment },
52+
Thread.new { subject.pop; result.increment },
53+
Thread.new { subject.pop; result.increment }
5654
]
5755

5856
threads.each{|t| t.join(0.1) }
@@ -64,8 +62,8 @@ module Channel
6462
result = nil
6563

6664
threads = [
67-
Thread.new { result = channel.pop; },
68-
Thread.new { channel.push 57 }
65+
Thread.new { result = subject.pop; },
66+
Thread.new { subject.push 57 }
6967
]
7068

7169
threads.each{|t| t.join(0.1) }
@@ -77,60 +75,56 @@ module Channel
7775
describe 'select' do
7876

7977
it 'does not block' do
80-
t = Thread.new { channel.select(probe) }
78+
t = Thread.new { subject.select(probe) }
8179
t.join(0.1)
8280

8381
expect(t.status).to eq false
8482
end
8583

8684
it 'gets notified by writer thread' do
87-
channel.select(probe)
85+
subject.select(probe)
8886

89-
Thread.new { channel.push 82 }
87+
Thread.new { subject.push 82 }
9088

9189
expect(probe.value.first).to eq 82
9290
end
9391

9492
it 'ignores already set probes and waits for a new one' do
9593
probe.set(27)
9694

97-
channel.select(probe)
95+
subject.select(probe)
9896

99-
t = Thread.new { channel.push 72 }
97+
t = Thread.new { subject.push 72 }
10098
t.join(0.1)
10199

102100
expect(t.status).to eq 'sleep'
103101

104102
new_probe = Channel::Probe.new
105103

106-
channel.select(new_probe)
104+
subject.select(new_probe)
107105
t.join(0.1)
108106

109107
expect(new_probe.value.first).to eq 72
110108
end
111-
112109
end
113110

114111
describe 'probe set' do
115112

116113
it 'has size zero after creation' do
117-
expect(channel.probe_set_size).to eq 0
114+
expect(subject.probe_set_size).to eq 0
118115
end
119116

120117
it 'increases size after a select' do
121-
channel.select(probe)
122-
expect(channel.probe_set_size).to eq 1
118+
subject.select(probe)
119+
expect(subject.probe_set_size).to eq 1
123120
end
124121

125122
it 'decreases size after a removal' do
126-
channel.select(probe)
127-
channel.remove_probe(probe)
128-
expect(channel.probe_set_size).to eq 0
123+
subject.select(probe)
124+
subject.remove_probe(probe)
125+
expect(subject.probe_set_size).to eq 0
129126
end
130-
131127
end
132-
133-
134128
end
135129
end
136130
end

0 commit comments

Comments
 (0)