Skip to content

Commit 8d3b1ed

Browse files
committed
added RingBuffer#peek
1 parent 8a05e22 commit 8d3b1ed

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

lib/concurrent/channel/ring_buffer.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ def take
4747
end
4848
end
4949

50+
def peek
51+
@mutex.synchronize { @buffer[@first] }
52+
end
53+
5054
private
5155

5256
def wait_while_full

spec/concurrent/channel/ring_buffer_spec.rb

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module Concurrent
55
describe RingBuffer do
66

77
let(:capacity) { 3 }
8-
let(:buffer) { RingBuffer.new( capacity ) }
8+
let(:buffer) { RingBuffer.new(capacity) }
99

1010
def fill_buffer
1111
capacity.times { buffer.put 3 }
@@ -111,6 +111,28 @@ def fill_buffer
111111
end
112112
end
113113

114+
describe '#peek' do
115+
context 'buffer empty' do
116+
it 'returns nil when buffer is empty' do
117+
buffer.peek.should be_nil
118+
end
119+
end
120+
121+
context 'not empty' do
122+
123+
before(:each) { buffer.put 'element' }
124+
125+
it 'returns the first value' do
126+
buffer.peek.should eq 'element'
127+
end
128+
129+
it 'does not change buffer' do
130+
buffer.peek
131+
buffer.count.should eq 1
132+
end
133+
end
134+
end
135+
114136
context 'circular condition' do
115137
it 'can filled many times' do
116138
fill_buffer

0 commit comments

Comments
 (0)