mirror of
https://github.com/meineerde/rackstash.git
synced 2026-03-18 22:38:14 +00:00
Automatically flush and clear a non-buffering Buffer after adding a message
This commit is contained in:
parent
cb4cdadd95
commit
e842d5c771
@ -52,7 +52,11 @@ module Rackstash
|
|||||||
end
|
end
|
||||||
|
|
||||||
# Add a new message to the buffer. This will mark the current buffer as
|
# Add a new message to the buffer. This will mark the current buffer as
|
||||||
# {pending?} and will result in the eventual flush of the logged data.
|
# {pending?} and will result in the eventual flush of the logged data.
|
||||||
|
#
|
||||||
|
# If the buffer is not {#buffering?}, it will be {#flush}ed and {#clear}ed
|
||||||
|
# after each added message. All fields and tags added before the log message
|
||||||
|
# will be flushed along with the single message.
|
||||||
#
|
#
|
||||||
# @param message [Message] A {Message} to add to the current message
|
# @param message [Message] A {Message} to add to the current message
|
||||||
# buffer.
|
# buffer.
|
||||||
@ -61,6 +65,11 @@ module Rackstash
|
|||||||
@messages << message
|
@messages << message
|
||||||
timestamp(message.time)
|
timestamp(message.time)
|
||||||
|
|
||||||
|
unless buffering?
|
||||||
|
flush
|
||||||
|
clear
|
||||||
|
end
|
||||||
|
|
||||||
message
|
message
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -33,6 +33,42 @@ describe Rackstash::Buffer do
|
|||||||
buffer.add_message msg
|
buffer.add_message msg
|
||||||
expect(buffer.timestamp).to eql '2016-10-17T10:37:00.000Z'
|
expect(buffer.timestamp).to eql '2016-10-17T10:37:00.000Z'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context 'when buffering?' do
|
||||||
|
before do
|
||||||
|
buffer_options[:buffering] = true
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'does not call #flush' do
|
||||||
|
expect(buffer).not_to receive(:flush)
|
||||||
|
buffer.add_message double(message: 'Hello World!', time: Time.now)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'does not call #clear' do
|
||||||
|
expect(buffer).not_to receive(:clear)
|
||||||
|
buffer.add_message double(message: 'Hello World!', time: Time.now)
|
||||||
|
expect(buffer.messages.count).to eql 1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when not buffering?' do
|
||||||
|
before do
|
||||||
|
buffer_options[:buffering] = false
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'calls #flush' do
|
||||||
|
expect(buffer).to receive(:flush)
|
||||||
|
buffer.add_message double(message: 'Hello World!', time: Time.now)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'calls #clear' do
|
||||||
|
allow(buffer).to receive(:flush)
|
||||||
|
expect(buffer).to receive(:clear).and_call_original
|
||||||
|
buffer.add_message double(message: 'Hello World!', time: Time.now)
|
||||||
|
expect(buffer.messages.count).to eql 0
|
||||||
|
expect(buffer.pending?).to be false
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe '#buffering?' do
|
describe '#buffering?' do
|
||||||
@ -99,6 +135,12 @@ describe Rackstash::Buffer do
|
|||||||
end
|
end
|
||||||
|
|
||||||
describe '#flush' do
|
describe '#flush' do
|
||||||
|
before do
|
||||||
|
# Create a buffering Buffer to prevent #add_message from flushing the
|
||||||
|
# Buffer on its own.
|
||||||
|
buffer_options[:buffering] = true
|
||||||
|
end
|
||||||
|
|
||||||
context 'when pending?' do
|
context 'when pending?' do
|
||||||
before do
|
before do
|
||||||
buffer.add_message double(message: 'Hello World!', time: Time.now)
|
buffer.add_message double(message: 'Hello World!', time: Time.now)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user