karafka / karafka-testing

Library which provides helpers for easier Karafka consumers tests
https://karafka.io
MIT License
22 stars 11 forks source link

have_enqueued_job rspec matcher does not work with :karafka ActiveJob queue_adapter #140

Open wahlg opened 1 year ago

wahlg commented 1 year ago

If you configure a Rails application to use the karafka queue adapter as described in https://karafka.io/docs/Active-Job/, then when writing rspec tests that use the have_enqueued_job matcher, they will fail reporting no jobs were enqueued


Expected behavior

When configuring rails to use the karafka ActiveJob adapter with:

config.active_job.queue_adapter = :karafka

Then if I write ruby code to publish a job:

class SomeJob < ApplicationJob
  def perform
    puts "performed"
  end
end

SomeJob.perform_later

I expect an rspec test like this to pass:

expect do 
  SomeJob.perform_later
end.to have_enqueued_job(SomeJob).once

Actual behavior

The rspec test fails with

expected to enqueue exactly 1 jobs, but enqueued 0

I also observed this happens regardless of whether I use the produce_sync option or not

karafka_options(
  dispatch_method: :produce_sync
)

Steps to reproduce the problem

Create a rails 7 application, and configure it to use the :karafka queue adapter for ActiveJob as per above

Set up the karafka gem

Write a test that enqueues a job

See that it fails to report enqueueing the job

Your setup details

I am using Rails 7.0.4.3 and rspec 3.12.0

Karafka info is below:

$ [bundle exec] karafka info
2023-04-21 09:30:25.307444 I [65649:57120] Rails -- Karafka version: 2.0.38
Ruby version: ruby 3.2.1 (2023-02-08 revision 31819e82c8) +YJIT [arm64-darwin22]
Rdkafka version: 0.12.1
Consumer groups count: 4
Subscription groups count: 4
Workers count: 5
...
mensfeld commented 1 year ago

this API needs to be expanded in karafka-testing:

https://github.com/rspec/rspec-rails/blob/main/lib/rspec/rails/matchers/active_job.rb#L233

moving there

mensfeld commented 1 year ago

sorry I forgot about this one. Checking now :pray:

mensfeld commented 1 year ago

@wahlg when I try to do what you did I am getting:

To use ActiveJob matchers set `ActiveJob::Base.queue_adapter = :test`

any chance you could use this example repo to create a repro? https://github.com/karafka/example-apps/tree/master/v2.1-rails