Class: Postburner::TimeTravelTestQueue
- Inherits:
-
InlineTestQueue
- Object
- StrictQueue
- InlineTestQueue
- Postburner::TimeTravelTestQueue
- Extended by:
- TimeHelpers
- Defined in:
- lib/postburner/strategies/time_travel_test_queue.rb
Overview
Requires ActiveSupport::Testing::TimeHelpers (Rails testing framework)
Time is stubbed globally during execution - side effects may occur
Multiple scheduled jobs execute in queue order, not scheduled order
Test queue strategy with automatic time travel for scheduled jobs.
This strategy executes jobs inline/synchronously like InlineTestQueue, but automatically uses time travel for scheduled jobs instead of raising exceptions. When a job has a future run_at, it travels to that time, executes the job, then returns to the present.
## When to Use TimeTravelTestQueue
Choose this strategy for test environments where you want:
-
**Automatic time management:** No need to manually call ‘travel_to`
-
**Simple scheduled job testing:** Test delayed/scheduled jobs without boilerplate
-
**Fast tests:** Jobs execute immediately regardless of schedule
-
**No Beanstalkd:** Tests run without external dependencies
-
**Convenience over control:** Less explicit but easier to use than InlineTestQueue
This is ideal for integration/feature tests where you want to verify that scheduled jobs execute correctly without managing time travel yourself. The tradeoff is less explicit control over timing compared to InlineTestQueue.
## Strategy Behavior
-
Execution: Synchronous/inline (no Beanstalkd required)
-
**Testing mode:** Returns true
-
**Premature execution:** Automatically travels to run_at and executes
-
Beanstalkd: Not used (bkid remains nil)
-
**Time travel:** Automatic using ActiveSupport::Testing::TimeHelpers
-
**Execution order:** Jobs may execute out of intended chronological order
## How Time Travel Works
When a job with future run_at is queued:
-
Detects job.run_at > Time.zone.now
-
Calls ‘travel_to(job.run_at)` to stub global time
-
Executes job at the scheduled time (Time.zone.now == job.run_at)
-
Returns to present time after execution
-
Job timestamps reflect the scheduled time, not actual time
## Usage
Class Method Summary collapse
-
.insert(job, options = {}) ⇒ Hash
private
Executes job inline with automatic time travel for scheduled jobs.
Methods included from TimeHelpers
Methods inherited from InlineTestQueue
handle_premature_perform, testing
Methods inherited from StrictQueue
handle_perform!, handle_premature_perform, testing
Class Method Details
.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 with automatic time travel for scheduled jobs.
Called automatically via after_save_commit hook when Job#queue! is invoked. If the job has a future run_at, travels to that time before execution. Otherwise executes immediately.
Uses recursion detection to prevent infinite loops when jobs create and enqueue other jobs (e.g., schedule callbacks). Jobs enqueued while already inside a perform! call are queued but not executed immediately.
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/postburner/strategies/time_travel_test_queue.rb', line 121 def insert(job, = {}) # Detect recursion: if we're already performing a job, don't execute # the newly enqueued job immediately. This prevents infinite loops # when job callbacks (like schedule_next_execution) enqueue new jobs. if Thread.current[:postburner_performing] return { status: 'INLINE_DEFERRED', id: nil } end begin Thread.current[:postburner_performing] = true # If job has a future run_at, travel to that time for execution if job.run_at && job.run_at > Time.current travel_to(job.run_at) do job.perform!(job.args) end else # No future run_at, execute normally job.perform!(job.args) end ensure Thread.current[:postburner_performing] = false end # Return format matching Beanstalkd response (symbol keys) { status: 'INLINE', id: nil } end |