Module: E2E::Rails

Defined in:
lib/e2e/rails.rb

Defined Under Namespace

Modules: ActiveRecordSharedConnection

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.shared_connectionObject

Returns the value of attribute shared_connection.



6
7
8
# File 'lib/e2e/rails.rb', line 6

def shared_connection
  @shared_connection
end

.shared_connection_enabledObject (readonly)

Returns the value of attribute shared_connection_enabled.



7
8
9
# File 'lib/e2e/rails.rb', line 7

def shared_connection_enabled
  @shared_connection_enabled
end

Class Method Details

.enable_shared_connection!Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/e2e/rails.rb', line 13

def enable_shared_connection!
  return unless defined?(ActiveRecord::Base)
  return if @shared_connection_enabled

  ActiveRecord::Base.singleton_class.prepend(ActiveRecordSharedConnection)
  @shared_connection_enabled = true
  # #connection (unlike #lease_connection/#with_connection, added in
  # Rails 7.2) exists on every supported Rails version, so it's the only
  # safe way to bootstrap the very first shared connection.
  self.shared_connection = ActiveRecord::Base.connection
end

.shared_connection_enabled?Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/e2e/rails.rb', line 9

def shared_connection_enabled?
  @shared_connection_enabled
end

.with_released_connectionObject

Yields while the example thread is not holding a sticky AR lease. Needed when the pool has a single connection and the server thread must check it out during a blocking Playwright navigation/action.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/e2e/rails.rb', line 28

def with_released_connection
  return yield unless shared_connection_enabled?
  return yield unless defined?(ActiveRecord::Base)

  pool = ActiveRecord::Base.connection_pool
  pool.release_connection if pool.active_connection?
  yield
ensure
  if shared_connection_enabled? && defined?(ActiveRecord::Base)
    # Keep the shared handle pointing at a live leased connection for
    # post-navigation assertions in the example thread.
    self.shared_connection = ActiveRecord::Base.connection
  end
end