Skip to content

Provides a test backend (aka test mode) #117

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

Merged
merged 3 commits into from
Oct 3, 2023
Merged

Provides a test backend (aka test mode) #117

merged 3 commits into from
Oct 3, 2023

Conversation

robacarp
Copy link
Collaborator

@robacarp robacarp commented Oct 2, 2023

fixes #43

Two approaches were suggested in the issue:

  • Run jobs synchronously.
  • Catalog jobs that were enqueued, and allow the history to be checked.

The synchronous job pattern is used elsewhere and provides convenience for integration testing. A developer can then test "a user clicks this button and an email is sent to them". However it also adds code to the worker and runner -- something I try to avoid if at all possible to keep mosquito as fast as possible.

I've chosen to implement a testing backend which can be activated in the testing setup code for a project and all enqueues will simply get stored for later inspection / assertion. This avoids adding test_mode=true? checks to worker code. For situations where you need to test the affects of a job, use #run as usual.

It'll look like this:

Mosquito.configure do |settings|
  settings.backend = Mosquito::TestBackend
end

Then in your tests:

describe "testing" do
  it "enqueues the job" do
    # build and enqueue a job
    job_run = EchoJob.new(text: "hello world").enqueue

    # assert that the job was enqueued
    lastest_enqueued_job = Mosquito::TestBackend.enqueued_jobs.last

    # check the job config
    assert_equal "hello world", latest_enqueued_job.config["text"]

    # check the job_id matches
    assert_equal job_run.id, latest_enqueued_job.id

    # optionally, truncate the history
    Mosquito::TestBackend.flush_enqueued_jobs!
  end
end

This is a bare bones test helper, but I think it'll save me and other folks some headache.

@robacarp robacarp mentioned this pull request Oct 2, 2023
@jwoertink
Copy link
Contributor

Looks pretty straight forward. I dig it.

@robacarp robacarp enabled auto-merge (rebase) October 3, 2023 16:14
@robacarp robacarp merged commit d00c75c into master Oct 3, 2023
@robacarp robacarp deleted the test_mode branch October 3, 2023 16:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Test Mode
2 participants