Sha256: a48b6e50680398cb4b17e4f7c2fa938f67789933861f4e022ba55a58f05f8da3

Contents?: true

Size: 1.48 KB

Versions: 14

Compression:

Stored size: 1.48 KB

Contents

# frozen_string_literal: true
# encoding: utf-8

require 'singleton'
require 'ostruct'

module Mongo
  module BackgroundThread

    alias :start_without_tracking! :start!

    def start!
      start_without_tracking!.tap do |thread|
        BackgroundThreadRegistry.instance.register(self, thread)
      end
    end
  end
end

class BackgroundThreadRegistry
  include Singleton

  def initialize
    @lock = Mutex.new
    @records = []
  end

  def register(object, thread)
    @lock.synchronize do
      @records << OpenStruct.new(
        thread: thread,
        object: object,
        example: $current_example,
      )
    end
  end

  def verify_empty!
    @lock.synchronize do
      alive_thread_records = @records.select { |record| record.thread.alive? }
      if alive_thread_records.any?
        msg = +"Live background threads after closing all clients:"
        alive_thread_records.each do |record|
          msg << "\n  #{record.object}"
          if record.object.respond_to?(:options)
            msg << "\n  with options: #{record.object.options}"
          end
          if record.example
            msg << "\n  in #{record.example.id} #{record.example.full_description}"
          else
            msg << "\n  not in an example"
          end
        end
        raise msg
      end
      @records.clear
    end
  end
end

RSpec.configure do |config|
  config.around do |example|
    $current_example = example
    begin
      example.run
    ensure
      $current_example = nil
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

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