Skip to content

Commit 8a05e22

Browse files
committed
added RingBuffer#empty? and RingBuffer#full?
1 parent af2c37d commit 8a05e22

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

lib/concurrent/channel/ring_buffer.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ def count
1717
@mutex.synchronize { @count }
1818
end
1919

20+
def full?
21+
@mutex.synchronize { @count == @buffer.size }
22+
end
23+
24+
def empty?
25+
@mutex.synchronize { @count == 0 }
26+
end
27+
2028
def put(value)
2129
@mutex.synchronize do
2230
wait_while_full

spec/concurrent/channel/ring_buffer_spec.rb

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ module Concurrent
77
let(:capacity) { 3 }
88
let(:buffer) { RingBuffer.new( capacity ) }
99

10+
def fill_buffer
11+
capacity.times { buffer.put 3 }
12+
end
13+
1014
describe '#capacity' do
1115
it 'returns the value passed in constructor' do
1216
buffer.capacity.should eq capacity
@@ -35,9 +39,31 @@ module Concurrent
3539
end
3640
end
3741

42+
describe '#empty?' do
43+
it 'is true when count is zero' do
44+
buffer.empty?.should be_true
45+
end
46+
47+
it 'is false when count is not zero' do
48+
buffer.put 82
49+
buffer.empty?.should be_false
50+
end
51+
end
52+
53+
describe '#full?' do
54+
it 'is true when count is capacity' do
55+
fill_buffer
56+
buffer.full?.should be_true
57+
end
58+
59+
it 'is false when count is not capacity' do
60+
buffer.full?.should be_false
61+
end
62+
end
63+
3864
describe '#put' do
3965
it 'block when buffer is full' do
40-
capacity.times { buffer.put 27 }
66+
fill_buffer
4167

4268
t = Thread.new { buffer.put 32 }
4369

@@ -87,7 +113,7 @@ module Concurrent
87113

88114
context 'circular condition' do
89115
it 'can filled many times' do
90-
capacity.times { buffer.put 3 }
116+
fill_buffer
91117
capacity.times { buffer.take }
92118

93119
buffer.put 'hi'

0 commit comments

Comments
 (0)