Sha256: 6902c74406c473b8e5c10b92043f26c3f05ec23db676fb674d79d8a70acd4620

Contents?: true

Size: 1.07 KB

Versions: 13

Compression:

Stored size: 1.07 KB

Contents

# frozen_string_literal: true
# encoding: utf-8

require 'singleton'

module Mongo
  class Client
    alias :get_session_without_tracking :get_session

    def get_session(options = {})
      get_session_without_tracking(options).tap do |session|
        SessionRegistry.instance.register(session)
      end
    end
  end

  class Session
    alias :end_session_without_tracking :end_session

    def end_session
      SessionRegistry.instance.unregister(self)
      end_session_without_tracking
    end
  end
end


class SessionRegistry
  include Singleton

  def initialize
    @registry = {}
  end

  def register(session)
    @registry[session.session_id] = session if session
  end

  def unregister(session)
    @registry.delete(session.session_id) unless session.ended?
  end

  def verify_sessions_ended!
    @registry.delete_if { |_, session| session.ended? }

    unless @registry.empty?
      sessions = @registry.map { |_, session| session }
      raise "Session registry contains live sessions: #{sessions.join(', ')}"
    end
  end

  def clear_registry
    @registry = {}
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
mongo-2.16.4 spec/support/session_registry.rb
mongo-2.17.4 spec/support/session_registry.rb
mongo-2.17.3 spec/support/session_registry.rb
mongo-2.17.2 spec/support/session_registry.rb
mongo-2.16.3 spec/support/session_registry.rb
mongo-2.16.2 spec/support/session_registry.rb
mongo-2.17.1 spec/support/session_registry.rb
mongo-2.16.1 spec/support/session_registry.rb
mongo-2.17.0 spec/support/session_registry.rb
mongo-2.16.0 spec/support/session_registry.rb
mongo-2.15.1 spec/support/session_registry.rb
mongo-2.16.0.alpha1 spec/support/session_registry.rb
mongo-2.15.0 spec/support/session_registry.rb