From c4e6fae37c4caf9ab4907af1c0d5e45f324e7d90 Mon Sep 17 00:00:00 2001 From: Pan Thomakos Date: Tue, 1 May 2012 15:54:04 -0700 Subject: [PATCH 1/2] Added inline testing functionality. Allows sidekiq jobs to run inline in test mode without having to modify the production code. In other words production code can still call the `perform_async` method and tests can still verify that functionality is run inline. --- lib/sidekiq/testing/inline.rb | 37 +++++++++++++++++ test/test_testing_inline.rb | 75 +++++++++++++++++++++++++++++++++++ 2 files changed, 112 insertions(+) create mode 100644 lib/sidekiq/testing/inline.rb create mode 100644 test/test_testing_inline.rb diff --git a/lib/sidekiq/testing/inline.rb b/lib/sidekiq/testing/inline.rb new file mode 100644 index 00000000..d1b76e73 --- /dev/null +++ b/lib/sidekiq/testing/inline.rb @@ -0,0 +1,37 @@ +module Sidekiq + module Worker + + ## + # The Sidekiq inline infrastructure overrides the perform_async so that it + # actually calls perform instead. This allows workers to be run inline in a + # testing environment. + # + # This is similar to `Resque.inline = true` functionality. + # + # Example: + # + # require 'sidekiq/testing/inline' + # + # $external_variable = 0 + # + # class ExternalWorker + # include Sidekiq::Worker + # + # def perform + # $external_variable = 1 + # end + # end + # + # assert_equal 0, $external_variable + # ExternalWorker.perform_async + # assert_equal 1, $external_variable + # + module ClassMethods + alias_method :perform_async_old, :perform_async + def perform_async(*args) + new.perform(*args) + true + end + end + end +end diff --git a/test/test_testing_inline.rb b/test/test_testing_inline.rb new file mode 100644 index 00000000..1a0b5721 --- /dev/null +++ b/test/test_testing_inline.rb @@ -0,0 +1,75 @@ +require 'helper' +require 'sidekiq' +require 'sidekiq/worker' +require 'active_record' +require 'action_mailer' +require 'sidekiq/rails' +require 'sidekiq/extensions/action_mailer' +require 'sidekiq/extensions/active_record' + +Sidekiq.hook_rails! + +class TestInline < MiniTest::Unit::TestCase + describe 'sidekiq inline testing' do + class InlineError < RuntimeError; end + + class InlineWorker + include Sidekiq::Worker + def perform(pass) + raise InlineError unless pass + end + end + + class InlineFooMailer < ActionMailer::Base + def bar(str) + raise InlineError + end + end + + class InlineFooModel < ActiveRecord::Base + def self.bar(str) + raise InlineError + end + end + + before do + load 'sidekiq/testing/inline.rb' + end + + after do + Sidekiq::Worker::ClassMethods.class_eval do + remove_method :perform_async + alias_method :perform_async, :perform_async_old + remove_method :perform_async_old + end + end + + it 'stubs the async call when in testing mode' do + assert InlineWorker.perform_async(true) + + assert_raises InlineError do + InlineWorker.perform_async(false) + end + end + + it 'stubs the delay call on mailers' do + assert_raises InlineError do + InlineFooMailer.delay.bar('three') + end + end + + it 'stubs the delay call on models' do + assert_raises InlineError do + InlineFooModel.delay.bar('three') + end + end + + it 'stubs the enqueue call when in testing mode' do + assert Sidekiq::Client.enqueue(InlineWorker, true) + + assert_raises InlineError do + Sidekiq::Client.enqueue(InlineWorker, false) + end + end + end +end From 92084397a98efd4449dc8dae3080865208b5d213 Mon Sep 17 00:00:00 2001 From: Pan Thomakos Date: Tue, 1 May 2012 18:10:05 -0700 Subject: [PATCH 2/2] Added inline testing change to changelog. --- Changes.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Changes.md b/Changes.md index a5e4f1e2..5c9cb6fd 100644 --- a/Changes.md +++ b/Changes.md @@ -29,6 +29,9 @@ end - In test environments add the `#drain` class method to workers. This method executes all previously queued jobs. (panthomakos) +- Sidekiq workers can be run inline during tests by requiring the + `sidekiq/testing/inline` file. (panthomakos) + 1.1.4 -----------