2012-02-19 00:08:53 -05:00
|
|
|
require 'helper'
|
2012-02-19 16:02:32 -05:00
|
|
|
require 'sidekiq'
|
2012-02-19 00:08:53 -05:00
|
|
|
require 'active_record'
|
|
|
|
require 'action_mailer'
|
2012-02-19 16:02:32 -05:00
|
|
|
require 'sidekiq/extensions/action_mailer'
|
|
|
|
require 'sidekiq/extensions/active_record'
|
2012-04-26 11:40:07 -04:00
|
|
|
require 'sidekiq/rails'
|
2012-02-19 16:02:32 -05:00
|
|
|
|
2012-03-10 15:30:15 -05:00
|
|
|
Sidekiq.hook_rails!
|
2012-02-19 00:08:53 -05:00
|
|
|
|
|
|
|
class TestExtensions < MiniTest::Unit::TestCase
|
|
|
|
describe 'sidekiq extensions' do
|
|
|
|
before do
|
2012-03-14 00:19:46 -04:00
|
|
|
Sidekiq.redis = REDIS
|
2012-03-28 22:16:54 -04:00
|
|
|
Sidekiq.redis {|c| c.flushdb }
|
2012-02-19 00:08:53 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
class MyModel < ActiveRecord::Base
|
|
|
|
def self.long_class_method
|
|
|
|
raise "Should not be called!"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-04-29 02:47:27 -04:00
|
|
|
it 'allows delayed execution of ActiveRecord class methods' do
|
2012-03-10 16:07:19 -05:00
|
|
|
assert_equal [], Sidekiq::Client.registered_queues
|
2012-03-28 22:16:54 -04:00
|
|
|
assert_equal 0, Sidekiq.redis {|c| c.llen('queue:default') }
|
2012-02-19 00:08:53 -05:00
|
|
|
MyModel.delay.long_class_method
|
2012-03-10 16:07:19 -05:00
|
|
|
assert_equal ['default'], Sidekiq::Client.registered_queues
|
2012-03-28 22:16:54 -04:00
|
|
|
assert_equal 1, Sidekiq.redis {|c| c.llen('queue:default') }
|
2012-02-19 00:08:53 -05:00
|
|
|
end
|
|
|
|
|
2012-05-25 23:21:42 -04:00
|
|
|
it 'allows delayed scheduling of AR class methods' do
|
|
|
|
assert_equal 0, Sidekiq.redis {|c| c.zcard('schedule') }
|
|
|
|
MyModel.delay_for(5.days).long_class_method
|
|
|
|
assert_equal 1, Sidekiq.redis {|c| c.zcard('schedule') }
|
|
|
|
end
|
|
|
|
|
2012-02-19 00:08:53 -05:00
|
|
|
class UserMailer < ActionMailer::Base
|
|
|
|
def greetings(a, b)
|
|
|
|
raise "Should not be called!"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-02-19 16:02:32 -05:00
|
|
|
it 'allows delayed delivery of ActionMailer mails' do
|
2012-03-10 16:07:19 -05:00
|
|
|
assert_equal [], Sidekiq::Client.registered_queues
|
2012-03-28 22:16:54 -04:00
|
|
|
assert_equal 0, Sidekiq.redis {|c| c.llen('queue:default') }
|
2012-02-19 00:08:53 -05:00
|
|
|
UserMailer.delay.greetings(1, 2)
|
2012-03-10 16:07:19 -05:00
|
|
|
assert_equal ['default'], Sidekiq::Client.registered_queues
|
2012-03-28 22:16:54 -04:00
|
|
|
assert_equal 1, Sidekiq.redis {|c| c.llen('queue:default') }
|
2012-02-19 00:08:53 -05:00
|
|
|
end
|
2012-05-25 23:21:42 -04:00
|
|
|
|
|
|
|
it 'allows delayed scheduling of AM mails' do
|
|
|
|
assert_equal 0, Sidekiq.redis {|c| c.zcard('schedule') }
|
|
|
|
UserMailer.delay_for(5.days).greetings(1, 2)
|
|
|
|
assert_equal 1, Sidekiq.redis {|c| c.zcard('schedule') }
|
|
|
|
end
|
2012-03-26 22:56:48 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'sidekiq rails extensions configuration' do
|
|
|
|
before do
|
|
|
|
@options = Sidekiq.options
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
Sidekiq.options = @options
|
|
|
|
end
|
2012-02-19 00:08:53 -05:00
|
|
|
|
2012-03-26 22:56:48 -04:00
|
|
|
it 'should set enable_rails_extensions option to true by default' do
|
|
|
|
assert Sidekiq.options[:enable_rails_extensions]
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should extend ActiveRecord and ActiveMailer if enable_rails_extensions is true' do
|
|
|
|
assert Sidekiq.hook_rails!
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should not extend ActiveRecord and ActiveMailer if enable_rails_extensions is false' do
|
|
|
|
Sidekiq.options = { :enable_rails_extensions => false }
|
|
|
|
refute Sidekiq.hook_rails!
|
|
|
|
end
|
2012-02-19 00:08:53 -05:00
|
|
|
end
|
|
|
|
end
|