From 3b2798479769c1c24020c65d0caf170423b3591a Mon Sep 17 00:00:00 2001 From: Daijiro Fukuda Date: Wed, 29 May 2024 17:23:35 +0900 Subject: [PATCH] test: fix IOError about Tempfile closed when GC `Tempfile#binmode` returns `File` object, not own `Tempfile` object. So, GC will cause its finalizer and the file can be closed during the test. This is the cause why these tests sometimes fail by `IOError: closed stream`. Signed-off-by: Daijiro Fukuda --- test/plugin/in_tail/test_io_handler.rb | 27 +++++++++++------------ test/plugin/in_tail/test_position_file.rb | 13 +++++------ 2 files changed, 19 insertions(+), 21 deletions(-) diff --git a/test/plugin/in_tail/test_io_handler.rb b/test/plugin/in_tail/test_io_handler.rb index 647c4a55a1..0def36b31c 100644 --- a/test/plugin/in_tail/test_io_handler.rb +++ b/test/plugin/in_tail/test_io_handler.rb @@ -5,20 +5,19 @@ require 'tempfile' class IntailIOHandlerTest < Test::Unit::TestCase - setup do - @file = Tempfile.new('intail_io_handler').binmode - opened_file_metrics = Fluent::Plugin::LocalMetrics.new - opened_file_metrics.configure(config_element('metrics', '', {})) - closed_file_metrics = Fluent::Plugin::LocalMetrics.new - closed_file_metrics.configure(config_element('metrics', '', {})) - rotated_file_metrics = Fluent::Plugin::LocalMetrics.new - rotated_file_metrics.configure(config_element('metrics', '', {})) - @metrics = Fluent::Plugin::TailInput::MetricsInfo.new(opened_file_metrics, closed_file_metrics, rotated_file_metrics) - end - - teardown do - @file.close rescue nil - @file.unlink rescue nil + def setup + Tempfile.create('intail_io_handler') do |file| + file.binmode + @file = file + opened_file_metrics = Fluent::Plugin::LocalMetrics.new + opened_file_metrics.configure(config_element('metrics', '', {})) + closed_file_metrics = Fluent::Plugin::LocalMetrics.new + closed_file_metrics.configure(config_element('metrics', '', {})) + rotated_file_metrics = Fluent::Plugin::LocalMetrics.new + rotated_file_metrics.configure(config_element('metrics', '', {})) + @metrics = Fluent::Plugin::TailInput::MetricsInfo.new(opened_file_metrics, closed_file_metrics, rotated_file_metrics) + yield + end end def create_target_info diff --git a/test/plugin/in_tail/test_position_file.rb b/test/plugin/in_tail/test_position_file.rb index af692fdee5..b1957f7353 100644 --- a/test/plugin/in_tail/test_position_file.rb +++ b/test/plugin/in_tail/test_position_file.rb @@ -6,13 +6,12 @@ require 'tempfile' class IntailPositionFileTest < Test::Unit::TestCase - setup do - @file = Tempfile.new('intail_position_file_test').binmode - end - - teardown do - @file.close rescue nil - @file.unlink rescue nil + def setup + Tempfile.create('intail_position_file_test') do |file| + file.binmode + @file = file + yield + end end UNWATCHED_STR = '%016x' % Fluent::Plugin::TailInput::PositionFile::UNWATCHED_POSITION