1
0
Fork 0
mirror of https://github.com/mperham/sidekiq.git synced 2022-11-09 13:52:34 -05:00
mperham--sidekiq/test/test_extensions.rb

128 lines
3.5 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
require_relative 'helper'
require 'sidekiq'
require 'active_record'
require 'action_mailer'
require 'sidekiq/extensions/action_mailer'
require 'sidekiq/extensions/active_record'
require 'sidekiq/rails'
Sidekiq.hook_rails!
class TestExtensions < Sidekiq::Test
describe 'sidekiq extensions' do
before do
Sidekiq.redis = REDIS
2012-03-28 22:16:54 -04:00
Sidekiq.redis {|c| c.flushdb }
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
2013-10-26 00:07:54 -04:00
assert_equal [], Sidekiq::Queue.all.map(&:name)
q = Sidekiq::Queue.new
assert_equal 0, q.size
MyModel.delay.long_class_method
2013-10-26 00:07:54 -04:00
assert_equal ['default'], Sidekiq::Queue.all.map(&:name)
assert_equal 1, q.size
end
2012-10-17 23:27:54 -04:00
it 'uses and stringifies specified options' do
2013-10-26 00:07:54 -04:00
assert_equal [], Sidekiq::Queue.all.map(&:name)
q = Sidekiq::Queue.new('notdefault')
assert_equal 0, q.size
2012-10-17 23:27:54 -04:00
MyModel.delay(queue: :notdefault).long_class_method
2013-10-26 00:07:54 -04:00
assert_equal ['notdefault'], Sidekiq::Queue.all.map(&:name)
assert_equal 1, q.size
2012-10-17 23:27:54 -04:00
end
it 'allows delayed scheduling of AR class methods' do
ss = Sidekiq::ScheduledSet.new
assert_equal 0, ss.size
MyModel.delay_for(5.days).long_class_method
assert_equal 1, ss.size
end
2012-11-08 12:05:11 -05:00
it 'allows until delayed scheduling of AR class methods' do
ss = Sidekiq::ScheduledSet.new
assert_equal 0, ss.size
2012-11-08 12:05:11 -05:00
MyModel.delay_until(1.day.from_now).long_class_method
assert_equal 1, ss.size
2012-11-08 12:05:11 -05:00
end
class UserMailer < ActionMailer::Base
def greetings(a, b)
raise "Should not be called!"
end
end
it 'allows delayed delivery of ActionMailer mails' do
2013-10-26 00:07:54 -04:00
assert_equal [], Sidekiq::Queue.all.map(&:name)
q = Sidekiq::Queue.new
assert_equal 0, q.size
UserMailer.delay.greetings(1, 2)
2013-10-26 00:07:54 -04:00
assert_equal ['default'], Sidekiq::Queue.all.map(&:name)
assert_equal 1, q.size
end
it 'allows delayed scheduling of AM mails' do
ss = Sidekiq::ScheduledSet.new
assert_equal 0, ss.size
UserMailer.delay_for(5.days).greetings(1, 2)
assert_equal 1, ss.size
end
2012-11-08 12:05:11 -05:00
it 'allows until delay scheduling of AM mails' do
ss = Sidekiq::ScheduledSet.new
assert_equal 0, ss.size
2012-11-08 12:05:11 -05:00
UserMailer.delay_until(5.days.from_now).greetings(1, 2)
assert_equal 1, ss.size
2012-11-08 12:05:11 -05:00
end
class SomeClass
def self.doit(arg)
end
end
it 'allows delay of any ole class method' do
q = Sidekiq::Queue.new
assert_equal 0, q.size
SomeClass.delay.doit(Date.today)
assert_equal 1, q.size
end
2012-09-30 12:09:42 -04:00
module SomeModule
def self.doit(arg)
end
end
it 'allows delay of any module class method' do
q = Sidekiq::Queue.new
assert_equal 0, q.size
2012-09-30 12:09:42 -04:00
SomeModule.delay.doit(Date.today)
assert_equal 1, q.size
2012-09-30 12:09:42 -04:00
end
it 'allows removing of the #delay methods' do
q = Sidekiq::Queue.new
Sidekiq.remove_delay!
assert_equal 0, q.size
assert_raises NoMethodError do
SomeModule.delay.doit(Date.today)
end
Sidekiq.instance_eval { remove_instance_variable :@delay_removed }
# Reload modified modules
load 'sidekiq/extensions/action_mailer.rb'
load 'sidekiq/extensions/active_record.rb'
load 'sidekiq/extensions/generic_proxy.rb'
load 'sidekiq/extensions/class_methods.rb'
end
2012-03-26 22:56:48 -04:00
end
2012-11-08 12:05:11 -05:00
end