Class: Lignite::Connection::Replay

Inherits:
Lignite::Connection show all
Includes:
Bytes
Defined in:
lib/lignite/connection/replay.rb

Overview

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

Instance Method Summary collapse

Methods included from Bytes

#bin_to_hex, #f32, #hex_to_bin, #u16, #u32, #u8, #unpack_f32, #unpack_u16, #unpack_u32, #unpack_u8

Methods inherited from Lignite::Connection

create, #read, reset, #write

Methods included from Logger

default_logger, #logger

Constructor Details

#initialize(filename) ⇒ Replay

Returns a new instance of Replay.



14
15
16
17
18
19
20
21
22
23
24
25
# 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

Instance Method Details

#closeObject

Raises:



49
50
51
52
# 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

#receiveByteString

Returns a complete message.

Returns:

Raises:



41
42
43
44
45
46
47
# 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) ⇒ Object

Parameters:

Raises:



28
29
30
31
32
33
34
35
36
37
38
# 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