Skip to content

Commit

Permalink
Merge pull request #19 from Jack12816/master
Browse files Browse the repository at this point in the history
Added support for the test adapter
  • Loading branch information
y-yagi committed Dec 16, 2017
2 parents 72feb4d + 773c167 commit 66d09ad
Show file tree
Hide file tree
Showing 7 changed files with 204 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
@@ -1,6 +1,6 @@
# ActiveJob::Cancel

`activejob-cancel` provides cancel method to Active Job. Currently it supports only `Sidekiq`, `Delayed Job` and `resque`.
`activejob-cancel` provides cancel method to Active Job. Currently it supports only `Sidekiq`, `Delayed Job`, `resque` and the Active Job `TestAdapter`.

[![Build Status](https://travis-ci.org/y-yagi/activejob-cancel.svg?branch=master)](https://travis-ci.org/y-yagi/activejob-cancel)
[![Gem Version](https://badge.fury.io/rb/activejob-cancel.svg)](http://badge.fury.io/rb/activejob-cancel)
Expand Down
3 changes: 2 additions & 1 deletion lib/active_job/cancel.rb
@@ -1,13 +1,14 @@
require 'active_support'
require 'active_job'
require 'active_job/cancel/queue_adapters'
require 'active_job/cancel/queue_adapters/test_adapter'
require 'active_job/cancel/version'

module ActiveJob
module Cancel
extend ActiveSupport::Concern

SUPPORTED_ADAPTERS = %w(Sidekiq DelayedJob Resque).freeze
SUPPORTED_ADAPTERS = %w(Sidekiq DelayedJob Resque Test).freeze

def cancel
if self.class.can_cancel?
Expand Down
1 change: 1 addition & 0 deletions lib/active_job/cancel/queue_adapters.rb
Expand Up @@ -6,6 +6,7 @@ module QueueAdapters
autoload :SidekiqAdapter
autoload :DelayedJobAdapter
autoload :ResqueAdapter
autoload :TestAdapter
end
end
end
48 changes: 48 additions & 0 deletions lib/active_job/cancel/queue_adapters/test_adapter.rb
@@ -0,0 +1,48 @@
require 'active_job'

module ActiveJob
module QueueAdapters
# Unfortunately we need to monkey patch the Rails TestAdapter class,
# because it does not save the job id on the enqueued_jobs array. We rely
# on a persisted id to fulfill the canceling of any given job id.
class TestAdapter
def initialize
if Gem::Requirement.new('~> 5.0').satisfied_by? ActiveJob.version
require 'active_job/cancel/queue_adapters/test_adapter/rails_5'
elsif Gem::Requirement.new('~> 4.2').satisfied_by? ActiveJob.version
require 'active_job/cancel/queue_adapters/test_adapter/rails_4'
end

super
end
end
end

module Cancel
module QueueAdapters
class TestAdapter
def cancel(job_id, queue_name)
original_count = adapter.enqueued_jobs.count
adapter.enqueued_jobs = reject_job_from_enqueued_jobs(job_id)
(original_count == adapter.enqueued_jobs.count) ? false : true
end

def cancel_by(opts, queue_name)
unless opts[:provider_job_id]
raise ArgumentError, 'Please specify ":provider_job_id"'
end
self.cancel(opts[:provider_job_id], queue_name)
end

private
def adapter
ActiveJob::Base.queue_adapter
end

def reject_job_from_enqueued_jobs(job_id)
adapter.enqueued_jobs.reject { |job| job[:id] == job_id }
end
end
end
end
end
25 changes: 25 additions & 0 deletions lib/active_job/cancel/queue_adapters/test_adapter/rails_4.rb
@@ -0,0 +1,25 @@
module ActiveJob
module QueueAdapters
class TestAdapter
alias original_enqueue enqueue
alias original_enqueue_at enqueue_at

def fixup_last_job(job)
list = perform_enqueued_jobs ? performed_jobs : enqueued_jobs
list.last[:id] = job.job_id
end

def enqueue(job)
result = original_enqueue(job)
fixup_last_job(job)
result
end

def enqueue_at(job, timestamp)
result = original_enqueue_at(job, timestamp)
fixup_last_job(job)
result
end
end
end
end
14 changes: 14 additions & 0 deletions lib/active_job/cancel/queue_adapters/test_adapter/rails_5.rb
@@ -0,0 +1,14 @@
module ActiveJob
module QueueAdapters
class TestAdapter
def job_to_hash(job, extras = {})
{
id: job.job_id,
job: job.class,
args: job.serialize.fetch('arguments'),
queue: job.queue_name
}.merge!(extras)
end
end
end
end
113 changes: 113 additions & 0 deletions test/queue_adapters/test_adapter_test.rb
@@ -0,0 +1,113 @@
require 'test_helper'

module ActiveJob::Cancel::QueueAdapters
class ActiveJob::Cancel::QueueAdapters::TestAdapterTest< Minitest::Test
def setup
ActiveJob::Base.queue_adapter = :test
@hello_job_queue_name = HelloJob.queue_name.call
@fail_job_queue_name = FailJob.queue_name.call
end

def teardown
end

def test_cancel_queued_job_with_instance_method
assert_equal 0, queue.size

job = HelloJob.perform_later
assert_equal 1, queue.size

job.cancel
assert_equal 0, queue.size
ensure
queue.clear
end

def test_cancel_queued_job_with_class_method
assert_equal 0, queue.size

job = HelloJob.perform_later
assert_equal 1, queue.size

HelloJob.cancel(job.job_id)
assert_equal 0, queue.size
ensure
queue.clear
end

def test_cancel_scheduled_job_with_instance_method
assert_equal 0, queue.size

job = HelloJob.set(wait: 30.seconds).perform_later
assert_equal 1, queue.size

job.cancel
assert_equal 0, queue.size
ensure
queue.clear
end

def test_cancel_scheduled_job_with_class_method
assert_equal 0, queue.size

job = HelloJob.set(wait: 30.seconds).perform_later
assert_equal 1, queue.size

HelloJob.cancel(job.job_id)
assert_equal 0, queue.size
ensure
queue.map(&:delete)
end

def test_cancel_with_invalid_id
job = HelloJob.perform_later

HelloJob.cancel(job.job_id.to_i + 1)
assert_equal 1, queue.size
ensure
queue.clear
end

def test_cancel_by_with_invalid_parameters
assert_raises(ArgumentError) { HelloJob.cancel_by(id: 1) }
end

def test_cancel_queued_job_with_provider_job_id
assert_equal 0, queue.size

HelloJob.perform_later
assert_equal 1, queue.size

HelloJob.cancel_by(provider_job_id: queue.map.first[:id])
assert_equal 0, queue.size
ensure
queue.clear
end

def test_cancel_scheduled_job_with_provider_job_id
assert_equal 0, queue.size

HelloJob.set(wait: 30.seconds).perform_later
assert_equal 1, queue.size

HelloJob.cancel_by(provider_job_id: queue.map.first[:id])
assert_equal 0, queue.size
ensure
queue.map(&:delete)
end

def test_cancel_by_with_invalid_job_id
HelloJob.perform_later

refute HelloJob.cancel_by(provider_job_id: queue.map.first[:id].to_i + 1)
assert_equal 1, queue.size
ensure
queue.clear
end

private
def queue
ActiveJob::Base.queue_adapter.enqueued_jobs
end
end
end

0 comments on commit 66d09ad

Please sign in to comment.