From ae2a1d5c4c8f9627929b129e9c6c2dd8947b3ba7 Mon Sep 17 00:00:00 2001 From: Christian Menges Date: Wed, 14 Jun 2023 11:51:43 +0200 Subject: [PATCH 1/3] rubocop: Use count instead of select...size Signed-off-by: Christian Menges --- lib/fluent/plugin_helper/thread.rb | 2 +- test/plugin/test_in_forward.rb | 18 ++-- test/plugin/test_in_unix.rb | 4 +- test/plugin/test_multi_output.rb | 2 +- test/plugin/test_out_exec_filter.rb | 4 +- test/plugin/test_out_file.rb | 4 +- test/plugin/test_output.rb | 24 ++--- test/plugin/test_output_as_buffered.rb | 88 +++++++++---------- .../test_output_as_buffered_secondary.rb | 4 +- test/plugin_helper/test_child_process.rb | 2 +- test/test_log.rb | 2 +- 11 files changed, 77 insertions(+), 77 deletions(-) diff --git a/lib/fluent/plugin_helper/thread.rb b/lib/fluent/plugin_helper/thread.rb index 8d725ae5af..2cc0958e39 100644 --- a/lib/fluent/plugin_helper/thread.rb +++ b/lib/fluent/plugin_helper/thread.rb @@ -101,7 +101,7 @@ def thread_create(title) end def thread_exist?(title) - @_threads.values.select{|thread| title == thread[:_fluentd_plugin_helper_thread_title] }.size > 0 + @_threads.values.count{|thread| title == thread[:_fluentd_plugin_helper_thread_title] } > 0 end def thread_started?(title) diff --git a/test/plugin/test_in_forward.rb b/test/plugin/test_in_forward.rb index 5383813e9d..bce0cdc857 100644 --- a/test/plugin/test_in_forward.rb +++ b/test/plugin/test_in_forward.rb @@ -367,7 +367,7 @@ def create_driver(conf=base_config) end logs = d.instance.log.out.logs - assert{ logs.select{|line| line =~ /skip invalid event/ }.size == 2 } + assert{ logs.count{|line| line =~ /skip invalid event/ } == 2 } d.instance_shutdown end @@ -593,10 +593,10 @@ def create_driver(conf=base_config) # check log logs = d.instance.log.logs - assert_equal 1, logs.select{|line| + assert_equal 1, logs.count{|line| line =~ / \[warn\]: Input chunk size is larger than 'chunk_size_warn_limit':/ && line =~ / tag="test.tag" host="#{LOCALHOST_HOSTNAME}" limit=16777216 size=16777501/ - }.size, "large chunk warning is not logged" + }, "large chunk warning is not logged" d.instance_shutdown end @@ -619,10 +619,10 @@ def create_driver(conf=base_config) # check log logs = d.instance.log.logs - assert_equal 1, logs.select{ |line| + assert_equal 1, logs.count{ |line| line =~ / \[warn\]: Input chunk size is larger than 'chunk_size_warn_limit':/ && line =~ / tag="test.tag" host="#{LOCALHOST_HOSTNAME}" limit=16777216 size=16777501/ - }.size, "large chunk warning is not logged" + }, "large chunk warning is not logged" d.instance_shutdown end @@ -653,10 +653,10 @@ def create_driver(conf=base_config) # check log logs = d.instance.log.logs - assert_equal 1, logs.select{|line| + assert_equal 1, logs.count{|line| line =~ / \[warn\]: Input chunk size is larger than 'chunk_size_limit', dropped:/ && line =~ / tag="test.tag" host="#{LOCALHOST_HOSTNAME}" limit=33554432 size=33554989/ - }.size, "large chunk warning is not logged" + }, "large chunk warning is not logged" d.instance_shutdown end @@ -676,9 +676,9 @@ def create_driver(conf=base_config) # check log logs = d.instance.log.logs - assert_equal 1, logs.select{|line| + assert_equal 1, logs.count{|line| line =~ / \[warn\]: incoming chunk is broken: host="#{LOCALHOST_HOSTNAME}" msg=#{data.inspect}/ - }.size, "should not accept broken chunk" + }, "should not accept broken chunk" d.instance_shutdown end diff --git a/test/plugin/test_in_unix.rb b/test/plugin/test_in_unix.rb index 25fa046764..abc6f3ea6a 100644 --- a/test/plugin/test_in_unix.rb +++ b/test/plugin/test_in_unix.rb @@ -174,8 +174,8 @@ def test_broken_message(data) assert_equal 0, @d.events.size logs = @d.instance.log.logs - assert_equal 1, logs.select { |line| + assert_equal 1, logs.count { |line| line =~ / \[warn\]: incoming data is broken: msg=#{data.inspect}/ - }.size, "should not accept broken chunk" + }, "should not accept broken chunk" end end unless Fluent.windows? diff --git a/test/plugin/test_multi_output.rb b/test/plugin/test_multi_output.rb index 2351d1385f..52db9e9161 100644 --- a/test/plugin/test_multi_output.rb +++ b/test/plugin/test_multi_output.rb @@ -150,7 +150,7 @@ def create_output(type=:multi) log_size_for_metrics_plugin_helper = 4 expected_warn_log_size = log_size_for_multi_output_itself + log_size_for_metrics_plugin_helper logs = @i.log.out.logs - assert{ logs.select{|log| log.include?('[warn]') && log.include?("'type' is deprecated parameter name. use '@type' instead.") }.size == expected_warn_log_size } + assert{ logs.count{|log| log.include?('[warn]') && log.include?("'type' is deprecated parameter name. use '@type' instead.") } == expected_warn_log_size } end test '#emit_events calls #process always' do diff --git a/test/plugin/test_out_exec_filter.rb b/test/plugin/test_out_exec_filter.rb index ee91f83079..5a7dc71044 100644 --- a/test/plugin/test_out_exec_filter.rb +++ b/test/plugin/test_out_exec_filter.rb @@ -597,8 +597,8 @@ def create_driver(conf) # the number of pids should be same with number of child processes assert_equal 2, pid_list.size logs = d.instance.log.out.logs - assert_equal 2, logs.select { |l| l.include?('child process exits with error code') }.size - assert_equal 2, logs.select { |l| l.include?('respawning child process') }.size + assert_equal 2, logs.count { |l| l.include?('child process exits with error code') } + assert_equal 2, logs.count { |l| l.include?('respawning child process') } ensure d.run(start: false, shutdown: true) diff --git a/test/plugin/test_out_file.rb b/test/plugin/test_out_file.rb index 099315e92c..5727bc1e69 100644 --- a/test/plugin/test_out_file.rb +++ b/test/plugin/test_out_file.rb @@ -264,8 +264,8 @@ def create_driver(conf = CONFIG, opts = {}) assert !(Dir.exist?("#{TMP_DIR}/my.data/a")) assert !(Dir.exist?("#{TMP_DIR}/your.data/a")) buffer_files = Dir.entries("#{TMP_DIR}/buf_full").reject{|e| e =~ /^\.+$/ } - assert_equal 2, buffer_files.select{|n| n.end_with?('.meta') }.size - assert_equal 2, buffer_files.select{|n| !n.end_with?('.meta') }.size + assert_equal 2, buffer_files.count{|n| n.end_with?('.meta') } + assert_equal 2, buffer_files.count{|n| !n.end_with?('.meta') } m1 = d.instance.metadata('my.data', t1, {"type" => "a"}) m2 = d.instance.metadata('your.data', t3, {"type" => "a"}) diff --git a/test/plugin/test_output.rb b/test/plugin/test_output.rb index 7bbd5cb5a9..2ec11d50c3 100644 --- a/test/plugin/test_output.rb +++ b/test/plugin/test_output.rb @@ -447,25 +447,25 @@ def waiting(seconds) @i.configure(config_element('ROOT', '', {}, [config_element('buffer', '')])) validators = @i.placeholder_validators(:path, "/my/path/${tag}/${username}/file.%Y%m%d_%H%M.log") assert_equal 3, validators.size - assert_equal 1, validators.select(&:time?).size - assert_equal 1, validators.select(&:tag?).size - assert_equal 1, validators.select(&:keys?).size + assert_equal 1, validators.count(&:time?) + assert_equal 1, validators.count(&:tag?) + assert_equal 1, validators.count(&:keys?) end test 'returns validators for time, tag and keys when a plugin is configured with these keys even if a template does not have placeholders' do @i.configure(config_element('ROOT', '', {}, [config_element('buffer', 'time,tag,username', {'timekey' => 60})])) validators = @i.placeholder_validators(:path, "/my/path/file.log") assert_equal 3, validators.size - assert_equal 1, validators.select(&:time?).size - assert_equal 1, validators.select(&:tag?).size - assert_equal 1, validators.select(&:keys?).size + assert_equal 1, validators.count(&:time?) + assert_equal 1, validators.count(&:tag?) + assert_equal 1, validators.count(&:keys?) end test 'returns a validator for time if a template has timestamp placeholders' do @i.configure(config_element('ROOT', '', {}, [config_element('buffer', '')])) validators = @i.placeholder_validators(:path, "/my/path/file.%Y-%m-%d.log") assert_equal 1, validators.size - assert_equal 1, validators.select(&:time?).size + assert_equal 1, validators.count(&:time?) assert_raise Fluent::ConfigError.new("Parameter 'path: /my/path/file.%Y-%m-%d.log' has timestamp placeholders, but chunk key 'time' is not configured") do validators.first.validate! end @@ -475,7 +475,7 @@ def waiting(seconds) @i.configure(config_element('ROOT', '', {}, [config_element('buffer', 'time', {'timekey' => '30'})])) validators = @i.placeholder_validators(:path, "/my/path/to/file.log") assert_equal 1, validators.size - assert_equal 1, validators.select(&:time?).size + assert_equal 1, validators.count(&:time?) assert_raise Fluent::ConfigError.new("Parameter 'path: /my/path/to/file.log' doesn't have timestamp placeholders for timekey 30") do validators.first.validate! end @@ -485,7 +485,7 @@ def waiting(seconds) @i.configure(config_element('ROOT', '', {}, [config_element('buffer', '')])) validators = @i.placeholder_validators(:path, "/my/path/${tag}/file.log") assert_equal 1, validators.size - assert_equal 1, validators.select(&:tag?).size + assert_equal 1, validators.count(&:tag?) assert_raise Fluent::ConfigError.new("Parameter 'path: /my/path/${tag}/file.log' has tag placeholders, but chunk key 'tag' is not configured") do validators.first.validate! end @@ -495,7 +495,7 @@ def waiting(seconds) @i.configure(config_element('ROOT', '', {}, [config_element('buffer', 'tag')])) validators = @i.placeholder_validators(:path, "/my/path/file.log") assert_equal 1, validators.size - assert_equal 1, validators.select(&:tag?).size + assert_equal 1, validators.count(&:tag?) assert_raise Fluent::ConfigError.new("Parameter 'path: /my/path/file.log' doesn't have tag placeholder") do validators.first.validate! end @@ -505,7 +505,7 @@ def waiting(seconds) @i.configure(config_element('ROOT', '', {}, [config_element('buffer', '')])) validators = @i.placeholder_validators(:path, "/my/path/${username}/file.${group}.log") assert_equal 1, validators.size - assert_equal 1, validators.select(&:keys?).size + assert_equal 1, validators.count(&:keys?) assert_raise Fluent::ConfigError.new("Parameter 'path: /my/path/${username}/file.${group}.log' has placeholders, but chunk keys doesn't have keys group,username") do validators.first.validate! end @@ -515,7 +515,7 @@ def waiting(seconds) @i.configure(config_element('ROOT', '', {}, [config_element('buffer', 'username,group')])) validators = @i.placeholder_validators(:path, "/my/path/file.log") assert_equal 1, validators.size - assert_equal 1, validators.select(&:keys?).size + assert_equal 1, validators.count(&:keys?) assert_raise Fluent::ConfigError.new("Parameter 'path: /my/path/file.log' doesn't have enough placeholders for keys group,username") do validators.first.validate! end diff --git a/test/plugin/test_output_as_buffered.rb b/test/plugin/test_output_as_buffered.rb index 9263781318..7b4cc0e04e 100644 --- a/test/plugin/test_output_as_buffered.rb +++ b/test/plugin/test_output_as_buffered.rb @@ -510,7 +510,7 @@ def waiting(seconds) logs = @i.log.out.logs.dup @i.start @i.after_start - assert{ logs.select{|log| log.include?('[warn]') }.size == 0 } + assert{ logs.count{|log| log.include?('[warn]') } == 0 } end test 'a warning reported with 4 chunk keys' do @@ -522,7 +522,7 @@ def waiting(seconds) @i.after_start assert_equal ['key1', 'key2', 'key3', 'key4'], @i.chunk_keys - assert{ logs.select{|log| log.include?('[warn]: many chunk keys specified, and it may cause too many chunks on your system.') }.size == 1 } + assert{ logs.count{|log| log.include?('[warn]: many chunk keys specified, and it may cause too many chunks on your system.') } == 1 } end test 'a warning reported with 4 chunk keys including "tag"' do @@ -531,7 +531,7 @@ def waiting(seconds) logs = @i.log.out.logs.dup @i.start # this calls `log.reset`... capturing logs about configure must be done before this line @i.after_start - assert{ logs.select{|log| log.include?('[warn]: many chunk keys specified, and it may cause too many chunks on your system.') }.size == 1 } + assert{ logs.count{|log| log.include?('[warn]: many chunk keys specified, and it may cause too many chunks on your system.') } == 1 } end test 'time key is not included for warned chunk keys' do @@ -540,7 +540,7 @@ def waiting(seconds) logs = @i.log.out.logs.dup @i.start @i.after_start - assert{ logs.select{|log| log.include?('[warn]') }.size == 0 } + assert{ logs.count{|log| log.include?('[warn]') } == 0 } end end @@ -968,8 +968,8 @@ def waiting(seconds) waiting(4){ sleep 0.1 until ary.size == 3 } assert_equal 3, ary.size - assert_equal 2, ary.select{|e| e[0] == "test.tag.1" }.size - assert_equal 1, ary.select{|e| e[0] == "test.tag.2" }.size + assert_equal 2, ary.count{|e| e[0] == "test.tag.1" } + assert_equal 1, ary.count{|e| e[0] == "test.tag.2" } Timecop.freeze( Time.parse('2016-04-13 14:04:04 +0900') ) @@ -985,8 +985,8 @@ def waiting(seconds) assert{ @i.buffer.stage.size == 1 && @i.write_count == 2 } assert_equal 9, ary.size - assert_equal 7, ary.select{|e| e[0] == "test.tag.1" }.size - assert_equal 2, ary.select{|e| e[0] == "test.tag.2" }.size + assert_equal 7, ary.count{|e| e[0] == "test.tag.1" } + assert_equal 2, ary.count{|e| e[0] == "test.tag.2" } assert metachecks.all?{|e| e } end @@ -1224,8 +1224,8 @@ def waiting(seconds) # events fulfills a chunk (and queued immediately) assert_equal 5, ary.size - assert_equal 5, ary.select{|e| e[0] == "test.tag.1" }.size - assert_equal 0, ary.select{|e| e[0] == "test.tag.2" }.size + assert_equal 5, ary.count{|e| e[0] == "test.tag.1" } + assert_equal 0, ary.count{|e| e[0] == "test.tag.2" } Timecop.freeze( Time.parse('2016-04-13 14:04:09 +0900') ) @@ -1249,8 +1249,8 @@ def waiting(seconds) assert{ @i.buffer.stage.size == 0 && @i.write_count == 3 } assert_equal 11, ary.size - assert_equal 8, ary.select{|e| e[0] == "test.tag.1" }.size - assert_equal 3, ary.select{|e| e[0] == "test.tag.2" }.size + assert_equal 8, ary.count{|e| e[0] == "test.tag.1" } + assert_equal 3, ary.count{|e| e[0] == "test.tag.2" } assert metachecks.all?{|e| e } end @@ -1315,8 +1315,8 @@ def waiting(seconds) # events fulfills a chunk (and queued immediately) assert_equal 5, ary.size - assert_equal 5, ary.select{|e| e[0] == "test.tag.1" }.size - assert_equal 0, ary.select{|e| e[0] == "test.tag.2" }.size + assert_equal 5, ary.count{|e| e[0] == "test.tag.1" } + assert_equal 0, ary.count{|e| e[0] == "test.tag.2" } @i.stop @i.before_shutdown @@ -1330,8 +1330,8 @@ def waiting(seconds) assert{ @i.buffer.stage.size == 0 && @i.buffer.queue.size == 0 && @i.write_count == 3 } assert_equal 11, ary.size - assert_equal 8, ary.select{|e| e[0] == "test.tag.1" }.size - assert_equal 3, ary.select{|e| e[0] == "test.tag.2" }.size + assert_equal 8, ary.count{|e| e[0] == "test.tag.1" } + assert_equal 3, ary.count{|e| e[0] == "test.tag.2" } assert metachecks.all?{|e| e } end @@ -1435,8 +1435,8 @@ def waiting(seconds) # events fulfills a chunk (and queued immediately) assert_equal 5, ary.size - assert_equal 5, ary.select{|e| e[0] == "test.tag.1" }.size - assert_equal 0, ary.select{|e| e[0] == "test.tag.2" }.size + assert_equal 5, ary.count{|e| e[0] == "test.tag.1" } + assert_equal 0, ary.count{|e| e[0] == "test.tag.2" } assert ary[0...5].all?{|e| e[2]["name"] == "xxx" && e[2]["service"] == "a" } Timecop.freeze( Time.parse('2016-04-13 14:04:09 +0900') ) @@ -1465,11 +1465,11 @@ def waiting(seconds) assert{ @i.buffer.stage.size == 0 && @i.write_count == 4 } assert_equal 11, ary.size - assert_equal 8, ary.select{|e| e[0] == "test.tag.1" }.size - assert_equal 3, ary.select{|e| e[0] == "test.tag.2" }.size - assert_equal 6, ary.select{|e| e[2]["name"] == "xxx" && e[2]["service"] == "a" }.size - assert_equal 3, ary.select{|e| e[2]["name"] == "yyy" && e[2]["service"] == "a" }.size - assert_equal 2, ary.select{|e| e[2]["name"] == "xxx" && e[2]["service"] == "b" }.size + assert_equal 8, ary.count{|e| e[0] == "test.tag.1" } + assert_equal 3, ary.count{|e| e[0] == "test.tag.2" } + assert_equal 6, ary.count{|e| e[2]["name"] == "xxx" && e[2]["service"] == "a" } + assert_equal 3, ary.count{|e| e[2]["name"] == "yyy" && e[2]["service"] == "a" } + assert_equal 2, ary.count{|e| e[2]["name"] == "xxx" && e[2]["service"] == "b" } assert metachecks.all?{|e| e } end @@ -1525,8 +1525,8 @@ def waiting(seconds) # events fulfills a chunk (and queued immediately) assert_equal 5, ary.size - assert_equal 5, ary.select{|e| e[0] == "test.tag.1" }.size - assert_equal 0, ary.select{|e| e[0] == "test.tag.2" }.size + assert_equal 5, ary.count{|e| e[0] == "test.tag.1" } + assert_equal 0, ary.count{|e| e[0] == "test.tag.2" } @i.stop @i.before_shutdown @@ -1540,11 +1540,11 @@ def waiting(seconds) assert{ @i.buffer.stage.size == 0 && @i.buffer.queue.size == 0 && @i.write_count == 4 } assert_equal 11, ary.size - assert_equal 8, ary.select{|e| e[0] == "test.tag.1" }.size - assert_equal 3, ary.select{|e| e[0] == "test.tag.2" }.size - assert_equal 6, ary.select{|e| e[2]["name"] == "xxx" && e[2]["service"] == "a" }.size - assert_equal 3, ary.select{|e| e[2]["name"] == "yyy" && e[2]["service"] == "a" }.size - assert_equal 2, ary.select{|e| e[2]["name"] == "xxx" && e[2]["service"] == "b" }.size + assert_equal 8, ary.count{|e| e[0] == "test.tag.1" } + assert_equal 3, ary.count{|e| e[0] == "test.tag.2" } + assert_equal 6, ary.count{|e| e[2]["name"] == "xxx" && e[2]["service"] == "a" } + assert_equal 3, ary.count{|e| e[2]["name"] == "yyy" && e[2]["service"] == "a" } + assert_equal 2, ary.count{|e| e[2]["name"] == "xxx" && e[2]["service"] == "b" } assert metachecks.all?{|e| e } end @@ -1683,8 +1683,8 @@ def waiting(seconds) # events fulfills a chunk (and queued immediately) assert_equal 5, ary.size - assert_equal 5, ary.select{|e| e[0] == "test.tag.1" }.size - assert_equal 0, ary.select{|e| e[0] == "test.tag.2" }.size + assert_equal 5, ary.count{|e| e[0] == "test.tag.1" } + assert_equal 0, ary.count{|e| e[0] == "test.tag.2" } assert_equal 1, chunks.size assert !chunks.first.empty? @@ -1716,8 +1716,8 @@ def waiting(seconds) assert{ @i.buffer.dequeued.size == 3 } assert_equal 11, ary.size - assert_equal 8, ary.select{|e| e[0] == "test.tag.1" }.size - assert_equal 3, ary.select{|e| e[0] == "test.tag.2" }.size + assert_equal 8, ary.count{|e| e[0] == "test.tag.1" } + assert_equal 3, ary.count{|e| e[0] == "test.tag.2" } assert_equal 3, chunks.size assert chunks.all?{|c| !c.empty? } @@ -1802,8 +1802,8 @@ def waiting(seconds) # events fulfills a chunk (and queued immediately) assert_equal 5, ary.size - assert_equal 5, ary.select{|e| e[0] == "test.tag.1" }.size - assert_equal 0, ary.select{|e| e[0] == "test.tag.2" }.size + assert_equal 5, ary.count{|e| e[0] == "test.tag.1" } + assert_equal 0, ary.count{|e| e[0] == "test.tag.2" } assert_equal 1, chunks.size assert !chunks.first.empty? @@ -1835,8 +1835,8 @@ def waiting(seconds) assert{ @i.buffer.dequeued.size == 3 } assert_equal 11, ary.size - assert_equal 8, ary.select{|e| e[0] == "test.tag.1" }.size - assert_equal 3, ary.select{|e| e[0] == "test.tag.2" }.size + assert_equal 8, ary.count{|e| e[0] == "test.tag.1" } + assert_equal 3, ary.count{|e| e[0] == "test.tag.2" } assert_equal 3, chunks.size assert chunks.all?{|c| !c.empty? } @@ -1892,8 +1892,8 @@ def waiting(seconds) assert{ @i.write_count == 7 } assert_equal 11, ary.size - assert_equal 8, ary.select{|e| e[0] == "test.tag.1" }.size - assert_equal 3, ary.select{|e| e[0] == "test.tag.2" }.size + assert_equal 8, ary.count{|e| e[0] == "test.tag.1" } + assert_equal 3, ary.count{|e| e[0] == "test.tag.2" } assert{ chunks.size == 3 } assert{ chunks.all?{|c| !c.empty? } } @@ -1963,8 +1963,8 @@ def waiting(seconds) # events fulfills a chunk (and queued immediately) assert_equal 5, ary.size - assert_equal 5, ary.select{|e| e[0] == "test.tag.1" }.size - assert_equal 0, ary.select{|e| e[0] == "test.tag.2" }.size + assert_equal 5, ary.count{|e| e[0] == "test.tag.1" } + assert_equal 0, ary.count{|e| e[0] == "test.tag.2" } assert_equal 1, chunks.size assert !chunks.first.empty? @@ -1999,8 +1999,8 @@ def waiting(seconds) assert{ @i.rollback_count == 0 } assert_equal 11, ary.size - assert_equal 8, ary.select{|e| e[0] == "test.tag.1" }.size - assert_equal 3, ary.select{|e| e[0] == "test.tag.2" }.size + assert_equal 8, ary.count{|e| e[0] == "test.tag.1" } + assert_equal 3, ary.count{|e| e[0] == "test.tag.2" } assert{ chunks.size == 3 } assert{ chunks.all?{|c| !c.empty? } } diff --git a/test/plugin/test_output_as_buffered_secondary.rb b/test/plugin/test_output_as_buffered_secondary.rb index b12bb154a3..c908daf74b 100644 --- a/test/plugin/test_output_as_buffered_secondary.rb +++ b/test/plugin/test_output_as_buffered_secondary.rb @@ -634,8 +634,8 @@ def dummy_event_stream assert @i.retry logs = @i.log.out.logs - waiting(4){ sleep 0.1 until logs.select{|l| l.include?("[warn]: failed to flush the buffer chunk, timeout to commit.") }.size == 2 } - assert{ logs.select{|l| l.include?("[warn]: failed to flush the buffer chunk, timeout to commit.") }.size == 2 } + waiting(4){ sleep 0.1 until logs.count{|l| l.include?("[warn]: failed to flush the buffer chunk, timeout to commit.") } == 2 } + assert{ logs.count{|l| l.include?("[warn]: failed to flush the buffer chunk, timeout to commit.") } == 2 } end test 'retry_wait for secondary is same with one for primary' do diff --git a/test/plugin_helper/test_child_process.rb b/test/plugin_helper/test_child_process.rb index 67eef96afb..4c55c6ec8c 100644 --- a/test/plugin_helper/test_child_process.rb +++ b/test/plugin_helper/test_child_process.rb @@ -559,7 +559,7 @@ def configure(conf) unless Fluent.windows? test 'can specify subprocess name' do io = IO.popen([["cat", "caaaaaaaaaaat"], '-']) - process_naming_enabled = (open("|ps opid,cmd"){|_io| _io.readlines }.select{|line| line.include?("caaaaaaaaaaat") }.size > 0) + process_naming_enabled = (open("|ps opid,cmd"){|_io| _io.readlines }.count{|line| line.include?("caaaaaaaaaaat") } > 0) Process.kill(:TERM, io.pid) rescue nil io.close rescue nil diff --git a/test/test_log.rb b/test/test_log.rb index 29fd2a0ce9..8139162420 100644 --- a/test/test_log.rb +++ b/test/test_log.rb @@ -660,7 +660,7 @@ def test_reopen log.reopen! log.info message - assert { path.read.lines.select{ |line| line.include?(message) }.size == 2 } + assert { path.read.lines.count{ |line| line.include?(message) } == 2 } # Assert reopening the same file. # Especially, on Windows, the filepath is fixed for each process with rotate, # so we need to care about this. From 980987dd49395f173663d8c20b3d3f65d0be97fc Mon Sep 17 00:00:00 2001 From: Christian Menges Date: Wed, 14 Jun 2023 11:58:21 +0200 Subject: [PATCH 2/3] rubocop: Use find instead of select.first Signed-off-by: Christian Menges --- lib/fluent/plugin/in_forward.rb | 2 +- lib/fluent/plugin/in_sample.rb | 2 +- lib/fluent/plugin/out_exec_filter.rb | 2 +- lib/fluent/plugin_helper/thread.rb | 4 ++-- test/plugin/test_output_as_buffered_retries.rb | 2 +- test/plugin_helper/test_child_process.rb | 2 +- 6 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/fluent/plugin/in_forward.rb b/lib/fluent/plugin/in_forward.rb index b93d676f60..eb8b3c629e 100644 --- a/lib/fluent/plugin/in_forward.rb +++ b/lib/fluent/plugin/in_forward.rb @@ -430,7 +430,7 @@ def check_ping(message, remote_addr, user_auth_salt, nonce) end _ping, hostname, shared_key_salt, shared_key_hexdigest, username, password_digest = message - node = @nodes.select{|n| n[:address].include?(remote_addr) rescue false }.first + node = @nodes.find{|n| n[:address].include?(remote_addr) rescue false } if !node && !@security.allow_anonymous_source log.warn "Anonymous client disallowed", address: remote_addr, hostname: hostname return false, "anonymous source host '#{remote_addr}' denied", nil diff --git a/lib/fluent/plugin/in_sample.rb b/lib/fluent/plugin/in_sample.rb index 6f36762e03..ccd918aa10 100644 --- a/lib/fluent/plugin/in_sample.rb +++ b/lib/fluent/plugin/in_sample.rb @@ -64,7 +64,7 @@ def initialize def configure(conf) super @sample_index = 0 - config = conf.elements.select{|e| e.name == 'storage' }.first + config = conf.elements.find{|e| e.name == 'storage' } @storage = storage_create(usage: 'suspend', conf: config, default_type: DEFAULT_STORAGE_TYPE) end diff --git a/lib/fluent/plugin/out_exec_filter.rb b/lib/fluent/plugin/out_exec_filter.rb index 3635239000..e5981d1eec 100644 --- a/lib/fluent/plugin/out_exec_filter.rb +++ b/lib/fluent/plugin/out_exec_filter.rb @@ -187,7 +187,7 @@ def start @rr = 0 exit_callback = ->(status){ - c = @children.select{|child| child.pid == status.pid }.first + c = @children.find{|child| child.pid == status.pid } if c unless self.stopped? log.warn "child process exits with error code", code: status.to_i, status: status.exitstatus, signal: status.termsig diff --git a/lib/fluent/plugin_helper/thread.rb b/lib/fluent/plugin_helper/thread.rb index 2cc0958e39..644642fce3 100644 --- a/lib/fluent/plugin_helper/thread.rb +++ b/lib/fluent/plugin_helper/thread.rb @@ -105,12 +105,12 @@ def thread_exist?(title) end def thread_started?(title) - t = @_threads.values.select{|thread| title == thread[:_fluentd_plugin_helper_thread_title] }.first + t = @_threads.values.find{|thread| title == thread[:_fluentd_plugin_helper_thread_title] } t && t[:_fluentd_plugin_helper_thread_started] end def thread_running?(title) - t = @_threads.values.select{|thread| title == thread[:_fluentd_plugin_helper_thread_title] }.first + t = @_threads.values.find{|thread| title == thread[:_fluentd_plugin_helper_thread_title] } t && t[:_fluentd_plugin_helper_thread_running] end diff --git a/test/plugin/test_output_as_buffered_retries.rb b/test/plugin/test_output_as_buffered_retries.rb index 1fd0307500..5927ecb35c 100644 --- a/test/plugin/test_output_as_buffered_retries.rb +++ b/test/plugin/test_output_as_buffered_retries.rb @@ -93,7 +93,7 @@ def dummy_event_stream end def get_log_time(msg, logs) log_time = nil - log = logs.select{|l| l.include?(msg) }.first + log = logs.find{|l| l.include?(msg) } if log && /^(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} [-+]\d{4}) \[error\]/ =~ log log_time = Time.parse($1) end diff --git a/test/plugin_helper/test_child_process.rb b/test/plugin_helper/test_child_process.rb index 4c55c6ec8c..5d84a0fd06 100644 --- a/test/plugin_helper/test_child_process.rb +++ b/test/plugin_helper/test_child_process.rb @@ -584,7 +584,7 @@ def configure(conf) m.lock pid = pids.first # 16357 sleeeeeeeeeper -e sleep 10; puts "hello" - assert{ proc_lines.select{|line| line =~ /^\s*#{pid}\s/ }.first.strip.split(/\s+/)[1] == "sleeeeeeeeeper" } + assert{ proc_lines.find{|line| line =~ /^\s*#{pid}\s/ }.strip.split(/\s+/)[1] == "sleeeeeeeeeper" } @d.stop; @d.shutdown; @d.close; @d.terminate end end From 01161b21aa0a3018b8f60ebf5ebd169c8b803ae1 Mon Sep 17 00:00:00 2001 From: Christian Menges Date: Wed, 14 Jun 2023 12:04:36 +0200 Subject: [PATCH 3/3] rubocop: Use reverse_each instead of reverse.each Signed-off-by: Christian Menges --- lib/fluent/plugin_helper/event_loop.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/fluent/plugin_helper/event_loop.rb b/lib/fluent/plugin_helper/event_loop.rb index 1ff9946b9a..4cf9320a04 100644 --- a/lib/fluent/plugin_helper/event_loop.rb +++ b/lib/fluent/plugin_helper/event_loop.rb @@ -99,7 +99,7 @@ def start def shutdown @_event_loop_mutex.synchronize do - @_event_loop_attached_watchers.reverse.each do |w| + @_event_loop_attached_watchers.reverse_each do |w| if w.attached? begin w.detach @@ -116,7 +116,7 @@ def shutdown def after_shutdown timeout_at = Fluent::Clock.now + EVENT_LOOP_SHUTDOWN_TIMEOUT @_event_loop_mutex.synchronize do - @_event_loop.watchers.reverse.each do |w| + @_event_loop.watchers.reverse_each do |w| begin w.detach rescue => e