Skip to content

Commit

Permalink
Test CLI run (write pid, system boot, print banner)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tensho committed Dec 5, 2018
1 parent 20cf862 commit 76e862e
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 27 deletions.
29 changes: 11 additions & 18 deletions lib/sidekiq/cli.rb
Expand Up @@ -24,7 +24,6 @@ class CLI
proc { |me, data| "stopping" if me.stopping? },
]

# Used for CLI testing
attr_accessor :launcher
attr_accessor :environment

Expand All @@ -45,7 +44,7 @@ def run
daemonize if options[:daemon]
write_pid
boot_system
print_banner
print_banner if environment == 'development' && $stdout.tty?

self_read, self_write = IO.pipe
sigs = %w(INT TERM TTIN TSTP)
Expand Down Expand Up @@ -93,6 +92,10 @@ def run
logger.debug { "Client Middleware: #{Sidekiq.client_middleware.map(&:klass).join(', ')}" }
logger.debug { "Server Middleware: #{Sidekiq.server_middleware.map(&:klass).join(', ')}" }

launch(self_read)
end

def launch(self_read)
if !options[:daemon]
logger.info 'Starting processing, hit Ctrl-C to stop'
end
Expand Down Expand Up @@ -178,21 +181,16 @@ def handle_signal(sig)
private

def print_banner
# Print logo and banner for development
if environment == 'development' && $stdout.tty?
puts "\e[#{31}m"
puts Sidekiq::CLI.banner
puts "\e[0m"
end
puts "\e[#{31}m"
puts Sidekiq::CLI.banner
puts "\e[0m"
end

def daemonize
raise ArgumentError, "You really should set a logfile if you're going to daemonize" unless options[:logfile]
files_to_reopen = []
ObjectSpace.each_object(File) do |file|
files_to_reopen << file unless file.closed?
end

files_to_reopen = ObjectSpace.each_object(File).reject { |f| f.closed? }

::Process.daemon(true, true)

files_to_reopen.each do |file|
Expand Down Expand Up @@ -251,8 +249,6 @@ def options
def boot_system
ENV['RACK_ENV'] = ENV['RAILS_ENV'] = environment

raise ArgumentError, "#{options[:require]} does not exist" unless File.exist?(options[:require])

if File.directory?(options[:require])
require 'rails'
if ::Rails::VERSION::MAJOR < 4
Expand All @@ -272,10 +268,7 @@ def boot_system
end
options[:tag] ||= default_tag
else
not_required_message = "#{options[:require]} was not required, you should use an explicit path: " +
"./#{options[:require]} or /path/to/#{options[:require]}"

require(options[:require]) || raise(ArgumentError, not_required_message)
require options[:require]
end
end

Expand Down
15 changes: 15 additions & 0 deletions test/dummy/config/application.rb
@@ -0,0 +1,15 @@
# frozen_string_literal: true

require "rails/all"

module Dummy
class Application < Rails::Application
config.root = File.expand_path("../..", __FILE__)
config.eager_load = false
config.logger = Logger.new('/dev/null')

if Rails::VERSION::MAJOR >= 5
config.active_record.sqlite3.represent_boolean_as_integer = true
end
end
end
11 changes: 11 additions & 0 deletions test/dummy/config/database.yml
@@ -0,0 +1,11 @@
development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000

test:
adapter: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000
3 changes: 3 additions & 0 deletions test/dummy/config/environment.rb
@@ -0,0 +1,3 @@
require_relative 'application'

Rails.application.initialize!
79 changes: 70 additions & 9 deletions test/test_cli.rb
Expand Up @@ -2,16 +2,15 @@

require_relative 'helper'
require 'sidekiq/cli'
require 'tempfile'

class TestCLI < Minitest::Test
describe '#parse' do
before do
@cli = Sidekiq::CLI.new
Sidekiq.options = Sidekiq::DEFAULTS.dup
@logger = Sidekiq.logger
@logdev = StringIO.new
Sidekiq.logger = Logger.new(@logdev)
@cli = Sidekiq::CLI.new
end

after do
Expand Down Expand Up @@ -263,19 +262,81 @@ class TestCLI < Minitest::Test
end
end
end
end

it 'does not require the specified Ruby code' do
@cli.parse(%w[sidekiq -r ./test/fake_env.rb])
describe '#run' do
before do
Sidekiq.options = Sidekiq::DEFAULTS.dup
Sidekiq.options[:require] = './test/fake_env.rb'
@logger = Sidekiq.logger
@logdev = StringIO.new
Sidekiq.logger = Logger.new(@logdev)
@cli = Sidekiq::CLI.new
end

after do
Sidekiq.logger = @logger
end

describe 'pidfile' do
it 'writes process pid to file' do
Sidekiq.options[:pidfile] = '/tmp/sidekiq.pid'
@cli.stub(:launch, nil) do
@cli.run
end

refute($LOADED_FEATURES.any? { |x| x =~ /fake_env/ })
assert_equal Process.pid, File.read('/tmp/sidekiq.pid').chop.to_i
end
end

it 'does not boot rails' do
refute defined?(::Rails::Application)
describe 'require workers' do
describe 'when path is a rails directory' do
before do
Sidekiq.options[:require] = './test/dummy'
@cli.environment = 'test'
end

it 'requires sidekiq railtie and rails application with environment' do
@cli.stub(:launch, nil) do
@cli.run
end

@cli.parse(%w[sidekiq -r ./myapp])
assert defined?(Sidekiq::Rails)
assert defined?(Dummy::Application)
end

it 'tags with the app directory name' do
@cli.stub(:launch, nil) do
@cli.run
end

refute defined?(::Rails::Application)
assert_equal 'dummy', Sidekiq.options[:tag]
end
end

describe 'when path is file' do
it 'requires application' do
@cli.stub(:launch, nil) do
@cli.run
end

assert $LOADED_FEATURES.any? { |x| x =~ /test\/fake_env/ }
end
end
end

describe 'when development environment and stdout tty' do
it 'prints banner' do
@cli.stub(:environment, 'development') do
assert_output(/#{Regexp.escape(Sidekiq::CLI.banner)}/) do
$stdout.stub(:tty?, true) do
@cli.stub(:launch, nil) do
@cli.run
end
end
end
end
end
end
end

Expand Down

0 comments on commit 76e862e

Please sign in to comment.