Skip to content

Commit

Permalink
Merge pull request #192 from wspurgin/ws/support-sidekiq-7
Browse files Browse the repository at this point in the history
Support Sidekiq 7
  • Loading branch information
wspurgin authored Jul 23, 2023
2 parents 66b4ebc + be21d8a commit e62a9cd
Show file tree
Hide file tree
Showing 9 changed files with 315 additions and 258 deletions.
1 change: 1 addition & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ jobs:
- 'gemfiles/sidekiq6_4_rails7.gemfile'
- 'gemfiles/sidekiq6_5_rails6.gemfile'
- 'gemfiles/sidekiq6_5_rails7.gemfile'
- 'gemfiles/sidekiq7_rails7.gemfile'
env:
BUNDLE_GEMFILE: ${{ github.workspace }}/${{ matrix.gemfile }}
steps:
Expand Down
12 changes: 12 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
The MIT License (MIT)

Copyright (c) 2023 Will Spurgin

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Elements of this software and associated documentation files are covered by the following license:

The MIT License (MIT)

Copyright (c) 2014, 2015 Phil Ostler

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
Expand Down
122 changes: 64 additions & 58 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,33 +36,58 @@ end
```

## Matchers
* [be_delayed](#be_delayed)
* [have_enqueued_sidekiq_job](#have_enqueued_sidekiq_job)
* [be_processed_in](#be_processed_in)
* [be_retryable](#be_retryable)
* [be_unique](#be_unique)
* [have_enqueued_sidekiq_job](#have_enqueued_sidekiq_job)
* [be_delayed (_deprecated_)](#be_delayed)

### have_enqueued_sidekiq_job
*Describes that there should be an enqueued job with the specified arguments*

**Note:** When using rspec-rails >= 3.4, use `have_enqueued_sidekiq_job` instead to
prevent a name clash with rspec-rails' ActiveJob matcher.

### be_delayed
*Describes a method that should be invoked asynchronously (See [Sidekiq Delayed Extensions][sidekiq_wiki_delayed_extensions])*
```ruby
Object.delay.is_nil? # delay
expect(Object.method :is_nil?).to be_delayed
Object.delay.is_a? Object # delay with argument
expect(Object.method :is_a?).to be_delayed(Object)
AwesomeJob.perform_async 'Awesome', true
# test with...
expect(AwesomeJob).to have_enqueued_sidekiq_job('Awesome', true)

Object.delay_for(1.hour).is_nil? # delay for
expect(Object.method :is_nil?).to be_delayed.for 1.hour
Object.delay_for(1.hour).is_a? Object # delay for with argument
expect(Object.method :is_a?).to be_delayed(Object).for 1.hour
# Code written with older versions of the gem may use the deprecated
# have_enqueued_job matcher.
expect(AwesomeJob).to have_enqueued_job('Awesome', true)
```

Object.delay_until(1.hour.from_now).is_nil? # delay until
expect(Object.method :is_nil?).to be_delayed.until 1.hour.from_now
Object.delay_until(1.hour.from_now).is_a? Object # delay until with argument
expect(Object.method :is_a?).to be_delayed(Object).until 1.hour.from_now
#### Testing scheduled jobs

#Rails Mailer
MyMailer.delay.some_mail
expect(MyMailer.instance_method :some_mail).to be_delayed
*Use chainable matchers `#at` and `#in`*

```ruby
time = 5.minutes.from_now
Awesomejob.perform_at time, 'Awesome', true
# test with...
expect(AwesomeJob).to have_enqueued_sidekiq_job('Awesome', true).at(time)
```
```ruby
Awesomejob.perform_in 5.minutes, 'Awesome', true
# test with...
expect(AwesomeJob).to have_enqueued_sidekiq_job('Awesome', true).in(5.minutes)
```


#### Testing ActiveMailer jobs

```ruby
user = User.first
AwesomeActionMailer.invite(user, true).deliver_later

expect(Sidekiq::Worker).to have_enqueued_sidekiq_job(
"AwesomeActionMailer",
"invite",
"deliver_now",
user,
true
)
```

### be_processed_in
Expand Down Expand Up @@ -124,51 +149,32 @@ it { is_expected.to be_expired_in 1.hour }
it { is_expected.to_not be_expired_in 2.hours }
```

### have_enqueued_sidekiq_job
*Describes that there should be an enqueued job with the specified arguments*

**Note:** When using rspec-rails >= 3.4, use `have_enqueued_sidekiq_job` instead to
prevent a name clash with rspec-rails' ActiveJob matcher.

```ruby
AwesomeJob.perform_async 'Awesome', true
# test with...
expect(AwesomeJob).to have_enqueued_sidekiq_job('Awesome', true)

# Code written with older versions of the gem may use the deprecated
# have_enqueued_job matcher.
expect(AwesomeJob).to have_enqueued_job('Awesome', true)
```

#### Testing scheduled jobs
### be_delayed

*Use chainable matchers `#at` and `#in`*
**This matcher is deprecated**. Use of it with Sidekiq 7+ will raise an error.
Sidekiq 7 [dropped Delayed
Extensions](https://github.com/sidekiq/sidekiq/issues/5076).

*Describes a method that should be invoked asynchronously (See [Sidekiq Delayed Extensions][sidekiq_wiki_delayed_extensions])*
```ruby
time = 5.minutes.from_now
Awesomejob.perform_at time, 'Awesome', true
# test with...
expect(AwesomeJob).to have_enqueued_sidekiq_job('Awesome', true).at(time)
```
```ruby
Awesomejob.perform_in 5.minutes, 'Awesome', true
# test with...
expect(AwesomeJob).to have_enqueued_sidekiq_job('Awesome', true).in(5.minutes)
```
Object.delay.is_nil? # delay
expect(Object.method :is_nil?).to be_delayed
Object.delay.is_a? Object # delay with argument
expect(Object.method :is_a?).to be_delayed(Object)

#### Testing ActiveMailer jobs
Object.delay_for(1.hour).is_nil? # delay for
expect(Object.method :is_nil?).to be_delayed.for 1.hour
Object.delay_for(1.hour).is_a? Object # delay for with argument
expect(Object.method :is_a?).to be_delayed(Object).for 1.hour

```ruby
user = User.first
AwesomeActionMailer.invite(user, true).deliver_later
Object.delay_until(1.hour.from_now).is_nil? # delay until
expect(Object.method :is_nil?).to be_delayed.until 1.hour.from_now
Object.delay_until(1.hour.from_now).is_a? Object # delay until with argument
expect(Object.method :is_a?).to be_delayed(Object).until 1.hour.from_now

expect(Sidekiq::Worker).to have_enqueued_sidekiq_job(
"AwesomeActionMailer",
"invite",
"deliver_now",
user,
true
)
#Rails Mailer
MyMailer.delay.some_mail
expect(MyMailer.instance_method :some_mail).to be_delayed
```

## Example matcher usage
Expand Down
10 changes: 10 additions & 0 deletions gemfiles/sidekiq7_rails7.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
source 'https://rubygems.org'

gem 'sidekiq', '~> 7'
gem 'activejob', '~> 7'
gem 'actionmailer', '~> 7'
gem 'activerecord', '~> 7'
gem "activemodel", "~> 7"
gem "railties", "~> 7"

gemspec :path => '../'
15 changes: 13 additions & 2 deletions lib/rspec/sidekiq/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
require "rubygems"

module RSpec
module Sidekiq
class Configuration
attr_accessor :clear_all_enqueued_jobs, :enable_terminal_colours, :warn_when_jobs_not_processed_by_sidekiq
attr_accessor :clear_all_enqueued_jobs,
:enable_terminal_colours,
:warn_when_jobs_not_processed_by_sidekiq

def initialize
@clear_all_enqueued_jobs = true
# Display settings defaults
@enable_terminal_colours = true

# Functional settings defaults
@clear_all_enqueued_jobs = true
@warn_when_jobs_not_processed_by_sidekiq = true
end

def sidekiq_gte_7?
Gem::Version.new(::Sidekiq::VERSION) >= Gem::Version.new("7.0.0")
end
end
end
end
4 changes: 4 additions & 0 deletions lib/rspec/sidekiq/matchers/be_delayed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ def be_delayed(*expected_arguments)

class BeDelayed
def initialize(*expected_arguments)
raise <<~MSG if RSpec::Sidekiq.configuration.sidekiq_gte_7?
Use of the be_delayed matcher with Sidekiq 7+ is not possible. Try refactoring to a Sidekiq Job with `perform_at` or `perform_in` and the `have_enqueued_sidekiq_job` matcher
MSG

@expected_arguments = expected_arguments
end

Expand Down
62 changes: 30 additions & 32 deletions rspec-sidekiq.gemspec
Original file line number Diff line number Diff line change
@@ -1,40 +1,38 @@
require File.expand_path('../lib/rspec/sidekiq/version', __FILE__)
require File.expand_path("../lib/rspec/sidekiq/version", __FILE__)

Gem::Specification.new do |s|
s.name = 'rspec-sidekiq'
s.name = "rspec-sidekiq"
s.version = RSpec::Sidekiq::VERSION
s.platform = Gem::Platform::RUBY
s.author = 'Phil Ostler'
s.email = '[email protected]'
s.homepage = 'http://github.com/philostler/rspec-sidekiq'
s.summary = 'RSpec for Sidekiq'
s.description = 'Simple testing of Sidekiq jobs via a collection of matchers and helpers'
s.license = 'MIT'
s.authors = ["Aidan Coyle", "Phil Ostler", "Will Spurgin"]
s.homepage = "http://github.com/wspurgin/rspec-sidekiq"
s.summary = "RSpec for Sidekiq"
s.description = "Simple testing of Sidekiq jobs via a collection of matchers and helpers"
s.license = "MIT"

s.add_dependency 'rspec-core', '~> 3.0', '>= 3.0.0'
s.add_dependency 'sidekiq', '>= 2.4.0', '< 7'
s.add_dependency "rspec-core", "~> 3.0", ">= 3.0.0"
s.add_dependency "sidekiq", ">= 5", "< 8"

s.add_development_dependency 'pry'
s.add_development_dependency 'pry-doc'
s.add_development_dependency 'pry-nav'
s.add_development_dependency 'rspec'
s.add_development_dependency 'coveralls'
s.add_development_dependency 'fuubar'
s.add_development_dependency 'activejob'
s.add_development_dependency 'actionmailer'
s.add_development_dependency 'activerecord'
s.add_development_dependency 'activemodel'
s.add_development_dependency 'activesupport'
s.add_development_dependency "pry"
s.add_development_dependency "pry-doc"
s.add_development_dependency "pry-nav"
s.add_development_dependency "rspec"
s.add_development_dependency "coveralls"
s.add_development_dependency "fuubar"
s.add_development_dependency "activejob"
s.add_development_dependency "actionmailer"
s.add_development_dependency "activerecord"
s.add_development_dependency "activemodel"
s.add_development_dependency "activesupport"


s.files = Dir['.gitattributes'] +
Dir['.gitignore'] +
Dir['.rspec'] +
Dir['CHANGES.md'] +
Dir['Gemfile'] +
Dir['LICENSE'] +
Dir['README.md'] +
Dir['rspec-sidekiq.gemspec'] +
Dir['**/*.rb']
s.require_paths = ['lib']
s.files = Dir[".gitattributes"] +
Dir[".gitignore"] +
Dir[".rspec"] +
Dir["CHANGES.md"] +
Dir["Gemfile"] +
Dir["LICENSE"] +
Dir["README.md"] +
Dir["rspec-sidekiq.gemspec"] +
Dir["**/*.rb"]
s.require_paths = ["lib"]
end
Loading

0 comments on commit e62a9cd

Please sign in to comment.