Skip to content

Commit

Permalink
in_tail_with_throttle: Fix test_io_handler to use Tailwatcher rathe…
Browse files Browse the repository at this point in the history
…r than string

Signed-off-by: Pranjal Gupta <pranjal.gupta2@ibm.com>
  • Loading branch information
Pranjal-Gupta2 committed Oct 20, 2021
1 parent a61f94a commit 0e5e037
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 7 deletions.
16 changes: 12 additions & 4 deletions test/plugin/in_tail/test_io_handler.rb
Expand Up @@ -21,12 +21,20 @@ class IntailIOHandlerTest < Test::Unit::TestCase
@file.unlink rescue nil
end

def create_target_info
Fluent::Plugin::TailInput::TargetInfo.new(@file.path, Fluent::FileWrapper.stat(@file.path).ino)
end

def create_watcher
Fluent::Plugin::TailInput::TailWatcher.new(create_target_info, nil, nil, nil, nil, nil, nil, nil, nil)
end

test '#on_notify load file content and passed it to receive_lines method' do
text = "this line is test\ntest line is test\n"
@file.write(text)
@file.close

watcher = 'watcher'
watcher = create_watcher

update_pos = 0

Expand Down Expand Up @@ -61,7 +69,7 @@ class IntailIOHandlerTest < Test::Unit::TestCase

update_pos = 0

watcher = 'watcher'
watcher = create_watcher
stub(watcher).pe do
pe = 'position_file'
stub(pe).read_pos { 0 }
Expand Down Expand Up @@ -92,7 +100,7 @@ class IntailIOHandlerTest < Test::Unit::TestCase

update_pos = 0

watcher = 'watcher'
watcher = create_watcher
stub(watcher).pe do
pe = 'position_file'
stub(pe).read_pos { 0 }
Expand All @@ -118,7 +126,7 @@ class IntailIOHandlerTest < Test::Unit::TestCase

update_pos = 0

watcher = 'watcher'
watcher = create_watcher
stub(watcher).pe do
pe = 'position_file'
stub(pe).read_pos { 0 }
Expand Down
81 changes: 78 additions & 3 deletions test/plugin/test_in_tail_with_throttle.rb
@@ -1,12 +1,87 @@
require_relative '../helper'
require_relative './test_in_tail'
# require_relative './test_in_tail'
require 'fluent/test'
require 'fluent/test/helpers'
require 'fluent/test/driver/input'
require 'fluent/plugin/in_tail_with_throttle'

class ThrottleInputTest < TailInputTest

class ThrottleInputTest < Test::Unit::TestCase

def setup
Fluent::Test.setup
cleanup_directory(TMP_DIR)
end

def teardown
super
cleanup_directory(TMP_DIR)
Fluent::Engine.stop
end

def cleanup_directory(path)
unless Dir.exist?(path)
FileUtils.mkdir_p(path)
return
end

if Fluent.windows?
Dir.glob("*", base: path).each do |name|
begin
cleanup_file(File.join(path, name))
rescue
# expect test driver block release already owned file handle.
end
end
else
begin
FileUtils.rm_f(path, secure:true)
rescue ArgumentError
FileUtils.rm_f(path) # For Ruby 2.6 or before.
end
if File.exist?(path)
FileUtils.remove_entry_secure(path, true)
end
end
FileUtils.mkdir_p(path)
end

def cleanup_file(path)
if Fluent.windows?
# On Windows, when the file or directory is removed and created
# frequently, there is a case that creating file or directory will
# fail. This situation is caused by pending file or directory
# deletion which is mentioned on win32 API document [1]
# As a workaround, execute rename and remove method.
#
# [1] https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilea#files
#
file = File.join(Dir.tmpdir, SecureRandom.hex(10))
begin
FileUtils.mv(path, file)
FileUtils.rm_rf(file, secure: true)
rescue ArgumentError
FileUtils.rm_rf(file) # For Ruby 2.6 or before.
end
if File.exist?(file)
# ensure files are closed for Windows, on which deleted files
# are still visible from filesystem
GC.start(full_mark: true, immediate_mark: true, immediate_sweep: true)
FileUtils.remove_entry_secure(file, true)
end
else
begin
FileUtils.rm_f(path, secure: true)
rescue ArgumentError
FileUtils.rm_f(path) # For Ruby 2.6 or before.
end
if File.exist?(path)
FileUtils.remove_entry_secure(path, true)
end
end
end

TMP_DIR = File.dirname(__FILE__) + "/../tmp/tail_with_throttle#{ENV['TEST_ENV_NUMBER']}"

def create_group_directive(pattern, rate_period, *rules)
config_element("", "", {}, [
config_element("group", "", {
Expand Down

0 comments on commit 0e5e037

Please sign in to comment.