1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
|
Given /^there is a wire server (running |)on port (\d+) which understands the following protocol:$/ do |running, port, table|
protocol = table.hashes.map do |table_hash|
table_hash['response'] = table_hash['response'].gsub(/\n/, '\n')
table_hash
end
@server = FakeWireServer.new(port.to_i, protocol)
start_wire_server if running.strip == "running"
end
Given /^the wire server takes (.*) seconds to respond to the invoke message$/ do |timeout|
@server.delay_response(:invoke, timeout.to_f)
start_wire_server
end
Given /^I have environment variable (\w+) set to "([^"]*)"$/ do |variable, value|
set_env(variable, value)
end
Then(/^the wire server should have received the following messages:$/) do |expected_messages|
expect(messages_received).to eq expected_messages.raw.flatten
end
module WireHelper
attr_reader :messages_received
def start_wire_server
@messages_received = []
reader, writer = IO.pipe
@wire_pid = fork {
reader.close
@server.run(writer)
}
writer.close
Thread.new do
while message = reader.gets
@messages_received << message.strip
end
end
at_exit { stop_wire_server }
end
def stop_wire_server
return unless @wire_pid
Process.kill('KILL', @wire_pid)
Process.wait(@wire_pid)
rescue Errno::ESRCH
# No such process - wire server has already been stopped by the After hook
end
end
Before do
extend(WireHelper)
end
After do
stop_wire_server
end
|