Module: Sidekiq::TransactionGuard::RSpecIntegration Private

Defined in:
lib/sidekiq/transaction_guard/rspec.rb

Overview

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

RSpec integration. Requiring this file wraps every example in a Sidekiq::TransactionGuard.testing block and manages the transaction guard mode with a thread local override so parallel test threads can't interfere with each other.

The per example mode can be controlled with the :sidekiq_transaction_guard metadata tag. The value can be a mode symbol, false to disable the guard, or :default to use the globally configured mode. Untagged examples run with the :error mode.

This file should be required after rspec-rails (or any other library that opens transactions in its setup hooks) so that the transaction level snapshot taken before each example runs after those transactions are opened.

Class Method Summary collapse

Class Method Details

.apply_example_mode(example) ⇒ Object

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.



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/sidekiq/transaction_guard/rspec.rb', line 43

def self.apply_example_mode(example)
   = example.
  if .key?(:sidekiq_transaction_guard)
    mode = [:sidekiq_transaction_guard]
    mode = :disabled if mode == false
    mode = nil if mode == :default
    mode = :error unless mode.nil? || mode.is_a?(Symbol)
    Sidekiq::TransactionGuard.thread_local_mode = mode
  elsif Sidekiq::TransactionGuard.thread_local_mode == :disabled
    # Apply the default mode only if an earlier hook hasn't already set one.
    Sidekiq::TransactionGuard.thread_local_mode = :error
  end
end

.included(example_group) ⇒ Object

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.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/sidekiq/transaction_guard/rspec.rb', line 23

def self.included(example_group)
  # Nested example groups inherit hooks from their parent group, so only
  # register the hooks on the outermost group that includes this module.
  return if example_group.is_a?(Class) && example_group.superclass.include?(RSpecIntegration)

  # These are registered as group level hooks so they run after hooks added
  # by modules included earlier — in particular after rspec-rails has opened
  # the transactional fixture transaction — so the snapshot taken here
  # treats transactions opened by test setup as the baseline.
  example_group.before do |example|
    Sidekiq::TransactionGuard::RSpecIntegration.apply_example_mode(example)
    Sidekiq::TransactionGuard.set_allowed_transaction_level(:all)
  end

  example_group.after do
    # Disable the guard before fixture rollback and other teardown hooks run.
    Sidekiq::TransactionGuard.thread_local_mode = :disabled
  end
end