1
0
mirror of https://github.com/meineerde/rackstash.git synced 2025-10-17 14:01:01 +00:00

Return nil on BufferStacl#flush_and_pop

The buffer should not be reused after a flush anyway. By not returning
it, we ensure that it does not leak outside.
This commit is contained in:
Holger Just 2017-04-27 22:04:11 +02:00
parent 4bbbf25ef8
commit 2b0de27956
2 changed files with 16 additions and 12 deletions

View File

@ -51,12 +51,11 @@ module Rackstash
# If there was a buffer on the stack and it has pending data, it is flushed # If there was a buffer on the stack and it has pending data, it is flushed
# to the {#sink} before it is returned. # to the {#sink} before it is returned.
# #
# @return [Buffer, nil] the popped and flushed {Buffer} or `nil` if there # @return [nil]
# was no Buffer in the stack
def flush_and_pop def flush_and_pop
@stack.pop.tap do |buffer| buffer = @stack.pop
buffer.flush if buffer buffer.flush if buffer
end nil
end end
end end
end end

View File

@ -23,13 +23,12 @@ describe Rackstash::BufferStack do
it 'adds a new implicit buffer' do it 'adds a new implicit buffer' do
expect(stack.current).to be_a Rackstash::Buffer expect(stack.current).to be_a Rackstash::Buffer
expect(stack.flush_and_pop).to be_a Rackstash::Buffer stack.flush_and_pop
# no further Buffer on the stack expect(stack.instance_variable_get(:@stack).count).to eql 0
expect(stack.flush_and_pop).to be nil
expect(stack.current).to be_a Rackstash::Buffer expect(stack.current).to be_a Rackstash::Buffer
expect(stack.flush_and_pop).to be_a Rackstash::Buffer expect(stack.instance_variable_get(:@stack).count).to eql 1
end end
end end
@ -63,9 +62,15 @@ describe Rackstash::BufferStack do
.to change { stack.instance_variable_get(:@stack).count }.from(1).to(0) .to change { stack.instance_variable_get(:@stack).count }.from(1).to(0)
end end
it 'returns the removed buffer' do it 'does nothing if there is no buffer' do
expect(stack.instance_variable_get(:@stack).count).to eql 0
expect { stack.flush_and_pop }
.not_to change { stack.instance_variable_get(:@stack) }
end
it 'always returns nil' do
new_buffer = stack.push new_buffer = stack.push
expect(stack.flush_and_pop).to equal new_buffer expect(stack.flush_and_pop).to be nil
expect(stack.flush_and_pop).to be nil expect(stack.flush_and_pop).to be nil
end end
@ -75,7 +80,7 @@ describe Rackstash::BufferStack do
expect(new_buffer).to receive(:flush).once expect(new_buffer).to receive(:flush).once
stack.flush_and_pop stack.flush_and_pop
stack.flush_and_pop # nil, thus `#flush` is not called again stack.flush_and_pop # no further buffer, thus `#flush` is not called again
end end
end end
end end