2012-02-12 22:53:34 -05:00
|
|
|
require 'helper'
|
|
|
|
require 'sidekiq/cli'
|
2012-02-15 13:56:36 -05:00
|
|
|
require 'tempfile'
|
2012-02-12 22:53:34 -05:00
|
|
|
|
2012-03-08 23:58:51 -05:00
|
|
|
cli = Sidekiq::CLI.instance
|
|
|
|
def cli.die(code)
|
|
|
|
@code = code
|
|
|
|
end
|
|
|
|
|
|
|
|
def cli.valid?
|
|
|
|
!@code
|
|
|
|
end
|
|
|
|
|
2013-09-22 17:38:33 -04:00
|
|
|
class TestCli < Sidekiq::Test
|
2012-02-12 22:53:34 -05:00
|
|
|
describe 'with cli' do
|
2012-03-08 23:58:51 -05:00
|
|
|
|
2012-02-12 22:53:34 -05:00
|
|
|
before do
|
2012-03-08 23:58:51 -05:00
|
|
|
@cli = Sidekiq::CLI.instance
|
2012-02-12 22:53:34 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'blows up with an invalid require' do
|
|
|
|
assert_raises ArgumentError do
|
|
|
|
@cli.parse(['sidekiq', '-r', 'foobar'])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-03-08 23:58:51 -05:00
|
|
|
it 'requires the specified Ruby code' do
|
2012-02-12 22:53:34 -05:00
|
|
|
@cli.parse(['sidekiq', '-r', './test/fake_env.rb'])
|
|
|
|
assert($LOADED_FEATURES.any? { |x| x =~ /fake_env/ })
|
|
|
|
assert @cli.valid?
|
|
|
|
end
|
|
|
|
|
2014-02-01 18:04:20 -05:00
|
|
|
it 'boots rails' do
|
|
|
|
refute defined?(::Rails)
|
|
|
|
@cli.parse(['sidekiq', '-r', './myapp'])
|
|
|
|
assert defined?(::Rails)
|
|
|
|
end
|
|
|
|
|
2012-02-12 22:53:34 -05:00
|
|
|
it 'changes concurrency' do
|
|
|
|
@cli.parse(['sidekiq', '-c', '60', '-r', './test/fake_env.rb'])
|
2012-02-19 16:02:32 -05:00
|
|
|
assert_equal 60, Sidekiq.options[:concurrency]
|
2012-02-12 22:53:34 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'changes queues' do
|
|
|
|
@cli.parse(['sidekiq', '-q', 'foo', '-r', './test/fake_env.rb'])
|
2012-02-19 16:02:32 -05:00
|
|
|
assert_equal ['foo'], Sidekiq.options[:queues]
|
2012-02-12 22:53:34 -05:00
|
|
|
end
|
|
|
|
|
2013-01-06 16:01:30 -05:00
|
|
|
it 'accepts a process index' do
|
|
|
|
@cli.parse(['sidekiq', '-i', '7', '-r', './test/fake_env.rb'])
|
|
|
|
assert_equal 7, Sidekiq.options[:index]
|
|
|
|
end
|
|
|
|
|
2013-04-24 14:48:22 -04:00
|
|
|
it 'accepts a stringy process index' do
|
|
|
|
@cli.parse(['sidekiq', '-i', 'worker.7', '-r', './test/fake_env.rb'])
|
|
|
|
assert_equal 7, Sidekiq.options[:index]
|
|
|
|
end
|
|
|
|
|
2012-07-31 16:58:50 -04:00
|
|
|
it 'sets strictly ordered queues if weights are not present' do
|
2013-12-23 10:32:53 -05:00
|
|
|
@cli.parse(['sidekiq', '-q', 'foo', '-q', 'bar', '-r', './test/fake_env.rb'])
|
|
|
|
assert_equal true, !!Sidekiq.options[:strict]
|
|
|
|
end
|
|
|
|
|
2012-07-31 16:58:50 -04:00
|
|
|
it 'does not set strictly ordered queues if weights are present' do
|
|
|
|
@cli.parse(['sidekiq', '-q', 'foo,3', '-r', './test/fake_env.rb'])
|
|
|
|
assert_equal false, !!Sidekiq.options[:strict]
|
2012-07-27 18:18:11 -04:00
|
|
|
end
|
|
|
|
|
2013-12-23 10:32:53 -05:00
|
|
|
it 'does not set strictly ordered queues if weights are present with multiple queues' do
|
|
|
|
@cli.parse(['sidekiq', '-q', 'foo,3', '-q', 'bar', '-r', './test/fake_env.rb'])
|
|
|
|
assert_equal false, !!Sidekiq.options[:strict]
|
|
|
|
end
|
|
|
|
|
2012-03-08 23:58:51 -05:00
|
|
|
it 'changes timeout' do
|
|
|
|
@cli.parse(['sidekiq', '-t', '30', '-r', './test/fake_env.rb'])
|
|
|
|
assert_equal 30, Sidekiq.options[:timeout]
|
|
|
|
end
|
|
|
|
|
2013-12-25 07:30:20 -05:00
|
|
|
it 'handles multiple queues with weights' do
|
2012-04-03 23:19:29 -04:00
|
|
|
@cli.parse(['sidekiq', '-q', 'foo,3', '-q', 'bar', '-r', './test/fake_env.rb'])
|
2012-07-27 18:18:11 -04:00
|
|
|
assert_equal %w(foo foo foo bar), Sidekiq.options[:queues]
|
|
|
|
end
|
|
|
|
|
2012-08-15 14:46:43 -04:00
|
|
|
it 'handles queues with multi-word names' do
|
2013-12-25 07:30:20 -05:00
|
|
|
@cli.parse(['sidekiq', '-q', 'queue_one', '-q', 'queue-two', '-r', './test/fake_env.rb'])
|
2012-08-15 14:46:43 -04:00
|
|
|
assert_equal %w(queue_one queue-two), Sidekiq.options[:queues]
|
|
|
|
end
|
|
|
|
|
2012-10-25 15:40:42 -04:00
|
|
|
it 'handles queues with dots in the name' do
|
|
|
|
@cli.parse(['sidekiq', '-q', 'foo.bar', '-r', './test/fake_env.rb'])
|
|
|
|
assert_equal ['foo.bar'], Sidekiq.options[:queues]
|
|
|
|
end
|
|
|
|
|
2012-03-14 13:09:46 -04:00
|
|
|
it 'sets verbose' do
|
2012-05-15 22:44:35 -04:00
|
|
|
old = Sidekiq.logger.level
|
2012-03-14 13:09:46 -04:00
|
|
|
@cli.parse(['sidekiq', '-v', '-r', './test/fake_env.rb'])
|
2012-05-15 22:44:35 -04:00
|
|
|
assert_equal Logger::DEBUG, Sidekiq.logger.level
|
2012-03-14 13:09:46 -04:00
|
|
|
# If we leave the logger at DEBUG it'll add a lot of noise to the test output
|
2012-12-12 14:27:27 -05:00
|
|
|
Sidekiq.options.delete(:verbose)
|
2012-05-15 22:44:35 -04:00
|
|
|
Sidekiq.logger.level = old
|
2012-03-14 13:09:46 -04:00
|
|
|
end
|
|
|
|
|
2012-12-11 06:32:22 -05:00
|
|
|
describe 'with logfile' do
|
|
|
|
before do
|
|
|
|
@old_logger = Sidekiq.logger
|
|
|
|
@tmp_log_path = '/tmp/sidekiq.log'
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
Sidekiq.logger = @old_logger
|
|
|
|
Sidekiq.options.delete(:logfile)
|
2014-03-19 20:41:11 -04:00
|
|
|
File.unlink @tmp_log_path if File.exist?(@tmp_log_path)
|
2012-12-11 06:32:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets the logfile path' do
|
|
|
|
@cli.parse(['sidekiq', '-L', @tmp_log_path, '-r', './test/fake_env.rb'])
|
|
|
|
|
|
|
|
assert_equal @tmp_log_path, Sidekiq.options[:logfile]
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates and writes to a logfile' do
|
|
|
|
@cli.parse(['sidekiq', '-L', @tmp_log_path, '-r', './test/fake_env.rb'])
|
|
|
|
|
|
|
|
Sidekiq.logger.info('test message')
|
|
|
|
|
2013-01-18 23:57:26 -05:00
|
|
|
assert_match(/test message/, File.read(@tmp_log_path), "didn't include the log message")
|
2012-12-11 06:32:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'appends messages to a logfile' do
|
|
|
|
File.open(@tmp_log_path, 'w') do |f|
|
|
|
|
f.puts 'already existant log message'
|
|
|
|
end
|
|
|
|
|
|
|
|
@cli.parse(['sidekiq', '-L', @tmp_log_path, '-r', './test/fake_env.rb'])
|
|
|
|
|
|
|
|
Sidekiq.logger.info('test message')
|
|
|
|
|
|
|
|
log_file_content = File.read(@tmp_log_path)
|
2013-01-18 23:57:26 -05:00
|
|
|
assert_match(/already existant/, log_file_content, "didn't include the old message")
|
|
|
|
assert_match(/test message/, log_file_content, "didn't include the new message")
|
2012-12-11 06:32:22 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-02-15 13:56:36 -05:00
|
|
|
describe 'with pidfile' do
|
|
|
|
before do
|
|
|
|
@tmp_file = Tempfile.new('sidekiq-test')
|
|
|
|
@tmp_path = @tmp_file.path
|
|
|
|
@tmp_file.close!
|
2012-02-15 21:13:32 -05:00
|
|
|
|
2012-02-15 14:24:01 -05:00
|
|
|
@cli.parse(['sidekiq', '-P', @tmp_path, '-r', './test/fake_env.rb'])
|
2012-02-15 13:56:36 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
File.unlink @tmp_path if File.exist? @tmp_path
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets pidfile path' do
|
2012-02-19 16:02:32 -05:00
|
|
|
assert_equal @tmp_path, Sidekiq.options[:pidfile]
|
2012-02-15 13:56:36 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'writes pidfile' do
|
|
|
|
assert_equal File.read(@tmp_path).strip.to_i, Process.pid
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-02-15 21:13:32 -05:00
|
|
|
describe 'with config file' do
|
|
|
|
before do
|
|
|
|
@cli.parse(['sidekiq', '-C', './test/config.yml'])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'takes a path' do
|
2012-02-19 16:02:32 -05:00
|
|
|
assert_equal './test/config.yml', Sidekiq.options[:config_file]
|
2012-02-15 21:13:32 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets verbose' do
|
2012-02-19 16:02:32 -05:00
|
|
|
refute Sidekiq.options[:verbose]
|
2012-02-15 21:13:32 -05:00
|
|
|
end
|
2012-02-19 16:02:32 -05:00
|
|
|
|
2012-02-15 21:13:32 -05:00
|
|
|
it 'sets require file' do
|
2012-02-19 16:02:32 -05:00
|
|
|
assert_equal './test/fake_env.rb', Sidekiq.options[:require]
|
2012-02-15 21:13:32 -05:00
|
|
|
end
|
|
|
|
|
2013-10-16 22:24:22 -04:00
|
|
|
it 'does not set environment' do
|
|
|
|
assert_equal nil, Sidekiq.options[:environment]
|
2012-02-15 21:13:32 -05:00
|
|
|
end
|
|
|
|
|
2013-01-18 12:51:16 -05:00
|
|
|
it 'sets concurrency' do
|
|
|
|
assert_equal 50, Sidekiq.options[:concurrency]
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets pid file' do
|
|
|
|
assert_equal '/tmp/sidekiq-config-test.pid', Sidekiq.options[:pidfile]
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets logfile' do
|
|
|
|
assert_equal '/tmp/sidekiq.log', Sidekiq.options[:logfile]
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets queues' do
|
|
|
|
assert_equal 2, Sidekiq.options[:queues].count { |q| q == 'very_often' }
|
|
|
|
assert_equal 1, Sidekiq.options[:queues].count { |q| q == 'seldom' }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'with env based config file' do
|
|
|
|
before do
|
|
|
|
@cli.parse(['sidekiq', '-e', 'staging', '-C', './test/env_based_config.yml'])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'takes a path' do
|
|
|
|
assert_equal './test/env_based_config.yml', Sidekiq.options[:config_file]
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets verbose' do
|
|
|
|
refute Sidekiq.options[:verbose]
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets require file' do
|
|
|
|
assert_equal './test/fake_env.rb', Sidekiq.options[:require]
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets environment' do
|
|
|
|
assert_equal 'staging', Sidekiq.options[:environment]
|
|
|
|
end
|
|
|
|
|
2012-02-15 21:13:32 -05:00
|
|
|
it 'sets concurrency' do
|
2013-01-18 23:57:26 -05:00
|
|
|
assert_equal 5, Sidekiq.options[:concurrency]
|
2012-02-15 21:13:32 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets pid file' do
|
2012-02-19 16:02:32 -05:00
|
|
|
assert_equal '/tmp/sidekiq-config-test.pid', Sidekiq.options[:pidfile]
|
2012-02-15 21:13:32 -05:00
|
|
|
end
|
|
|
|
|
2012-12-11 06:32:22 -05:00
|
|
|
it 'sets logfile' do
|
|
|
|
assert_equal '/tmp/sidekiq.log', Sidekiq.options[:logfile]
|
|
|
|
end
|
|
|
|
|
2012-02-15 21:13:32 -05:00
|
|
|
it 'sets queues' do
|
2012-10-19 16:50:59 -04:00
|
|
|
assert_equal 2, Sidekiq.options[:queues].count { |q| q == 'very_often' }
|
2012-04-03 23:19:29 -04:00
|
|
|
assert_equal 1, Sidekiq.options[:queues].count { |q| q == 'seldom' }
|
2012-02-15 21:13:32 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'with config file and flags' do
|
|
|
|
before do
|
|
|
|
# We need an actual file here.
|
|
|
|
@tmp_lib_path = '/tmp/require-me.rb'
|
|
|
|
File.open(@tmp_lib_path, 'w') do |f|
|
|
|
|
f.puts "# do work"
|
|
|
|
end
|
|
|
|
|
|
|
|
@tmp_file = Tempfile.new('sidekiqr')
|
|
|
|
@tmp_path = @tmp_file.path
|
|
|
|
@tmp_file.close!
|
|
|
|
|
|
|
|
@cli.parse(['sidekiq',
|
|
|
|
'-C', './test/config.yml',
|
|
|
|
'-e', 'snoop',
|
|
|
|
'-c', '100',
|
|
|
|
'-r', @tmp_lib_path,
|
|
|
|
'-P', @tmp_path,
|
2012-04-03 23:19:29 -04:00
|
|
|
'-q', 'often,7',
|
|
|
|
'-q', 'seldom,3'])
|
2012-02-15 21:13:32 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
File.unlink @tmp_lib_path if File.exist? @tmp_lib_path
|
|
|
|
File.unlink @tmp_path if File.exist? @tmp_path
|
|
|
|
end
|
|
|
|
|
2012-02-16 12:45:55 -05:00
|
|
|
it 'uses concurrency flag' do
|
2012-02-19 16:02:32 -05:00
|
|
|
assert_equal 100, Sidekiq.options[:concurrency]
|
2012-02-15 21:13:32 -05:00
|
|
|
end
|
2012-02-19 16:02:32 -05:00
|
|
|
|
2012-02-15 21:13:32 -05:00
|
|
|
it 'uses require file flag' do
|
2012-02-19 16:02:32 -05:00
|
|
|
assert_equal @tmp_lib_path, Sidekiq.options[:require]
|
2012-02-15 21:13:32 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'uses environment flag' do
|
2012-02-19 16:02:32 -05:00
|
|
|
assert_equal 'snoop', Sidekiq.options[:environment]
|
2012-02-15 21:13:32 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'uses pidfile flag' do
|
2012-02-19 16:02:32 -05:00
|
|
|
assert_equal @tmp_path, Sidekiq.options[:pidfile]
|
2012-02-15 21:13:32 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets queues' do
|
2012-04-03 23:19:29 -04:00
|
|
|
assert_equal 7, Sidekiq.options[:queues].count { |q| q == 'often' }
|
|
|
|
assert_equal 3, Sidekiq.options[:queues].count { |q| q == 'seldom' }
|
2012-02-15 21:13:32 -05:00
|
|
|
end
|
|
|
|
end
|
2012-07-27 18:18:11 -04:00
|
|
|
|
|
|
|
describe 'Sidekiq::CLI#parse_queues' do
|
2012-11-07 01:54:31 -05:00
|
|
|
describe 'when weight is present' do
|
|
|
|
it 'concatenates queues by factor of weight and sets strict to false' do
|
2013-12-23 10:32:53 -05:00
|
|
|
opts = { strict: true }
|
2014-02-14 09:43:34 -05:00
|
|
|
@cli.__send__ :parse_queues, opts, [['often', 7], ['repeatedly', 3]]
|
|
|
|
@cli.__send__ :parse_queues, opts, [['once']]
|
2013-12-23 10:32:53 -05:00
|
|
|
assert_equal (%w[often] * 7 + %w[repeatedly] * 3 + %w[once]), opts[:queues]
|
2012-11-07 01:54:31 -05:00
|
|
|
assert !opts[:strict]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'when weight is not present' do
|
|
|
|
it 'returns queues and sets strict' do
|
2013-12-23 10:32:53 -05:00
|
|
|
opts = { strict: true }
|
2014-02-14 09:43:34 -05:00
|
|
|
@cli.__send__ :parse_queues, opts, [['once'], ['one_time']]
|
|
|
|
@cli.__send__ :parse_queues, opts, [['einmal']]
|
2013-12-23 10:32:53 -05:00
|
|
|
assert_equal %w[once one_time einmal], opts[:queues]
|
2012-11-07 01:54:31 -05:00
|
|
|
assert opts[:strict]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'Sidekiq::CLI#parse_queue' do
|
2012-07-27 18:18:11 -04:00
|
|
|
describe 'when weight is present' do
|
2013-12-23 10:32:53 -05:00
|
|
|
it 'concatenates queue to opts[:queues] weight number of times and sets strict to false' do
|
|
|
|
opts = { strict: true }
|
2014-02-14 09:43:34 -05:00
|
|
|
@cli.__send__ :parse_queue, opts, 'often', 7
|
2012-07-27 18:18:11 -04:00
|
|
|
assert_equal %w[often] * 7, opts[:queues]
|
2013-12-23 10:32:53 -05:00
|
|
|
assert !opts[:strict]
|
2012-07-27 18:18:11 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'when weight is not present' do
|
2013-12-23 10:32:53 -05:00
|
|
|
it 'concatenates queue to opts[:queues] once and leaves strict true' do
|
|
|
|
opts = { strict: true }
|
2014-02-14 09:43:34 -05:00
|
|
|
@cli.__send__ :parse_queue, opts, 'once', nil
|
2012-07-27 18:18:11 -04:00
|
|
|
assert_equal %w[once], opts[:queues]
|
2013-12-23 10:32:53 -05:00
|
|
|
assert opts[:strict]
|
2012-07-27 18:18:11 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2012-02-12 22:53:34 -05:00
|
|
|
end
|
2012-03-08 23:58:51 -05:00
|
|
|
|
2012-02-12 22:53:34 -05:00
|
|
|
end
|