diff --git a/test/test_cli.rb b/test/test_cli.rb index e70b538f..8e70a60f 100644 --- a/test/test_cli.rb +++ b/test/test_cli.rb @@ -309,30 +309,82 @@ class TestCli < Sidekiq::Test end describe 'misc' do + before do + @cli = Sidekiq::CLI.new + end + it 'handles interrupts' do - cli = Sidekiq::CLI.new assert_raises Interrupt do - cli.handle_signal('INT') + @cli.handle_signal('INT') end assert_raises Interrupt do - cli.handle_signal('TERM') - end - - cli.stub(:launcher , OpenStruct.new) do - cli.handle_signal('USR1') - end - - options_with_log = Sidekiq.options.merge(:logfile => 'tmp/test.log') - Sidekiq.stub(:options, options_with_log) do - cli.handle_signal('USR2') - end - cli.handle_signal('TTIN') - - Thread.stub(:list, [OpenStruct.new]) do - cli.handle_signal('TTIN') + @cli.handle_signal('TERM') end end + describe 'handles USR1 and USR2' do + it 'shuts down the worker' do + count = 0 + Sidekiq.options[:lifecycle_events][:quiet] = [proc { + count += 1 + }] + @cli.launcher = Sidekiq::Launcher.new(Sidekiq.options) + @cli.handle_signal('USR1') + + assert_equal 1, count + end + + it 'reopens logs' do + mock = MiniTest::Mock.new + # reopen_logs returns number of files reopened so mock that + mock.expect(:call, 1) + + Sidekiq::Logging.stub(:reopen_logs, mock) do + @cli.handle_signal('USR2') + end + mock.verify + + #@cli.handle_signal('USR2') + #klass.verify + end + end + + describe 'handles TTIN' do + before do + @tmp_log_path = '/tmp/sidekiq.log' + @cli.parse(['sidekiq', '-L', @tmp_log_path, '-r', './test/fake_env.rb']) + @mock_thread = MiniTest::Mock.new + @mock_thread.expect(:[], 'interrupt_test', ['label']) + end + + after do + File.unlink @tmp_log_path if File.exists? @tmp_log_path + end + + describe 'with backtrace' do + it 'logs backtrace' do + 2.times { @mock_thread.expect(:backtrace, ['something went wrong']) } + + Thread.stub(:list, [@mock_thread]) do + @cli.handle_signal('TTIN') + assert_match(/something went wrong/, File.read(@tmp_log_path), "didn't include the log message") + end + end + end + + describe 'without backtrace' do + it 'logs no backtrace available' do + @mock_thread.expect(:backtrace, nil) + + Thread.stub(:list, [@mock_thread]) do + @cli.handle_signal('TTIN') + assert_match(/no backtrace available/, File.read(@tmp_log_path), "didn't include the log message") + end + end + end + end + + it 'can fire events' do count = 0 Sidekiq.options[:lifecycle_events][:startup] = [proc {