-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
103 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |