Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ActiveJob] test adapter is override by self.enque_adapter = :sidekiq #26360

Closed
zw963 opened this issue Sep 2, 2016 · 5 comments
Closed

[ActiveJob] test adapter is override by self.enque_adapter = :sidekiq #26360

zw963 opened this issue Sep 2, 2016 · 5 comments

Comments

@zw963
Copy link

zw963 commented Sep 2, 2016

Steps to reproduce

class BidGoodsItemExpiredJob < ApplicationJob
  self.queue_adapter = :sidekiq
  def perform
       ....
  end
end

This will result in following test code not work.

assert_enqueued_with(job: BidGoodsItemExpiredJob) do
      BidGoodsItemExpiredJob.set(wait_until: bid_goods_item.bid_expired_at).perform_later(bid_goods_item.id)
end

We must change job code to following to make test work.

class BidGoodsItemExpiredJob < ApplicationJob
  self.queue_adapter = :sidekiq unless rails.env.test?
  def perform
       ....
  end
end

It weired, spent me hours to found out test use
ActiveJob::QueueAdapters::TestAdapter to make test work.

Expected behavior

self.queue_adapter = :sidekiq in job should not change the default
test adapter ... it only test enque, not test enque to sidekiq.

System configuration

Rails 5.0.0
Ruby 2.2.4

@zw963 zw963 changed the title ActiveJob test adapter is override by self.enque_adapter = :sidekiq [ActiveJob] test adapter is override by self.enque_adapter = :sidekiq Sep 2, 2016
@prathamesh-sonpatki
Copy link
Member

I tried to reproduce the issue but could not. See reproduction script here ~ https://gist.github.com/prathamesh-sonpatki/4dd96cee548758bf63fd37004d6404a0.

Can you upload a sample application on Github that reproduces the issue? Thanks.

@prathamesh-sonpatki
Copy link
Member

@zw963 You can solve this issue by providing custom adapter for the test class by using queue_adapter_for_test method.

For eg.

class JobTest < ActiveJob::TestCase
  def queue_adapter_for_test
    ActiveJob::QueueAdapters::SidekiqAdapter
  end
end

You can check more documentation about this method here - http://api.rubyonrails.org/classes/ActiveJob/TestHelper.html#method-i-queue_adapter_for_test.

I am giving it a close as we already have a solution. Thanks!

@zw963
Copy link
Author

zw963 commented Sep 5, 2016

I tried to reproduce the issue but could not. See reproduction script here ~ https://gist.github.com/prathamesh-sonpatki/4dd96cee548758bf63fd37004d6404a0.

I could not too, but this is not the same case as my.

You can solve this issue by providing custom adapter for the test class by using queue_adapter_for_test method.

Sorry not worked for me.

# Running:

DEPRECATION WARNING: Passing an adapter class is deprecated and will be removed in Rails 5.1. Please pass an adapter name (.queue_adapter = :sidekiq) or an instance (.queue_adapter = ActiveJob::QueueAdapters::SidekiqAdapter.new) instead. (called from <top (required)> at /home/zw963/Xthink/ershou_web/bin/rails:9)
E

Error:
BidGoodsItemExpiredJobTest#test_in_queue:
NoMethodError: undefined method `enqueued_jobs' for #<ActiveJob::QueueAdapters::SidekiqAdapter:0x000000037cc6f0>


Error:
BidGoodsItemExpiredJobTest#test_in_queue:
NoMethodError: undefined method `each' for nil:NilClass

I change to following:

class JobTest < ActiveJob::TestCase
  def queue_adapter_for_test
    ActiveJob::QueueAdapters::SidekiqAdapter
  end
end

DEPRECATION WARNING is gone, but still got following error.

Error:
BidGoodsItemExpiredJobTest#test_: when no one biding should change to no_dealt. :
NoMethodError: undefined method `enqueued_jobs' for #<ActiveJob::QueueAdapters::SidekiqAdapter:0x0000000375f708>


Error:
BidGoodsItemExpiredJobTest#test_: when no one biding should change to no_dealt. :
NoMethodError: undefined method `each' for nil:NilClass

here is my job file: https://gist.github.com/208385fb2e6f53ef107509cc58073d68
here is my job test file: https://gist.github.com/068623e22d8b66d5334b082ff3fba18f

Thanks.

@prathamesh-sonpatki
Copy link
Member

I will take a look today, thanks for the explanation.

@prathamesh-sonpatki prathamesh-sonpatki self-assigned this Sep 6, 2016
y-yagi added a commit to y-yagi/rails that referenced this issue Jan 27, 2017
The `ActiveJob::TestHelper` replace the adapter to test adapter in
`before_setup`. It gets the target class using the `descendants`, but if
the test target job class is not loaded, will not be a replacement of
the adapter.
Therefore, instead of replacing with `before_setup`, modified to
replace when setting adapter.

Fixes rails#26360
arthurnn pushed a commit that referenced this issue Jan 31, 2017
…job (#26690)

The `ActiveJob::TestHelper` replace the adapter to test adapter in
`before_setup`. It gets the target class using the `descendants`, but if
the test target job class is not loaded, will not be a replacement of
the adapter.
Therefore, instead of replacing with `before_setup`, modified to
replace when setting adapter.

Fixes #26360
@zw963
Copy link
Author

zw963 commented Feb 3, 2017

Thank all!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants