lib/lignite/connection/replay.rb in lignite-0.3.0 vs lib/lignite/connection/replay.rb in lignite-0.4.0
- old
+ new
@@ -1,15 +1,18 @@
require "yaml"
module Lignite
class Connection
+ # Errors that may happen when using a {Replay} connection.
class ReplayError < RuntimeError
end
# Replays a recorded communication.
# It checks that #send matches the stored sends, replays the #receive.
class Replay < Connection
+ include Bytes
+
def initialize(filename)
@filename = filename
# [
# {"SEND" => "foo"},
@@ -22,15 +25,18 @@
end
# @param payload [ByteString]
def send(payload)
recorded = @stream.shift
- raise ReplayError, "Nothing left in the recording" if recorded.nil?
+ 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)
- raise ReplayError, "Called SEND but the recorded data does not match" if data != payload
+ 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
# @return [ByteString] a complete message
def receive
recorded = @stream.shift
@@ -41,17 +47,9 @@
end
def close
super
raise ReplayError, "Called close but the recording has leftover data" unless @stream.empty?
- end
-
- private
-
- # @param hex [String] "413432"
- # @return [ByteString] "A42"
- def hex_to_bin(hex)
- [hex].pack("H*")
end
end
end
end