Skip to content

Commit

Permalink
Merge pull request fluent#4201 from Garfield96/refactoring-1
Browse files Browse the repository at this point in the history
Refactoring: Combine chained method calls
  • Loading branch information
daipom authored Jun 20, 2023
2 parents dcc05e5 + 01161b2 commit 02ae0ae
Show file tree
Hide file tree
Showing 16 changed files with 86 additions and 86 deletions.
2 changes: 1 addition & 1 deletion lib/fluent/plugin/in_forward.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/fluent/plugin/in_sample.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/fluent/plugin/out_exec_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/fluent/plugin_helper/event_loop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions lib/fluent/plugin_helper/thread.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,16 @@ 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)
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

Expand Down
18 changes: 9 additions & 9 deletions test/plugin/test_in_forward.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions test/plugin/test_in_unix.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
2 changes: 1 addition & 1 deletion test/plugin/test_multi_output.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions test/plugin/test_out_exec_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions test/plugin/test_out_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"})
Expand Down
24 changes: 12 additions & 12 deletions test/plugin/test_output.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
Loading

0 comments on commit 02ae0ae

Please sign in to comment.