class Object

Constants

EVENT_WAIT_COUNT
EVENT_WAIT_TIME
TEMP_PATH

Public Instance Methods

receive_and_check(args = {}) { |e| ... } click to toggle source
# File lib/log-courier/rspec/spec_helper.rb, line 171
def receive_and_check(args = {})
  args = {
    total: nil,
    check: true,
    check_file: true,
    check_order: true,
    host: nil,
  }.merge!(args)

  # Quick check of the total events we are expecting - but allow time to receive them
  total = if args[:total].nil?
            @files.reduce(0) do |sum, f|
              sum + f.count
            end
          else
            args[:total]
          end

  args.delete_if do |_, v|
    v.nil?
  end

  orig_total = total
  check = args[:check]

  waited = 0
  while total.positive? && waited <= EVENT_WAIT_COUNT
    if @event_queue.length.zero?
      sleep(EVENT_WAIT_TIME)
      waited += 1
      next
    end

    waited = 0
    until @event_queue.length.zero?
      e = @event_queue.pop
      total -= 1
      next unless check

      yield e
    end
  end

  # Fancy calculation to give a nice "expected" output of expected num of events
  expect(orig_total - total).to eq orig_total
  nil
end
server_count(id = '__default__') click to toggle source

A helper to get number of events received on the server

# File lib/log-courier/rspec/spec_helper.rb, line 167
def server_count(id = '__default__')
  @server_counts[id]
end
server_port(id = '__default__') click to toggle source

A helper to get the port a server is bound to

# File lib/log-courier/rspec/spec_helper.rb, line 162
def server_port(id = '__default__')
  @servers[id].port
end
shutdown_client(which = nil) click to toggle source
# File lib/log-courier/rspec/spec_helper.rb, line 93
def shutdown_client(which = nil)
  which = if which.nil?
            @clients.keys
          else
            [which]
          end
  which.each do |id|
    @clients[id].shutdown
    @clients.delete id
  end
  nil
end
shutdown_server(which = nil) click to toggle source

A helper to shutdown a Log Courier server

# File lib/log-courier/rspec/spec_helper.rb, line 145
def shutdown_server(which = nil)
  which = if which.nil?
            @servers.keys
          else
            [which]
          end
  which.each do |id|
    @server_threads[id].raise LogCourier::ShutdownSignal
    @server_threads[id].join
    @server_threads.delete id
    @server_counts.delete id
    @servers.delete id
  end
  nil
end
start_client(**args) click to toggle source
# File lib/log-courier/rspec/spec_helper.rb, line 69
def start_client(**args)
  args = {
    id: '__default__',
    transport: 'tls',
    addresses: ['127.0.0.1'],
  }.merge!(**args)

  args[:ssl_ca] = @ssl_cert.path if args[:transport] == 'tls'

  id = args[:id]
  args[:port] = server_port(id) unless args.key?(:port)

  logger = Cabin::Channel.new
  logger.subscribe $stdout
  logger['instance'] = "Client #{id}"
  logger.level = :debug

  # Reset server for each test
  @clients[id] = LogCourier::Client.new(
    logger: logger,
    **args,
  )
end
start_server(**args) click to toggle source
# File lib/log-courier/rspec/spec_helper.rb, line 106
def start_server(**args)
  args = {
    id: '__default__',
    transport: 'tls',
  }.merge!(**args)

  if args[:transport] == 'tls'
    args[:ssl_certificate] = @ssl_cert.path
    args[:ssl_key] = @ssl_key.path
  end

  id = args[:id]

  logger = Cabin::Channel.new
  logger.subscribe $stdout
  logger['instance'] = "Server #{id}"
  logger.level = :debug

  raise 'Server already initialised' if @servers.key?(id)

  # Reset server for each test
  @servers[id] = LogCourier::Server.new(
    logger: logger,
    **args,
  )

  @server_counts[id] = 0
  @server_threads[id] = Thread.new do
    @servers[id].run do |event|
      @server_counts[id] += 1
      @event_queue << event
    end
  rescue LogCourier::ShutdownSignal
    0
  end
  @servers[id]
end