Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CLI Run Test - Write Pid, System Boot, Print Banner #4039

Merged
merged 5 commits into from Dec 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -2,6 +2,7 @@
.ruby-version
tags
Gemfile.lock
gemfiles/*.lock
*.swp
dump.rdb
.rbx
Expand Down
7 changes: 5 additions & 2 deletions .travis.yml
Expand Up @@ -4,8 +4,11 @@ cache: bundler
services:
- redis-server
before_install:
- gem install bundler
- gem update bundler
- gem update --system
gemfile:
- gemfiles/rails_4.gemfile
- gemfiles/rails_5.gemfile
bundler_args: --without development load_test
rvm:
- 2.2.10
- 2.3.7
Expand Down
9 changes: 9 additions & 0 deletions Appraisals
@@ -0,0 +1,9 @@
appraise "rails-4" do
gem "rails", "~> 4.2"
gem 'activerecord-jdbcsqlite3-adapter', '< 50', platforms: :jruby
end

appraise "rails-5" do
gem "rails", "~> 5.2"
gem 'activerecord-jdbcsqlite3-adapter', '>= 50', platforms: :jruby
end
28 changes: 21 additions & 7 deletions Gemfile
@@ -1,15 +1,29 @@
source 'https://rubygems.org'

gemspec

# load testing
#gem "hiredis"
#gem 'toxiproxy'
gem 'rake'
gem 'redis-namespace'
gem 'rails', '~> 5.2'
gem 'sqlite3', platforms: :ruby
gem 'activerecord-jdbcsqlite3-adapter', platforms: :jruby

group :development do
gem 'appraisal'
end

group :test do
gem 'rails', '>= 5.0.1'
gem 'minitest'
gem 'pry-byebug', platforms: :mri
gem 'rake'
gem 'redis-namespace'
gem 'minitest-focus'
gem 'minitest-reporters'
gem 'simplecov'
end

group :development, :test do
gem 'pry-byebug', platforms: :mri
end

group :load_test do
gem 'hiredis'
gem 'toxiproxy'
end
3 changes: 2 additions & 1 deletion Rakefile
@@ -1,8 +1,9 @@
require 'bundler/gem_tasks'
require 'rake/testtask'

Rake::TestTask.new(:test) do |test|
test.warning = true
test.pattern = 'test/**/test_*.rb'
end

task :default => :test
task default: :test
31 changes: 31 additions & 0 deletions gemfiles/rails_4.gemfile
@@ -0,0 +1,31 @@
# This file was generated by Appraisal

source "https://rubygems.org"

gem "rake"
gem "redis-namespace"
gem "rails", "~> 4.2"
gem "sqlite3", platforms: :ruby
gem "activerecord-jdbcsqlite3-adapter", "< 50", platforms: :jruby

group :development do
gem "appraisal"
end

group :test do
gem "minitest"
gem "minitest-focus"
gem "minitest-reporters"
gem "simplecov"
end

group :development, :test do
gem "pry-byebug", platforms: :mri
end

group :load_test do
gem "hiredis"
gem "toxiproxy"
end

gemspec path: "../"
31 changes: 31 additions & 0 deletions gemfiles/rails_5.gemfile
@@ -0,0 +1,31 @@
# This file was generated by Appraisal

source "https://rubygems.org"

gem "rake"
gem "redis-namespace"
gem "rails", "~> 5.2"
gem "sqlite3", platforms: :ruby
gem "activerecord-jdbcsqlite3-adapter", ">= 50", platforms: :jruby

group :development do
gem "appraisal"
end

group :test do
gem "minitest"
gem "minitest-focus"
gem "minitest-reporters"
gem "simplecov"
end

group :development, :test do
gem "pry-byebug", platforms: :mri
end

group :load_test do
gem "hiredis"
gem "toxiproxy"
end

gemspec path: "../"
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])
mperham marked this conversation as resolved.
Show resolved Hide resolved

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: " +
mperham marked this conversation as resolved.
Show resolved Hide resolved
"./#{options[:require]} or /path/to/#{options[:require]}"

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

Expand Down
3 changes: 2 additions & 1 deletion lib/sidekiq/rails.rb
@@ -1,4 +1,5 @@
# frozen_string_literal: true

module Sidekiq
class Rails < ::Rails::Engine
# We need to setup this up before any application configuration which might
Expand Down Expand Up @@ -54,4 +55,4 @@ def inspect
$stderr.puts("**************************************************")
$stderr.puts("⛔️ WARNING: Sidekiq server is no longer supported by Rails 3.2 - please ensure your server/workers are updated")
$stderr.puts("**************************************************")
end
end
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!
37 changes: 8 additions & 29 deletions test/helper.rb
@@ -1,7 +1,12 @@
# frozen_string_literal: true

require "bundler/setup"
Bundler.require
require 'bundler/setup'
Bundler.require(:default, :test)

require 'minitest/reporters'
require 'minitest/autorun'

MiniTest::Reporters.use! Minitest::Reporters::DefaultReporter.new

$TESTING = true
# disable minitest/parallel threads
Expand All @@ -15,36 +20,10 @@
end
end

ENV['RACK_ENV'] = ENV['RAILS_ENV'] = 'test'

trap 'TSTP' do
mperham marked this conversation as resolved.
Show resolved Hide resolved
threads = Thread.list

puts
puts "=" * 80
puts "Received TSTP signal; printing all #{threads.count} thread backtraces."

threads.each do |thr|
description = thr == Thread.main ? "Main thread" : thr.inspect
puts
puts "#{description} backtrace: "
puts thr.backtrace.join("\n")
end

puts "=" * 80
end

require 'minitest/autorun'
ENV['REDIS_URL'] ||= 'redis://localhost/15'

Sidekiq.logger.level = Logger::ERROR

REDIS_URL = ENV['REDIS_URL'] || 'redis://localhost/15'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't want the test suite to use the default Redis localhost:6379/0

Copy link
Contributor Author

@Tensho Tensho Dec 6, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I may return back

ENV['REDIS_URL'] ||= 'redis://localhost/15'

or without customisation oportunity

ENV['REDIS_URL'] = 'redis://localhost/15'

That should be enough I guess. BTW, why 15? Is it kind of virtual host in RabbitMQ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, yeah, I want people's default redis database to be untouched by Sidekiq's test suite. We'll need to fix those tests (or make them read only)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Already done this, Mike ^_^

REDIS = Sidekiq::RedisConnection.create(:url => REDIS_URL)

Sidekiq.configure_client do |config|
config.redis = { :url => REDIS_URL }
end

def capture_logging(lvl=Logger::INFO)
old = Sidekiq.logger
begin
Expand Down