Skip to content

Commit

Permalink
Merge pull request #47 from Shopify/uk-add-conditional-sorbet-runtime…
Browse files Browse the repository at this point in the history
…-support

Optional `sorbet-runtime` support for `JobIteration::Iteration` interface validation
  • Loading branch information
paracycle authored Feb 25, 2020
2 parents 6018be9 + e923331 commit d469709
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ gem 'mocha'
gem 'rubocop', '~> 0.77.0'
gem 'yard'
gem 'rake'

# for unit testing optional sorbet support
gem 'sorbet-runtime'
13 changes: 12 additions & 1 deletion lib/job-iteration/iteration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ def assert_implements_methods!
end

if respond_to?(:build_enumerator, true)
parameters = method(:build_enumerator).parameters
parameters = method_parameters(:build_enumerator)
unless valid_cursor_parameter?(parameters)
raise ArgumentError, "Iteration job (#{self.class}) #build_enumerator " \
"expects the keyword argument `cursor`"
Expand All @@ -187,6 +187,17 @@ def assert_implements_methods!
end
end

def method_parameters(method_name)
method = method(method_name)

if defined?(T::Private::Methods)
signature = T::Private::Methods.signature_for_method(method)
method = signature.method if signature
end

method.parameters
end

def iteration_instrumentation_tags
{ job_class: self.class.name }
end
Expand Down
20 changes: 20 additions & 0 deletions test/unit/iteration_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require "test_helper"
require "sorbet-runtime"

class JobIterationTest < IterationUnitTest
class JobWithNoMethods < ActiveJob::Base
Expand All @@ -17,6 +18,20 @@ def each_iteration(*)
end
end

class JobWithRightMethodsButWithSorbetSignatures < ActiveJob::Base
extend T::Sig
include JobIteration::Iteration

sig { params(_params: T.untyped, cursor: T.untyped).returns(T::Enumerator[T.untyped]) }
def build_enumerator(_params, cursor:)
enumerator_builder.build_times_enumerator(2, cursor: cursor)
end

sig { params(product: T.untyped, params: T.untyped).void }
def each_iteration(product, params)
end
end

class JobWithRightMethodsButMissingCursorKeywordArgument < ActiveJob::Base
include JobIteration::Iteration
def build_enumerator(params, cursor)
Expand Down Expand Up @@ -53,6 +68,11 @@ def test_jobs_that_define_build_enumerator_and_each_iteration_will_not_raise
work_one_job
end

def test_jobs_that_define_build_enumerator_and_each_iteration_with_sigs_will_not_raise
push(JobWithRightMethodsButWithSorbetSignatures, 'walrus' => 'best')
work_one_job
end

def test_jobs_that_pass_splat_argument_to_build_enumerator_will_not_raise
push(JobWithRightMethodsUsingSplatInTheArguments, {})
work_one_job
Expand Down

0 comments on commit d469709

Please sign in to comment.