From e7f160dc97abf465c4e2d70a46403bf934e5feab Mon Sep 17 00:00:00 2001 From: Brent Snook Date: Tue, 17 Sep 2019 12:20:55 +1000 Subject: [PATCH 1/2] Validate if arity - must be 1. --- lib/clockwork/event.rb | 5 +++-- test/manager_test.rb | 6 ++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/clockwork/event.rb b/lib/clockwork/event.rb index 7e6538f..eef3148 100644 --- a/lib/clockwork/event.rb +++ b/lib/clockwork/event.rb @@ -74,8 +74,9 @@ def run_if?(t) end def validate_if_option(if_option) - if if_option && !if_option.respond_to?(:call) - raise ArgumentError.new(':if expects a callable object, but #{if_option} does not respond to call') + return unless if_option + unless if_option.respond_to?(:call) && if_option.arity == 1 + raise ArgumentError.new(':if expects a callable object that accepts a single argument, but #{if_option} is not') end end end diff --git a/test/manager_test.rb b/test/manager_test.rb index c102c43..47ba7fe 100644 --- a/test/manager_test.rb +++ b/test/manager_test.rb @@ -260,6 +260,12 @@ def assert_wont_run(t) @manager.every(1.second, 'myjob', :if => true) end end + + it ":if does not have an arity of 1 then raise ArgumentError" do + assert_raises(ArgumentError) do + @manager.every(1.second, 'myjob', :if => lambda {}) + end + end end describe "max_threads" do From 115360d7ff505ad2f2d5b19466345d68a31a06d4 Mon Sep 17 00:00:00 2001 From: Brent Snook Date: Tue, 17 Sep 2019 12:47:53 +1000 Subject: [PATCH 2/2] Also check proc responds to :arity --- lib/clockwork/event.rb | 2 +- test/manager_test.rb | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/clockwork/event.rb b/lib/clockwork/event.rb index eef3148..6f7a4b0 100644 --- a/lib/clockwork/event.rb +++ b/lib/clockwork/event.rb @@ -75,7 +75,7 @@ def run_if?(t) def validate_if_option(if_option) return unless if_option - unless if_option.respond_to?(:call) && if_option.arity == 1 + unless if_option.respond_to?(:call) && if_option.respond_to?(:arity) && if_option.arity == 1 raise ArgumentError.new(':if expects a callable object that accepts a single argument, but #{if_option} is not') end end diff --git a/test/manager_test.rb b/test/manager_test.rb index 47ba7fe..fe6a5df 100644 --- a/test/manager_test.rb +++ b/test/manager_test.rb @@ -261,6 +261,16 @@ def assert_wont_run(t) end end + it ":if does not respond to arity then raise ArgumentError" do + assert_raises(ArgumentError) do + callable = Class.new do + def call; end + end + + @manager.every(1.second, 'myjob', :if => callable.new) + end + end + it ":if does not have an arity of 1 then raise ArgumentError" do assert_raises(ArgumentError) do @manager.every(1.second, 'myjob', :if => lambda {})