Class: Postburner::InlineTestQueue

Inherits:
StrictQueue show all
Defined in:
lib/postburner/strategies/inline_test_queue.rb

Overview

Note:

Auto-detected when Rails.env.test? and ActiveJob adapter is :test

Inline test queue strategy with strict scheduling enforcement.

This strategy executes jobs inline/synchronously without Beanstalkd, making tests fast and predictable. It raises Job::PrematurePerform if a job has a future run_at, forcing you to use explicit time travel in tests.

## When to Use InlineTestQueue

Choose this strategy for test environments where you want:

  • **Explicit time management:** Forces you to use ‘travel_to` for scheduled jobs

  • **Strict testing:** Catches scheduling bugs by failing loudly

  • **Synchronous execution:** Jobs run immediately on ‘queue!` call

  • **No Beanstalkd:** Tests run without external dependencies

  • **Predictable timing:** Full control over when jobs execute

This is ideal for unit/integration tests where you want to verify job scheduling logic and maintain explicit control over time progression.

## Strategy Behavior

  • Execution: Synchronous/inline (no Beanstalkd required)

  • **Testing mode:** Returns true

  • **Premature execution:** Raises Job::PrematurePerform exception

  • Beanstalkd: Not used (bkid remains nil)

  • **Time travel:** Requires explicit ‘travel_to` for scheduled jobs

## Usage

Examples:

Automatically activated in Rails test environment

# In test_helper.rb or rails_helper.rb
# InlineTestQueue is automatically set if Rails.env.test?
# and ActiveJob.queue_adapter == :test

Explicitly activate InlineTestQueue

Postburner.inline_test_strategy!
job = MyJob.create!(args: { user_id: 123 })
job.queue!
# Job executes immediately and synchronously
assert job.reload.processed_at

Scheduled jobs require explicit time travel

Postburner.inline_test_strategy!
job = MyJob.create!(args: {})

# This will raise PrematurePerform
# job.queue!(delay: 1.hour)

# Use time travel instead
job.queue!(delay: 1.hour)
travel_to(job.run_at) do
  # Job executes within the time travel block
end

Testing with scheduled jobs

test "processes payment after delay" do
  Postburner.inline_test_strategy!
  job = ProcessPayment.create!(args: { payment_id: 123 })

  future_time = 2.hours.from_now
  job.queue!(at: future_time)

  travel_to(future_time) do
    # Job executes here
    assert job.reload.processed_at
  end
end

See Also:

Direct Known Subclasses

TimeTravelTestQueue

Class Method Summary collapse

Methods inherited from StrictQueue

handle_perform!

Class Method Details

.handle_premature_perform(job) ⇒ void

This method returns an undefined value.

Handles jobs executed before their scheduled run_at time.

This strategy raises an exception with a helpful error message, forcing developers to use explicit ‘travel_to` calls in tests.

Parameters:

Raises:



123
124
125
# File 'lib/postburner/strategies/inline_test_queue.rb', line 123

def handle_premature_perform(job)
  raise Postburner::Job::PrematurePerform, "Job scheduled for #{job.run_at} (#{((job.run_at - Time.current) / 60).round(1)} minutes from now). Use `travel_to(job.run_at)` in your test, or set `Postburner.time_travel_test_strategy!` i.e. `Postburner::TimeTravelTestQueue` to execute scheduled jobs immediately."
end

.insert(job, options = {}) ⇒ Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Executes job inline/synchronously without Beanstalkd.

Called automatically via after_save_commit hook when Job#queue! is invoked. Executes the job immediately in the same process. If the job has a future run_at, raises Job::PrematurePerform to force explicit time management with ‘travel_to`.

Parameters:

  • job (Postburner::Job)

    The job to execute

  • options (Hash) (defaults to: {})

    Unused in test mode

Returns:

  • (Hash)

    Status hash with :status => ‘INLINE’, :id => nil

Raises:



103
104
105
106
107
108
109
110
# File 'lib/postburner/strategies/inline_test_queue.rb', line 103

def insert(job, options = {})
  # Execute immediately in test mode
  # Will raise PrematurePerform if run_at is in the future
  job.perform!(job.args)

  # Return format matching Beanstalkd response (symbol keys)
  { status: 'INLINE', id: nil }
end

.testingBoolean

Returns whether this strategy is for testing.

Returns:

  • (Boolean)

    Always true for test strategies



83
84
85
# File 'lib/postburner/strategies/inline_test_queue.rb', line 83

def testing
  true
end