1
0
mirror of https://github.com/meineerde/rackstash.git synced 2026-01-31 17:27:13 +00:00

Automatically flush and clear a non-buffering Buffer after adding a message

This commit is contained in:
Holger Just 2017-02-03 19:06:39 +01:00
parent cb4cdadd95
commit e842d5c771
2 changed files with 52 additions and 1 deletions

View File

@ -52,7 +52,11 @@ module Rackstash
end
# 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
# buffer.
@ -61,6 +65,11 @@ module Rackstash
@messages << message
timestamp(message.time)
unless buffering?
flush
clear
end
message
end

View File

@ -33,6 +33,42 @@ describe Rackstash::Buffer do
buffer.add_message msg
expect(buffer.timestamp).to eql '2016-10-17T10:37:00.000Z'
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
describe '#buffering?' do
@ -99,6 +135,12 @@ describe Rackstash::Buffer do
end
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
before do
buffer.add_message double(message: 'Hello World!', time: Time.now)