2017-07-09 13:49:52 -04:00
|
|
|
# frozen_string_literal: true
|
2017-07-10 09:40:03 -04:00
|
|
|
|
2016-08-06 12:41:18 -04:00
|
|
|
require "helper"
|
|
|
|
require "jobs/hello_job"
|
2021-01-19 09:57:46 -05:00
|
|
|
require "jobs/enqueue_error_job"
|
2016-08-06 12:41:18 -04:00
|
|
|
require "active_support/core_ext/numeric/time"
|
2014-05-18 06:32:22 -04:00
|
|
|
|
2014-05-18 05:44:28 -04:00
|
|
|
class QueuingTest < ActiveSupport::TestCase
|
2014-05-18 06:32:22 -04:00
|
|
|
setup do
|
2014-08-17 09:23:24 -04:00
|
|
|
JobBuffer.clear
|
2014-05-18 06:32:22 -04:00
|
|
|
end
|
|
|
|
|
2016-08-06 12:41:18 -04:00
|
|
|
test "run queued job" do
|
2014-08-25 10:34:50 -04:00
|
|
|
HelloJob.perform_later
|
2014-08-17 09:23:24 -04:00
|
|
|
assert_equal "David says hello", JobBuffer.last_value
|
2014-05-18 06:32:22 -04:00
|
|
|
end
|
|
|
|
|
2016-08-06 12:41:18 -04:00
|
|
|
test "run queued job with arguments" do
|
2014-08-25 10:34:50 -04:00
|
|
|
HelloJob.perform_later "Jamie"
|
2014-08-17 09:23:24 -04:00
|
|
|
assert_equal "Jamie says hello", JobBuffer.last_value
|
2014-05-18 05:44:28 -04:00
|
|
|
end
|
2014-05-19 18:27:28 -04:00
|
|
|
|
2016-08-06 12:41:18 -04:00
|
|
|
test "run queued job later" do
|
2018-12-20 12:44:01 -05:00
|
|
|
result = HelloJob.set(wait_until: 1.second.ago).perform_later "Jamie"
|
|
|
|
assert result
|
|
|
|
rescue NotImplementedError
|
|
|
|
skip
|
2014-05-19 18:27:28 -04:00
|
|
|
end
|
2014-08-15 17:02:06 -04:00
|
|
|
|
2016-08-06 12:41:18 -04:00
|
|
|
test "job returned by enqueue has the arguments available" do
|
2014-08-25 10:34:50 -04:00
|
|
|
job = HelloJob.perform_later "Jamie"
|
2014-05-22 13:33:23 -04:00
|
|
|
assert_equal [ "Jamie" ], job.arguments
|
|
|
|
end
|
|
|
|
|
2016-08-06 12:41:18 -04:00
|
|
|
test "job returned by perform_at has the timestamp available" do
|
2018-12-20 12:44:01 -05:00
|
|
|
job = HelloJob.set(wait_until: Time.utc(2014, 1, 1)).perform_later
|
|
|
|
assert_equal Time.utc(2014, 1, 1).to_f, job.scheduled_at
|
|
|
|
rescue NotImplementedError
|
|
|
|
skip
|
2014-05-22 13:33:23 -04:00
|
|
|
end
|
2021-01-19 09:57:46 -05:00
|
|
|
|
|
|
|
test "job is yielded to block after enqueue with successfully_enqueued property set" do
|
|
|
|
HelloJob.perform_later "John" do |job|
|
|
|
|
assert_equal "John says hello", JobBuffer.last_value
|
|
|
|
assert_equal [ "John" ], job.arguments
|
|
|
|
assert_equal true, job.successfully_enqueued?
|
|
|
|
assert_nil job.enqueue_error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "when enqueuing raises an EnqueueError job is yielded to block with error set on job" do
|
|
|
|
EnqueueErrorJob.perform_later do |job|
|
|
|
|
assert_equal false, job.successfully_enqueued?
|
|
|
|
assert_equal ActiveJob::EnqueueError, job.enqueue_error.class
|
|
|
|
end
|
|
|
|
end
|
2014-05-18 05:44:28 -04:00
|
|
|
end
|