diff --git a/lib/rackstash/buffer.rb b/lib/rackstash/buffer.rb index e64d066..5707105 100644 --- a/lib/rackstash/buffer.rb +++ b/lib/rackstash/buffer.rb @@ -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 diff --git a/spec/rackstash/buffer_spec.rb b/spec/rackstash/buffer_spec.rb index a7f182e..a745f83 100644 --- a/spec/rackstash/buffer_spec.rb +++ b/spec/rackstash/buffer_spec.rb @@ -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)