2015-12-31 18:33:35 -05:00
|
|
|
# frozen_string_literal: true
|
2014-12-30 15:54:58 -05:00
|
|
|
require_relative 'helper'
|
2017-01-04 13:30:42 -05:00
|
|
|
require 'sidekiq/api'
|
2012-02-19 00:08:53 -05:00
|
|
|
require 'active_record'
|
|
|
|
require 'action_mailer'
|
2017-01-04 13:30:42 -05:00
|
|
|
Sidekiq::Extensions.enable_delay!
|
2012-02-19 00:08:53 -05:00
|
|
|
|
2019-02-28 15:43:50 -05:00
|
|
|
describe Sidekiq::Extensions do
|
|
|
|
before do
|
|
|
|
Sidekiq.redis {|c| c.flushdb }
|
|
|
|
end
|
2012-02-19 00:08:53 -05:00
|
|
|
|
2019-02-28 15:43:50 -05:00
|
|
|
class MyModel < ActiveRecord::Base
|
|
|
|
def self.long_class_method
|
|
|
|
raise "Should not be called!"
|
2012-02-19 00:08:53 -05:00
|
|
|
end
|
2019-02-28 15:43:50 -05:00
|
|
|
end
|
2012-02-19 00:08:53 -05:00
|
|
|
|
2019-02-28 15:43:50 -05:00
|
|
|
it 'allows delayed execution of ActiveRecord class methods' do
|
|
|
|
assert_equal [], Sidekiq::Queue.all.map(&:name)
|
|
|
|
q = Sidekiq::Queue.new
|
|
|
|
assert_equal 0, q.size
|
|
|
|
MyModel.delay.long_class_method
|
|
|
|
assert_equal ['default'], Sidekiq::Queue.all.map(&:name)
|
|
|
|
assert_equal 1, q.size
|
|
|
|
end
|
2012-02-19 00:08:53 -05:00
|
|
|
|
2019-02-28 15:43:50 -05:00
|
|
|
it 'uses and stringifies specified options' do
|
|
|
|
assert_equal [], Sidekiq::Queue.all.map(&:name)
|
|
|
|
q = Sidekiq::Queue.new('notdefault')
|
|
|
|
assert_equal 0, q.size
|
|
|
|
MyModel.delay(queue: :notdefault).long_class_method
|
|
|
|
assert_equal ['notdefault'], Sidekiq::Queue.all.map(&:name)
|
|
|
|
assert_equal 1, q.size
|
|
|
|
end
|
2012-10-17 23:27:54 -04:00
|
|
|
|
2019-02-28 15:43:50 -05:00
|
|
|
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-05-25 23:21:42 -04:00
|
|
|
|
2019-02-28 15:43:50 -05:00
|
|
|
it 'allows until delayed scheduling of AR class methods' do
|
|
|
|
ss = Sidekiq::ScheduledSet.new
|
|
|
|
assert_equal 0, ss.size
|
|
|
|
MyModel.delay_until(1.day.from_now).long_class_method
|
|
|
|
assert_equal 1, ss.size
|
|
|
|
end
|
2012-11-08 12:05:11 -05:00
|
|
|
|
2019-02-28 15:43:50 -05:00
|
|
|
class UserMailer < ActionMailer::Base
|
|
|
|
def greetings(a, b)
|
|
|
|
raise "Should not be called!"
|
2012-02-19 00:08:53 -05:00
|
|
|
end
|
2019-02-28 15:43:50 -05:00
|
|
|
end
|
2012-02-19 00:08:53 -05:00
|
|
|
|
2019-02-28 15:43:50 -05:00
|
|
|
it 'allows delayed delivery of ActionMailer mails' do
|
|
|
|
assert_equal [], Sidekiq::Queue.all.map(&:name)
|
|
|
|
q = Sidekiq::Queue.new
|
|
|
|
assert_equal 0, q.size
|
|
|
|
UserMailer.delay.greetings(1, 2)
|
|
|
|
assert_equal ['default'], Sidekiq::Queue.all.map(&:name)
|
|
|
|
assert_equal 1, q.size
|
|
|
|
end
|
2012-05-25 23:21:42 -04:00
|
|
|
|
2019-02-28 15:43:50 -05:00
|
|
|
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-08-16 19:43:01 -04:00
|
|
|
|
2019-02-28 15:43:50 -05:00
|
|
|
it 'allows until delay scheduling of AM mails' do
|
|
|
|
ss = Sidekiq::ScheduledSet.new
|
|
|
|
assert_equal 0, ss.size
|
|
|
|
UserMailer.delay_until(5.days.from_now).greetings(1, 2)
|
|
|
|
assert_equal 1, ss.size
|
|
|
|
end
|
2012-11-08 12:05:11 -05:00
|
|
|
|
2019-02-28 15:43:50 -05:00
|
|
|
class SomeClass
|
|
|
|
def self.doit(arg)
|
2012-08-16 19:43:01 -04:00
|
|
|
end
|
2019-02-28 15:43:50 -05:00
|
|
|
end
|
2012-08-16 19:43:01 -04:00
|
|
|
|
2019-02-28 15:43:50 -05:00
|
|
|
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
|
|
|
|
2019-02-28 15:43:50 -05:00
|
|
|
module SomeModule
|
|
|
|
def self.doit(arg)
|
2012-09-30 12:09:42 -04:00
|
|
|
end
|
2019-02-28 15:43:50 -05:00
|
|
|
end
|
2012-09-30 12:09:42 -04:00
|
|
|
|
2019-02-28 15:43:50 -05:00
|
|
|
it 'logs large payloads' do
|
|
|
|
output = capture_logging(Logger::WARN) do
|
|
|
|
SomeClass.delay.doit('a' * 8192)
|
2017-01-04 13:30:42 -05:00
|
|
|
end
|
2019-02-28 15:43:50 -05:00
|
|
|
assert_match(/#{SomeClass}.doit job argument is/, output)
|
|
|
|
end
|
2017-01-04 13:30:42 -05:00
|
|
|
|
2019-02-28 15:43:50 -05:00
|
|
|
it 'allows delay of any module class method' do
|
|
|
|
q = Sidekiq::Queue.new
|
|
|
|
assert_equal 0, q.size
|
|
|
|
SomeModule.delay.doit(Date.today)
|
|
|
|
assert_equal 1, q.size
|
|
|
|
end
|
2014-06-28 18:19:42 -04:00
|
|
|
|
2019-02-28 15:43:50 -05:00
|
|
|
it 'allows Psych to serialize anonymous structs' do
|
|
|
|
obj = Struct.new(:attribute).new(my: 'data')
|
2017-07-29 02:26:44 -04:00
|
|
|
|
2019-02-28 15:43:50 -05:00
|
|
|
assert_equal obj.attribute, Psych.load(Psych.dump(obj)).attribute
|
2012-03-26 22:56:48 -04:00
|
|
|
end
|
2012-02-19 00:08:53 -05:00
|
|
|
end
|