Skip to content

Commit

Permalink
Support Ruby 3.0 and Sidekiq 6.2+ (ixti#102)
Browse files Browse the repository at this point in the history
* Support Ruby 3.0

* Bump Ruby version for RuboCop

* Apply patch from ixti#97

* rubocop --auto-correct

* Fix test failure on `spec/sidekiq/throttled/strategy_collection_spec.rb`

* Add session middleware to `Sidekiq::Web`

See https://github.com/mperham/sidekiq/blob/v6.2.0/Changes.md

* Disable `fail-fast` for the test matrix on CI

* Fix RuboCop issues

* Enable `bundle-cache` of `ruby/setup-ruby`

* Disable coveralls temporarily

* Check Sidekiq version

* Set CSRF token for tests

* Fix RuboCop issues

* Refactor

* Add tests for Sidekiq 6.2 and 6.3
  • Loading branch information
ybiquitous authored Dec 14, 2021
1 parent 74dbf05 commit f27b067
Show file tree
Hide file tree
Showing 18 changed files with 134 additions and 54 deletions.
15 changes: 6 additions & 9 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ jobs:
name: "rspec (ruby:${{ matrix.ruby }} sidekiq:${{ matrix.sidekiq }})"

strategy:
fail-fast: false
matrix:
ruby: [ "2.6", "2.7" ]
sidekiq: [ "5.0", "5.1", "5.2", "6.0", "6.1" ]
ruby: [ "2.6", "2.7", "3.0" ]
sidekiq: [ "5.0", "5.1", "5.2", "6.0", "6.1", "6.2", "6.3" ]

runs-on: ubuntu-latest

Expand All @@ -32,9 +33,7 @@ jobs:
- uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}

- name: bundle install
run: bundle install --without development --jobs 4 --retry 3
bundler-cache: true

- run: bundle exec rspec

Expand All @@ -46,9 +45,7 @@ jobs:

- uses: ruby/setup-ruby@v1
with:
ruby-version: "2.4"

- name: bundle install
run: bundle install --without development --jobs 4 --retry 3
ruby-version: "2.6"
bundler-cache: true

- run: bundle exec rubocop
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ AllCops:
- gemfiles/**/*
- vendor/**/*
NewCops: enable
TargetRubyVersion: 2.4
TargetRubyVersion: 2.6
8 changes: 8 additions & 0 deletions Appraisals
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,11 @@ end
appraise "sidekiq-6.1" do
gem "sidekiq", "~> 6.1.0"
end

appraise "sidekiq-6.2" do
gem "sidekiq", "~> 6.2.0"
end

appraise "sidekiq-6.3" do
gem "sidekiq", "~> 6.3.0"
end
6 changes: 3 additions & 3 deletions gemfiles/sidekiq_5.0.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ source "https://rubygems.org"
gem "appraisal"
gem "rake"
gem "rspec"
gem "rubocop", "~> 0.82.0", require: false
gem "rubocop-performance", "~>1.5.2", require: false
gem "rubocop-rspec", "~> 1.39.0", require: false
gem "rubocop", "~> 0.90.0", require: false
gem "rubocop-performance", "~> 1.8.0", require: false
gem "rubocop-rspec", "~> 1.43.2", require: false
gem "sidekiq", "~> 5.0.0"

group :development do
Expand Down
6 changes: 3 additions & 3 deletions gemfiles/sidekiq_5.1.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ source "https://rubygems.org"
gem "appraisal"
gem "rake"
gem "rspec"
gem "rubocop", "~> 0.82.0", require: false
gem "rubocop-performance", "~>1.5.2", require: false
gem "rubocop-rspec", "~> 1.39.0", require: false
gem "rubocop", "~> 0.90.0", require: false
gem "rubocop-performance", "~> 1.8.0", require: false
gem "rubocop-rspec", "~> 1.43.2", require: false
gem "sidekiq", "~> 5.1.0"

group :development do
Expand Down
6 changes: 3 additions & 3 deletions gemfiles/sidekiq_5.2.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ source "https://rubygems.org"
gem "appraisal"
gem "rake"
gem "rspec"
gem "rubocop", "~> 0.82.0", require: false
gem "rubocop-performance", "~>1.5.2", require: false
gem "rubocop-rspec", "~> 1.39.0", require: false
gem "rubocop", "~> 0.90.0", require: false
gem "rubocop-performance", "~> 1.8.0", require: false
gem "rubocop-rspec", "~> 1.43.2", require: false
gem "sidekiq", "~> 5.2.0"

group :development do
Expand Down
6 changes: 3 additions & 3 deletions gemfiles/sidekiq_6.0.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ source "https://rubygems.org"
gem "appraisal"
gem "rake"
gem "rspec"
gem "rubocop", "~> 0.82.0", require: false
gem "rubocop-performance", "~>1.5.2", require: false
gem "rubocop-rspec", "~> 1.39.0", require: false
gem "rubocop", "~> 0.90.0", require: false
gem "rubocop-performance", "~> 1.8.0", require: false
gem "rubocop-rspec", "~> 1.43.2", require: false
gem "sidekiq", "~> 6.0.0"

group :development do
Expand Down
6 changes: 3 additions & 3 deletions gemfiles/sidekiq_6.1.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ source "https://rubygems.org"
gem "appraisal"
gem "rake"
gem "rspec"
gem "rubocop", "~> 0.82.0", require: false
gem "rubocop-performance", "~>1.5.2", require: false
gem "rubocop-rspec", "~> 1.39.0", require: false
gem "rubocop", "~> 0.90.0", require: false
gem "rubocop-performance", "~> 1.8.0", require: false
gem "rubocop-rspec", "~> 1.43.2", require: false
gem "sidekiq", "~> 6.1.0"

group :development do
Expand Down
31 changes: 31 additions & 0 deletions gemfiles/sidekiq_6.2.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# This file was generated by Appraisal

source "https://rubygems.org"

gem "appraisal"
gem "rake"
gem "rspec"
gem "rubocop", "~> 0.90.0", require: false
gem "rubocop-performance", "~> 1.8.0", require: false
gem "rubocop-rspec", "~> 1.43.2", require: false
gem "sidekiq", "~> 6.2.0"

group :development do
gem "byebug"
gem "guard", require: false
gem "guard-rspec", require: false
gem "guard-rubocop", require: false
end

group :test do
gem "apparition"
gem "capybara"
gem "coveralls", require: false
gem "puma"
gem "rack-test"
gem "simplecov"
gem "sinatra"
gem "timecop"
end

gemspec path: "../"
31 changes: 31 additions & 0 deletions gemfiles/sidekiq_6.3.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# This file was generated by Appraisal

source "https://rubygems.org"

gem "appraisal"
gem "rake"
gem "rspec"
gem "rubocop", "~> 0.90.0", require: false
gem "rubocop-performance", "~> 1.8.0", require: false
gem "rubocop-rspec", "~> 1.43.2", require: false
gem "sidekiq", "~> 6.3.0"

group :development do
gem "byebug"
gem "guard", require: false
gem "guard-rspec", require: false
gem "guard-rubocop", require: false
end

group :test do
gem "apparition"
gem "capybara"
gem "coveralls", require: false
gem "puma"
gem "rack-test"
gem "simplecov"
gem "sinatra"
gem "timecop"
end

gemspec path: "../"
10 changes: 4 additions & 6 deletions lib/sidekiq/throttled/communicator/callbacks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,13 @@ def on(event, &handler)
# @param [#to_s] event
# @param [Object] payload
# @return [void]
def run(event, payload = nil) # rubocop:disable Metrics/MethodLength
def run(event, payload = nil)
@mutex.synchronize do
fiber = Fiber.new do
@handlers[event.to_s].each do |callback|
begin
callback.call(payload)
rescue => e
handle_exception(e, :context => "sidekiq:throttled")
end
callback.call(payload)
rescue => e
handle_exception(e, :context => "sidekiq:throttled")
end
end

Expand Down
5 changes: 2 additions & 3 deletions lib/sidekiq/throttled/strategy_collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,9 @@ def reset!
def make_strategy(strategy, name, key_suffix, options)
return unless options

strategy.new("throttled:#{name}", {
strategy.new("throttled:#{name}",
:key_suffix => key_suffix,
**options
})
**options)
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions sidekiq-throttled.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ Gem::Specification.new do |spec|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]

spec.required_ruby_version = "~> 2.6"
spec.required_ruby_version = ">= 2.6"

spec.add_runtime_dependency "concurrent-ruby"
spec.add_runtime_dependency "redis-prescription"
spec.add_runtime_dependency "sidekiq"

spec.add_development_dependency "bundler", "~> 2.0"
spec.add_development_dependency "bundler", ">= 2.0"
end
20 changes: 9 additions & 11 deletions spec/sidekiq/throttled/communicator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,18 @@
let(:callbacks) { described_class::Callbacks.new }

around do |example|
begin
old_callbacks = communicator.instance_variable_get(:@callbacks)
old_listener = communicator.instance_variable_get(:@listener)
old_callbacks = communicator.instance_variable_get(:@callbacks)
old_listener = communicator.instance_variable_get(:@listener)

communicator.instance_variable_set(:@callbacks, callbacks)
communicator.instance_variable_set(:@listener, nil)
communicator.instance_variable_set(:@callbacks, callbacks)
communicator.instance_variable_set(:@listener, nil)

example.run
ensure
communicator.stop_listener
example.run
ensure
communicator.stop_listener

communicator.instance_variable_set(:@callbacks, old_callbacks)
communicator.instance_variable_set(:@listener, old_listener)
end
communicator.instance_variable_set(:@callbacks, old_callbacks)
communicator.instance_variable_set(:@listener, old_listener)
end

def run_callbacks(name, *args)
Expand Down
2 changes: 1 addition & 1 deletion spec/sidekiq/throttled/strategy_collection_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

RSpec.describe Sidekiq::Throttled::StrategyCollection do
let(:collection) { described_class.new(strategies, options) }
let(:collection) { described_class.new(strategies, **options) }

let(:options) do
{
Expand Down
12 changes: 9 additions & 3 deletions spec/sidekiq/throttled/web/queues_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,25 @@ def app
end

describe "POST /enhanced-queues/:queue" do
let(:csrf_token) { SecureRandom.base64(32) }

before do
env "rack.session", :csrf => csrf_token
end

it "allows pausing the queue" do
expect(pauser).to receive(:pause!).with("xxx")
post "/enhanced-queues/xxx", :action => "pause"
post "/enhanced-queues/xxx", :action => "pause", :authenticity_token => csrf_token
end

it "allows resuming the queue" do
expect(pauser).to receive(:resume!).with("xxx")
post "/enhanced-queues/xxx", :action => "resume"
post "/enhanced-queues/xxx", :action => "resume", :authenticity_token => csrf_token
end

it "allows deleting the queue" do
expect(::Sidekiq::Queue.new("xxx").size).to be >= 1
post "/enhanced-queues/xxx", :action => "delete"
post "/enhanced-queues/xxx", :action => "delete", :authenticity_token => csrf_token
expect(::Sidekiq::Queue.new("xxx").size).to eq 0
end
end
Expand Down
12 changes: 9 additions & 3 deletions spec/sidekiq/throttled/web/throttled_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,30 @@ def app
end

describe "POST /throttled/:id/reset" do
let(:csrf_token) { SecureRandom.base64(32) }

before do
env "rack.session", :csrf => csrf_token
end

context "when id is unknown" do
it "does not fail" do
post "/throttled/abc/reset"
post "/throttled/abc/reset", :authenticity_token => csrf_token
expect(last_response.status).to eq 302
end
end

context "when id is known" do
it "does not fail" do
post "/throttled/foo/reset"
post "/throttled/foo/reset", :authenticity_token => csrf_token
expect(last_response.status).to eq 302
end

it "calls #reset! on matchin strategy" do
strategy = Sidekiq::Throttled::Registry.get "foo"
expect(strategy).to receive(:reset!)

post "/throttled/foo/reset"
post "/throttled/foo/reset", :authenticity_token => csrf_token
end
end
end
Expand Down
6 changes: 6 additions & 0 deletions spec/sidekiq_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ def jid
Sidekiq.configure_server(&configure_redis)
Sidekiq.configure_client(&configure_redis)

# See https://github.com/mperham/sidekiq/blob/v6.2.0/Changes.md
if Gem::Version.new(Sidekiq::VERSION) >= Gem::Version.new("6.2.0")
require "sidekiq/web"
Sidekiq::Web.use Rack::Session::Cookie, :secret => SecureRandom.hex(32), :same_site => true, :max_age => 86_400
end

RSpec.configure do |config|
config.include JidGenerator
config.extend JidGenerator
Expand Down

0 comments on commit f27b067

Please sign in to comment.