2015-12-31 18:33:35 -05:00
|
|
|
# frozen_string_literal: true
|
2014-12-30 15:54:58 -05:00
|
|
|
require_relative 'helper'
|
2015-09-22 15:48:43 -04:00
|
|
|
|
2013-09-22 17:38:33 -04:00
|
|
|
class TestTesting < Sidekiq::Test
|
2012-02-10 00:46:44 -05:00
|
|
|
describe 'sidekiq testing' do
|
2013-09-20 17:39:11 -04:00
|
|
|
describe 'require/load sidekiq/testing.rb' do
|
|
|
|
before do
|
2015-09-22 15:48:43 -04:00
|
|
|
require 'sidekiq/testing'
|
2012-03-10 14:56:34 -05:00
|
|
|
end
|
|
|
|
|
2013-09-20 17:39:11 -04:00
|
|
|
after do
|
|
|
|
Sidekiq::Testing.disable!
|
2012-03-10 14:56:34 -05:00
|
|
|
end
|
|
|
|
|
2013-09-20 17:39:11 -04:00
|
|
|
it 'enables fake testing' do
|
|
|
|
Sidekiq::Testing.fake!
|
2015-09-22 15:48:43 -04:00
|
|
|
assert Sidekiq::Testing.enabled?
|
|
|
|
assert Sidekiq::Testing.fake?
|
|
|
|
refute Sidekiq::Testing.inline?
|
2012-02-10 00:46:44 -05:00
|
|
|
end
|
2012-03-10 14:56:34 -05:00
|
|
|
|
2013-09-20 17:39:11 -04:00
|
|
|
it 'enables fake testing in a block' do
|
|
|
|
Sidekiq::Testing.disable!
|
2015-09-22 15:48:43 -04:00
|
|
|
assert Sidekiq::Testing.disabled?
|
|
|
|
refute Sidekiq::Testing.fake?
|
2012-03-10 14:56:34 -05:00
|
|
|
|
2013-09-20 17:39:11 -04:00
|
|
|
Sidekiq::Testing.fake! do
|
2015-09-22 15:48:43 -04:00
|
|
|
assert Sidekiq::Testing.enabled?
|
|
|
|
assert Sidekiq::Testing.fake?
|
|
|
|
refute Sidekiq::Testing.inline?
|
2013-09-20 17:39:11 -04:00
|
|
|
end
|
2012-05-02 13:45:05 -04:00
|
|
|
|
2015-09-22 15:48:43 -04:00
|
|
|
refute Sidekiq::Testing.enabled?
|
|
|
|
refute Sidekiq::Testing.fake?
|
2012-05-02 13:45:05 -04:00
|
|
|
end
|
2013-03-28 00:24:47 -04:00
|
|
|
|
2013-09-20 17:39:11 -04:00
|
|
|
it 'disables testing in a block' do
|
|
|
|
Sidekiq::Testing.fake!
|
2015-09-22 15:48:43 -04:00
|
|
|
assert Sidekiq::Testing.fake?
|
2013-07-01 17:31:20 -04:00
|
|
|
|
2013-09-20 17:39:11 -04:00
|
|
|
Sidekiq::Testing.disable! do
|
2015-09-22 15:48:43 -04:00
|
|
|
refute Sidekiq::Testing.fake?
|
|
|
|
assert Sidekiq::Testing.disabled?
|
2013-07-01 17:31:20 -04:00
|
|
|
end
|
|
|
|
|
2015-09-22 15:48:43 -04:00
|
|
|
assert Sidekiq::Testing.fake?
|
|
|
|
assert Sidekiq::Testing.enabled?
|
2013-05-30 14:39:01 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-09-20 17:39:11 -04:00
|
|
|
describe 'require/load sidekiq/testing/inline.rb' do
|
|
|
|
before do
|
2015-09-22 15:48:43 -04:00
|
|
|
require 'sidekiq/testing/inline'
|
2012-10-19 00:16:28 -04:00
|
|
|
end
|
|
|
|
|
2013-09-20 17:39:11 -04:00
|
|
|
after do
|
|
|
|
Sidekiq::Testing.disable!
|
2012-10-19 00:16:28 -04:00
|
|
|
end
|
|
|
|
|
2013-09-20 17:39:11 -04:00
|
|
|
it 'enables inline testing' do
|
|
|
|
Sidekiq::Testing.inline!
|
2015-09-22 15:48:43 -04:00
|
|
|
assert Sidekiq::Testing.enabled?
|
|
|
|
assert Sidekiq::Testing.inline?
|
|
|
|
refute Sidekiq::Testing.fake?
|
2012-11-01 10:49:35 -04:00
|
|
|
end
|
2012-10-19 00:16:28 -04:00
|
|
|
|
2013-09-20 17:39:11 -04:00
|
|
|
it 'enables inline testing in a block' do
|
|
|
|
Sidekiq::Testing.disable!
|
2015-09-22 15:48:43 -04:00
|
|
|
assert Sidekiq::Testing.disabled?
|
|
|
|
refute Sidekiq::Testing.fake?
|
2012-10-19 00:16:28 -04:00
|
|
|
|
2013-09-20 17:39:11 -04:00
|
|
|
Sidekiq::Testing.inline! do
|
2015-09-22 15:48:43 -04:00
|
|
|
assert Sidekiq::Testing.enabled?
|
|
|
|
assert Sidekiq::Testing.inline?
|
2013-09-20 17:39:11 -04:00
|
|
|
end
|
2012-11-01 10:49:35 -04:00
|
|
|
|
2015-09-22 15:48:43 -04:00
|
|
|
refute Sidekiq::Testing.enabled?
|
|
|
|
refute Sidekiq::Testing.inline?
|
|
|
|
refute Sidekiq::Testing.fake?
|
2013-09-20 17:39:11 -04:00
|
|
|
end
|
2012-11-01 10:49:35 -04:00
|
|
|
end
|
2012-02-10 00:46:44 -05:00
|
|
|
end
|
2015-09-22 15:48:43 -04:00
|
|
|
|
|
|
|
describe 'with middleware' do
|
|
|
|
before do
|
|
|
|
require 'sidekiq/testing'
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
Sidekiq::Testing.disable!
|
|
|
|
end
|
|
|
|
|
|
|
|
class AttributeWorker
|
|
|
|
include Sidekiq::Worker
|
|
|
|
class_attribute :count
|
|
|
|
self.count = 0
|
|
|
|
attr_accessor :foo
|
|
|
|
|
|
|
|
def perform
|
|
|
|
self.class.count += 1 if foo == :bar
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class AttributeMiddleware
|
|
|
|
def call(worker, msg, queue)
|
|
|
|
worker.foo = :bar if worker.respond_to?(:foo=)
|
|
|
|
yield
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'wraps the inlined worker with middleware' do
|
|
|
|
Sidekiq::Testing.server_middleware do |chain|
|
|
|
|
chain.add AttributeMiddleware
|
|
|
|
end
|
|
|
|
|
|
|
|
begin
|
|
|
|
Sidekiq::Testing.fake! do
|
|
|
|
AttributeWorker.perform_async
|
|
|
|
assert_equal 0, AttributeWorker.count
|
|
|
|
end
|
|
|
|
|
|
|
|
AttributeWorker.perform_one
|
|
|
|
assert_equal 1, AttributeWorker.count
|
|
|
|
|
|
|
|
Sidekiq::Testing.inline! do
|
|
|
|
AttributeWorker.perform_async
|
|
|
|
assert_equal 2, AttributeWorker.count
|
|
|
|
end
|
|
|
|
ensure
|
|
|
|
Sidekiq::Testing.server_middleware.clear
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-02-10 00:46:44 -05:00
|
|
|
end
|