Skip to content

Commit

Permalink
test: fix IOError about Tempfile closed when GC
Browse files Browse the repository at this point in the history
`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 <[email protected]>
  • Loading branch information
daipom committed May 29, 2024
1 parent 0d9f3a6 commit 3b27984
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 21 deletions.
27 changes: 13 additions & 14 deletions test/plugin/in_tail/test_io_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 6 additions & 7 deletions test/plugin/in_tail/test_position_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3b27984

Please sign in to comment.