From cb4cdadd957ce204edd1e0444f6040c58b455032 Mon Sep 17 00:00:00 2001 From: Holger Just Date: Fri, 3 Feb 2017 19:05:44 +0100 Subject: [PATCH] Add Buffer#flush to flush the current Buffer to the Sink --- lib/rackstash/buffer.rb | 12 ++++++++++ spec/rackstash/buffer_spec.rb | 42 +++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/lib/rackstash/buffer.rb b/lib/rackstash/buffer.rb index 7b14a59..e64d066 100644 --- a/lib/rackstash/buffer.rb +++ b/lib/rackstash/buffer.rb @@ -98,6 +98,18 @@ module Rackstash self end + # Flush the current buffer to the log sink. Does nothing if the buffer is + # not pending. + # + # @return [self,nil] returns `self` if the buffer was flushed, `nil` + # otherwise + def flush + if pending? + @sink.flush(self) + self + end + end + # Return all logged messages on the current buffer. # # @return [Array] the list of messages of the curent buffer diff --git a/spec/rackstash/buffer_spec.rb b/spec/rackstash/buffer_spec.rb index 63aedda..a7f182e 100644 --- a/spec/rackstash/buffer_spec.rb +++ b/spec/rackstash/buffer_spec.rb @@ -98,6 +98,48 @@ describe Rackstash::Buffer do end end + describe '#flush' do + context 'when pending?' do + before do + buffer.add_message double(message: 'Hello World!', time: Time.now) + + # We might call Buffer#flush during the following tests + allow(sink).to receive(:flush).with(buffer).once + end + + it 'flushes the buffer to the sink' do + expect(sink).to receive(:flush).with(buffer).once + buffer.flush + end + + it 'does not clear the buffer' do + expect(buffer).not_to receive(:clear) + buffer.flush + expect(buffer.messages.count).to eql 1 + end + + it 'returns the buffer' do + expect(buffer.flush).to equal buffer + end + end + + context 'when not pending?' do + it 'does not flushes the buffer to the sink' do + expect(sink).not_to receive(:flush) + buffer.flush + end + + it 'does not clear the buffer' do + expect(buffer).not_to receive(:clear) + buffer.flush + end + + it 'returns nil' do + expect(buffer.flush).to be nil + end + end + end + describe '#messages' do it 'returns an array of messages' do msg = double(message: 'Hello World', time: Time.now)