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:
parent
4bbbf25ef8
commit
2b0de27956
@ -51,12 +51,11 @@ module Rackstash
|
||||
# If there was a buffer on the stack and it has pending data, it is flushed
|
||||
# to the {#sink} before it is returned.
|
||||
#
|
||||
# @return [Buffer, nil] the popped and flushed {Buffer} or `nil` if there
|
||||
# was no Buffer in the stack
|
||||
# @return [nil]
|
||||
def flush_and_pop
|
||||
@stack.pop.tap do |buffer|
|
||||
buffer.flush if buffer
|
||||
end
|
||||
buffer = @stack.pop
|
||||
buffer.flush if buffer
|
||||
nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -23,13 +23,12 @@ describe Rackstash::BufferStack do
|
||||
|
||||
it 'adds a new implicit buffer' do
|
||||
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.flush_and_pop).to be nil
|
||||
expect(stack.instance_variable_get(:@stack).count).to eql 0
|
||||
|
||||
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
|
||||
|
||||
@ -63,9 +62,15 @@ describe Rackstash::BufferStack do
|
||||
.to change { stack.instance_variable_get(:@stack).count }.from(1).to(0)
|
||||
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
|
||||
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
|
||||
end
|
||||
|
||||
@ -75,7 +80,7 @@ describe Rackstash::BufferStack do
|
||||
expect(new_buffer).to receive(:flush).once
|
||||
|
||||
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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user