Skip to content

Commit

Permalink
test_out_file: Don't extract the content of "test append" as a method
Browse files Browse the repository at this point in the history
Use data driven test instead to make easy to read.

Signed-off-by: Takuro Ashie <[email protected]>
  • Loading branch information
ashie committed Dec 27, 2021
1 parent 5a3151e commit 6539793
Showing 1 changed file with 42 additions and 46 deletions.
88 changes: 42 additions & 46 deletions test/plugin/test_out_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -376,47 +376,6 @@ def create_driver(conf = CONFIG, opts = {})
end
end

def check_events_append(compression)
time = event_time("2011-01-02 13:14:15 UTC")
formatted_lines = %[2011-01-02T13:14:15Z\ttest\t{"a":1}#{@default_newline}] + %[2011-01-02T13:14:15Z\ttest\t{"a":2}#{@default_newline}]

write_once = ->(){
config = %[
path #{TMP_DIR}/out_file_test
utc
append true
<buffer>
timekey_use_utc true
</buffer>
]
if compression
config << " compress gz"
end
d = create_driver(config)
d.run(default_tag: 'test'){
d.feed(time, {"a"=>1})
d.feed(time, {"a"=>2})
}
d.instance.last_written_path
}

log_file_name = "out_file_test.20110102.log"
if compression
log_file_name << ".gz"
end

1.upto(3) do |i|
path = write_once.call
assert_equal "#{TMP_DIR}/#{log_file_name}", path
expect = formatted_lines * i
if compression
check_gzipped_result(path, expect)
else
check_result(path, expect)
end
end
end

def check_gzipped_result(path, expect)
# Zlib::GzipReader has a bug of concatenated file: https://bugs.ruby-lang.org/issues/9790
# Following code from https://www.ruby-forum.com/topic/971591#979520
Expand Down Expand Up @@ -581,12 +540,49 @@ def parse_system(text)
assert_equal 3, Dir.glob("#{TMP_DIR}/out_file_test.*").size
end

test 'append' do
check_events_append(true)
end
data(
"with compression" => true,
"without compression" => false,
)
test 'append' do |compression|
time = event_time("2011-01-02 13:14:15 UTC")
formatted_lines = %[2011-01-02T13:14:15Z\ttest\t{"a":1}#{@default_newline}] + %[2011-01-02T13:14:15Z\ttest\t{"a":2}#{@default_newline}]

write_once = ->(){
config = %[
path #{TMP_DIR}/out_file_test
utc
append true
<buffer>
timekey_use_utc true
</buffer>
]
if compression
config << " compress gz"
end
d = create_driver(config)
d.run(default_tag: 'test'){
d.feed(time, {"a"=>1})
d.feed(time, {"a"=>2})
}
d.instance.last_written_path
}

test 'append without compression' do
check_events_append(false)
log_file_name = "out_file_test.20110102.log"
if compression
log_file_name << ".gz"
end

1.upto(3) do |i|
path = write_once.call
assert_equal "#{TMP_DIR}/#{log_file_name}", path
expect = formatted_lines * i
if compression
check_gzipped_result(path, expect)
else
check_result(path, expect)
end
end
end

test 'append when JST' do
Expand Down

0 comments on commit 6539793

Please sign in to comment.