class Lignite::Connection::Replay

Replays a recorded communication. It checks that send matches the stored sends, replays the receive.

Public Class Methods

new(filename) click to toggle source
# File lib/lignite/connection/replay.rb, line 14
def initialize(filename)
  @filename = filename

  # [
  #  {"SEND" => "foo"},
  #  {"SEND" => "foo2"},
  #  {"RECV" => "adfafd"},
  #  {"SEND" => "foo2"},
  #  {"RECV" => "adfafd"}
  # ]
  @stream = YAML.load_file(filename)
end

Public Instance Methods

close() click to toggle source
Calls superclass method Lignite::Connection#close
# File lib/lignite/connection/replay.rb, line 49
def close
  super
  raise ReplayError, "Called close but the recording has leftover data" unless @stream.empty?
end
receive() click to toggle source

@return [ByteString] a complete message

# File lib/lignite/connection/replay.rb, line 41
def receive
  recorded = @stream.shift
  raise ReplayError, "Nothing left in the recording" if recorded.nil?
  hex = recorded["RECV"]
  raise ReplayError, "Called RECV but the recording says SEND" if hex.nil?
  hex_to_bin(hex)
end
send(payload) click to toggle source

@param payload [ByteString]

# File lib/lignite/connection/replay.rb, line 28
def send(payload)
  recorded = @stream.shift
  raise ReplayError, "Nothing left in the recording (#{@filename})" if recorded.nil?
  hex = recorded["SEND"]
  raise ReplayError, "Called SEND but the recording says RECV" if hex.nil?
  data = hex_to_bin(hex)
  return if data == payload

  details = "actual: #{bin_to_hex(payload)}, recorded: #{hex}"
  raise ReplayError, "Called SEND but the recorded data does not match: #{details}"
end