Skip to content

Commit

Permalink
Merge pull request #20 from paladinsoftware/sidekiq-testing-support
Browse files Browse the repository at this point in the history
Support sidekiq-testing
  • Loading branch information
kukicola authored Mar 13, 2023
2 parents 0aeee37 + ae64257 commit 3082a9e
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 12 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## Unreleased
- support Sidekiq::Testing

## [2.0.1] - 2023-03-04
- don't remove debounce key in redis to avoid invalid debouncing

Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,15 @@ end

In order to test the behavior of `sidekiq-debouncer` it is necessary to disable testing mode. It is the limitation of internal implementation.

Sidekiq::Debouncer will not debounce the jobs in testing mode, instead they'll be executed like normal jobs (pushed to fake queue or executed inline depending on settings).
Server middleware needs to be added to `Sidekiq::Testing` chain:

```ruby
Sidekiq::Testing.server_middleware do |chain|
chain.add Sidekiq::Debouncer::Middleware::Server
end
```

## License

MIT Licensed. See LICENSE.txt for details.
Expand Down
12 changes: 9 additions & 3 deletions lib/sidekiq/debouncer/middleware/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ def call(worker_class, job, _queue, _redis_pool)
return job if !klass.get_sidekiq_options["debounce"] || job["debounce_key"]

debounce(klass, job)

# prevent normal sidekiq flow
false
end

private
Expand All @@ -46,9 +43,14 @@ def debounce(klass, job)
job["args"] = [job["args"]]
job.delete("debounce")

return job if testing?

redis do |connection|
redis_debounce(connection, keys: ["schedule", key], argv: [Sidekiq.dump_json(job), time, @debounce_key_ttl])
end

# prevent normal sidekiq flow
false
end

def debounce_key(klass, job, options)
Expand Down Expand Up @@ -79,6 +81,10 @@ def redis_debounce(connection, keys:, argv:)
retry
end
end

def testing?
defined?(Sidekiq::Testing) && Sidekiq::Testing.enabled?
end
end
end
end
Expand Down
51 changes: 42 additions & 9 deletions spec/sidekiq/middleware/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@
expect(queue.size).to eq(0)
end

it "doesnt land in queue" do
TestWorker.perform_async("A", "job 1")

expect(queue.size).to eq(0)
end

it "executes it after 5 minutes for symbol debounce" do
expect(TestWorkerWithSymbolAsDebounce).to receive(:debounce_method).with(["A", "job 1"]).once.and_call_original
TestWorkerWithSymbolAsDebounce.perform_async("A", "job 1")
Expand Down Expand Up @@ -169,15 +175,6 @@
end
end

context "normal job" do
it "ignores debounce logic" do
NormalWorker.perform_async("abc")

expect(schedule_set.size).to eq(0)
expect(queue.first["debounce_key"]).to be_nil
end
end

context "debounce method" do
it "works like perform_async" do
TestWorker.debounce("A", "job 1")
Expand Down Expand Up @@ -206,5 +203,41 @@
expect(queue.size).to eq(0)
end
end

context "sidekiq testing fake mode" do
it "uses standard sidekiq flow" do
Sidekiq::Testing.fake! do
TestWorker.debounce("A", "job 1")

expect(schedule_set.size).to eq(0)
expect(TestWorker.jobs.size).to eq(1)

expect_any_instance_of(TestWorker).to receive(:perform).with([["A", "job 1"]])
TestWorker.drain
end
end
end

context "sidekiq testing inline mode" do
it "uses standard sidekiq flow" do
Sidekiq::Testing.inline! do
expect_any_instance_of(TestWorker).to receive(:perform).with([["A", "job 1"]])

TestWorker.debounce("A", "job 1")

expect(schedule_set.size).to eq(0)
expect(TestWorker.jobs.size).to eq(0)
end
end
end
end

context "normal job" do
it "ignores debounce logic" do
NormalWorker.perform_async("abc")

expect(schedule_set.size).to eq(0)
expect(queue.first["debounce_key"]).to be_nil
end
end
end
6 changes: 6 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
require "sidekiq/scheduled"
require "sidekiq/processor"
require "sidekiq/api"
require "sidekiq/testing"

SimpleCov.start do
add_filter "/spec/"
Expand All @@ -23,6 +24,11 @@
Sidekiq.logger.level = Logger::UNKNOWN
end

Sidekiq::Testing.disable!
Sidekiq::Testing.server_middleware do |chain|
chain.add Sidekiq::Debouncer::Middleware::Server
end

RSpec.configure do |config|
config.order = "random"
config.color = true
Expand Down

0 comments on commit 3082a9e

Please sign in to comment.