-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #75 from Shopify/consolidate-integration-tests
Extract common `IntegrationBehaviour` test module
- Loading branch information
Showing
3 changed files
with
101 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
# frozen_string_literal: true | ||
|
||
module IntegrationBehaviour | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
setup do | ||
@original_adapter = ActiveJob::Base.queue_adapter | ||
ActiveJob::Base.queue_adapter = queue_adapter | ||
end | ||
|
||
teardown do | ||
ActiveJob::Base.queue_adapter = @original_adapter | ||
end | ||
|
||
test "interrupts the job" do | ||
IterationJob.perform_later | ||
|
||
start_worker_and_wait | ||
|
||
assert_equal 1, queue_size | ||
assert_equal 0, job_args.dig(0, "cursor_position") | ||
assert_equal 1, job_args.dig(0, "times_interrupted") | ||
|
||
start_worker_and_wait | ||
|
||
assert_equal 1, queue_size | ||
assert_equal 2, job_args.dig(0, "cursor_position") | ||
assert_equal 2, job_args.dig(0, "times_interrupted") | ||
|
||
TerminateJob.perform_later | ||
start_worker_and_wait | ||
|
||
assert_equal 0, queue_size | ||
end | ||
|
||
test "unserializable corruption is prevented" do | ||
# Cursors are serialized as JSON, but not all objects are serializable. | ||
# time = Time.at(0).utc # => 1970-01-01 00:00:00 UTC | ||
# json = JSON.dump(time) # => "\"1970-01-01 00:00:00 UTC\"" | ||
# string = JSON.parse(json) # => "1970-01-01 00:00:00 UTC" | ||
# We serialized a Time, but it was deserialized as a String. | ||
TimeCursorJob.perform_later | ||
TerminateJob.perform_later | ||
start_worker_and_wait | ||
|
||
assert_equal( | ||
JobIteration::Iteration::CursorError.name, | ||
failed_job_error_class_name, | ||
) | ||
end | ||
|
||
private | ||
|
||
# Should return the symbol to use when configuring the adapter | ||
# ActiveJob::Base.queue_adapter = adapter | ||
def adapter | ||
raise NotImplemented, "#{self.class.name} must implement #{__method__}" | ||
end | ||
|
||
# Should start the job worker process and allow it to work the queue | ||
def start_worker_and_wait | ||
raise NotImplemented, "#{self.class.name} must implement #{__method__}" | ||
end | ||
|
||
# Should return the number of jobs currently enqueued for processing | ||
def queue_size | ||
raise NotImplemented, "#{self.class.name} must implement #{__method__}" | ||
end | ||
|
||
# Should return the hash of job arguments belonging to the most recently enqueued job | ||
def job_args | ||
raise NotImplemented, "#{self.class.name} must implement #{__method__}" | ||
end | ||
|
||
# Should return a String matching the name of the error class of the most recently failed job | ||
def failed_job_error_class_name | ||
raise NotImplemented, "#{self.class.name} must implement #{__method__}" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters