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.

Examples:

Using in setup/teardown

class MyTest < ActiveSupport::TestCase
  include Postburner::TestHelpers

  def setup
    switch_queue_strategy!(:time_travel_test)
  end

  def teardown
    restore_queue_strategy!
  end

  def test_something
    job = MyJob.create!(args: {})
    job.queue!(delay: 1.hour)
    assert job.reload.processed_at
  end
end

Using block form

class MyTest < ActiveSupport::TestCase
  include Postburner::TestHelpers

  def test_something
    use_queue_strategy(:time_travel_test) do
      job = MyJob.create!(args: {})
      job.queue!(delay: 1.hour)
      assert job.reload.processed_at
    end
  end
end

Instance Method Summary collapse

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.

Examples:

Typical usage in teardown

def teardown
  restore_queue_strategy!
end

Returns:

  • (Class)

    The restored strategy class

Raises:

  • (RuntimeError)

    if no strategy was stashed



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.

Examples:

With class constant

switch_queue_strategy!(Postburner::TimeTravelTestQueue)

With symbol

switch_queue_strategy!(:time_travel_test)

Parameters:

  • strategy (Class, Symbol)

    Strategy class or symbol name Symbols: :inline_test, :time_travel_test, :strict, :default, :null

Returns:

  • (Class)

    The newly activated strategy class

Raises:

  • (ArgumentError)

    if strategy is invalid type or unknown symbol



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.

Examples:

use_queue_strategy(:time_travel_test) do
  job = MyJob.create!(args: {})
  job.queue!(delay: 1.hour)
  assert job.reload.processed_at
end

Parameters:

  • strategy (Class, Symbol)

    Strategy class or symbol name Symbols: :inline_test, :time_travel_test, :strict, :default, :null

Yields:

  • Block to execute with the temporary strategy

Returns:

  • (Object)

    The return value of the block

Raises:

  • (ArgumentError)

    if strategy is invalid type or unknown symbol



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