class Msgr::TestPool

Public Class Methods

new(*) click to toggle source
# File lib/msgr/test_pool.rb, line 5
def initialize(*)
  @queue = []
  @mutex = Mutex.new
  @event = ConditionVariable.new
end

Private Class Methods

clear() click to toggle source
# File lib/msgr/test_pool.rb, line 67
def clear
  @instance ? @instance.clear : nil
end
Also aliased as: reset
new(*args) click to toggle source
Calls superclass method
# File lib/msgr/test_pool.rb, line 59
def new(*args)
  @instance ||= super(*args) # rubocop:disable Naming/MemoizedInstanceVariableName
end
reset()
Alias for: clear
run(*args) click to toggle source
# File lib/msgr/test_pool.rb, line 63
def run(*args)
  new.run(*args)
end

Public Instance Methods

clear() click to toggle source
# File lib/msgr/test_pool.rb, line 24
def clear
  @mutex.synchronize do
    @queue.clear
  end
end
Also aliased as: reset
post(message, &block) click to toggle source
# File lib/msgr/test_pool.rb, line 11
def post(message, &block)
  @mutex.synchronize do
    @queue << [block, message]
    @event.signal
  end
end
reset()
Alias for: clear
run(**kwargs) click to toggle source
# File lib/msgr/test_pool.rb, line 18
def run(**kwargs)
  @mutex.synchronize do
    ns_run(**kwargs)
  end
end

Private Instance Methods

ns_run(count: 1, timeout: 5) click to toggle source
# File lib/msgr/test_pool.rb, line 34
def ns_run(count: 1, timeout: 5)
  received = 0

  while received < count
    if (item = @queue.pop)
      item[0].call item[1]
      received += 1
    else
      start = Time.now.to_f

      @event.wait(@mutex, timeout)

      stop = Time.now.to_f
      diff = stop - start
      timeout -= diff

      if timeout <= 0
        raise Timeout::Error.new \
          "Expected to receive #{count} messages but received #{received}."
      end
    end
  end
end