Skip to content

Commit

Permalink
Add Compressabl test
Browse files Browse the repository at this point in the history
  • Loading branch information
ganmacs committed Aug 19, 2016
1 parent 2b566e7 commit 3504bec
Showing 1 changed file with 103 additions and 0 deletions.
103 changes: 103 additions & 0 deletions test/plugin/test_compressable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
require_relative '../helper'
require 'fluent/plugin/buffer/compressable'
require 'fluent/plugin/buffer/memory_chunk'
require 'fluent/plugin/buffer'
require 'fluent/test/driver/output'

class CompressableTest < Test::Unit::TestCase
class DummyOutputPlugin < Fluent::Plugin::Output; end
class DummyMemoryChunk < Fluent::Plugin::Buffer::MemoryChunk; end

class DummyBuffer < Fluent::Plugin::Buffer
def generate_chunk(metadata)
DummyMemoryChunk.new(metadata)
end
end

def create_buffer(type = nil, buf_params = { 'compress' => true })
conf = config_element('buffer', '', buf_params, [])
if type == :remote
buffer = DummyBuffer.new
buffer.owner = Class.new(DummyOutputPlugin) {
def remote_write?; true; end
}.new
buffer.configure(conf)
buffer
else
buffer = DummyBuffer.new
buffer.owner = DummyOutputPlugin.new
buffer.configure(conf)
buffer
end
end

def create_metadata(timekey=nil, tag=nil, variables=nil)
Fluent::Plugin::Buffer::Metadata.new(timekey, tag, variables)
end

sub_test_case 'check valid module extended' do
test "don't extend when `compress` is false" do
buf = create_buffer(nil, {})
assert !buf.singleton_class.ancestors.include?(Fluent::Plugin::Compressable)
assert !buf.singleton_class.ancestors.include?(Fluent::Plugin::RemoteCompressable)
end

test 'extend when compress is true and `remote_write` method returns false' do
buf = create_buffer
m = create_metadata
chunk = buf.generate_chunk(m)

assert buf.singleton_class.ancestors.include?(Fluent::Plugin::Compressable)
assert chunk.singleton_class.ancestors.include?(Fluent::Plugin::ChunkCompressable)
assert chunk.singleton_class.ancestors.include?(Fluent::Plugin::ChunkUncompressable)
end

test 'extend when compress is true and `remote_write` returns true' do
buf = create_buffer(:remote)
m = create_metadata
chunk = buf.generate_chunk(m)

assert buf.singleton_class.ancestors.include?(Fluent::Plugin::RemoteCompressable)
assert chunk.singleton_class.ancestors.include?(Fluent::Plugin::ChunkCompressable)
assert !chunk.singleton_class.ancestors.include?(Fluent::Plugin::ChunkUncompressable)
end
end

def assert_compressed(expect, result)
assert_equal Zlib::Deflate.deflate(expect), result
end

sub_test_case 'generate compressed data' do
test 'compress and decompress when `remote_write` returns false' do
str = 'compressed data'
buf = create_buffer
m = create_metadata
chunk = buf.generate_chunk(m).tap do |c|
c.concat(str, str.size)
c.commit
end

assert_compressed str, chunk.read

io = StringIO.new
chunk.write_to(io)
assert_equal str, io.string
end

test 'compress when `remote_write` returns true' do
str = 'compressed data'
buf = create_buffer(:remote)
m = create_metadata
chunk = buf.generate_chunk(m).tap do |c|
c.concat(str, str.size)
c.commit
end

assert_compressed str, chunk.read

io = StringIO.new
chunk.write_to(io)
assert_compressed str, io.string
end
end
end

0 comments on commit 3504bec

Please sign in to comment.