From 04daf3481441001dd61901bef44a1bee3204fe54 Mon Sep 17 00:00:00 2001 From: ganmacs Date: Fri, 19 Aug 2016 17:53:55 +0900 Subject: [PATCH] Add Compressabl test --- test/plugin/test_compressable.rb | 103 +++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 test/plugin/test_compressable.rb diff --git a/test/plugin/test_compressable.rb b/test/plugin/test_compressable.rb new file mode 100644 index 0000000000..f9fdf4c2e7 --- /dev/null +++ b/test/plugin/test_compressable.rb @@ -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