Module: Postburner::TestHelpers
- Defined in:
- lib/postburner/test_helpers.rb
Overview
Test helper methods for switching queue strategies in tests.
Provides instance methods for managing queue strategies in a test-safe, thread-safe manner. Use these methods to switch between strategies for individual tests or groups of tests.
Instance Method Summary collapse
-
#restore_queue_strategy! ⇒ Class
Restore the previously stashed queue strategy.
-
#switch_queue_strategy!(strategy) ⇒ Class
Switch to a different queue strategy for testing.
-
#use_queue_strategy(strategy) { ... } ⇒ Object
Execute a block with a temporary queue strategy.
Instance Method Details
#restore_queue_strategy! ⇒ Class
Restore the previously stashed queue strategy.
Restores the strategy that was active when #switch_queue_strategy! was called. Clears the thread-local stash after restoring.
95 96 97 98 99 100 101 102 103 |
# File 'lib/postburner/test_helpers.rb', line 95 def restore_queue_strategy! original = Thread.current[:postburner_original_strategy] if original.nil? raise "No strategy to restore. Did you call switch_queue_strategy! first?" end Postburner.queue_strategy = original Thread.current[:postburner_original_strategy] = nil end |
#switch_queue_strategy!(strategy) ⇒ Class
Switch to a different queue strategy for testing.
Stashes the current strategy in a thread-local variable so it can be restored later with #restore_queue_strategy!. This is thread-safe for parallel test execution.
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/postburner/test_helpers.rb', line 62 def switch_queue_strategy!(strategy) Thread.current[:postburner_original_strategy] = Postburner.queue_strategy Postburner.queue_strategy = case strategy when Symbol strategy_method = "#{strategy}_strategy!".to_sym if Postburner.respond_to?(strategy_method, true) Postburner.send(strategy_method) Postburner.queue_strategy else raise ArgumentError, "Unknown strategy: #{strategy}. Available: inline_test, time_travel_test, strict, default, null" end when Class strategy else raise ArgumentError, "Expected Class or Symbol, got #{strategy.class}" end end |
#use_queue_strategy(strategy) { ... } ⇒ Object
Execute a block with a temporary queue strategy.
Automatically switches to the specified strategy before the block and restores the original strategy after, even if an exception occurs.
126 127 128 129 130 131 |
# File 'lib/postburner/test_helpers.rb', line 126 def use_queue_strategy(strategy, &block) switch_queue_strategy!(strategy) yield ensure restore_queue_strategy! end |