Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Be sure to detach one-shot timer watcher #1178

Merged
merged 3 commits into from
Aug 24, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/fluent/plugin_helper/timer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class TimerWatcher < Coolio::TimerWatcher
def initialize(title, interval, repeat, log, checker, &callback)
@title = title
@callback = callback
@repeat = repeat
@log = log
@checker = checker
super(interval, repeat)
Expand All @@ -76,8 +77,12 @@ def on_timer
rescue => e
@log.error "Unexpected error raised. Stopping the timer.", title: @title, error: e
@log.error_backtrace
self.detach
detach
@log.error "Timer detached.", title: @title
ensure
if attached?
detach unless @repeat
end
end
end
end
Expand Down
31 changes: 31 additions & 0 deletions test/plugin_helper/test_timer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,35 @@ class Dummy < Fluent::Plugin::TestBase

d1.shutdown; d1.close; d1.terminate
end

test 'can run at once' do
d1 = Dummy.new
d1.configure(config_element())
assert !d1.timer_running?
d1.start
assert d1.timer_running?

waiting_assertion = true
waiting_timer = true
counter = 0
d1.timer_execute(:test, 1, repeat: false) do
sleep(0.1) while waiting_assertion
counter += 1
waiting_timer = false
end

watchers = d1._event_loop.watchers.reject {|w| w.is_a?(Fluent::PluginHelper::EventLoop::DefaultWatcher) }
assert_equal(1, watchers.size)
assert(watchers.first.attached?)

waiting_assertion = false
sleep(0.1) while waiting_timer

assert_equal(1, counter)
assert_false(watchers.first.attached?)
watchers = d1._event_loop.watchers.reject {|w| w.is_a?(Fluent::PluginHelper::EventLoop::DefaultWatcher) }
assert_equal(0, watchers.size)

d1.shutdown; d1.close; d1.terminate
end
end