features/lib/support/fake_wire_server.rb in cucumber-2.0.0.rc.4 vs features/lib/support/fake_wire_server.rb in cucumber-2.0.0.rc.5

- old
+ new

@@ -5,40 +5,42 @@ def initialize(port, protocol_table) @port, @protocol_table = port, protocol_table @delays = {} end - def run + def run(io) @server = TCPServer.open(@port) - loop { handle_connections } + loop { handle_connections(io) } end def delay_response(message, delay) @delays[message] = delay end private - def handle_connections - Thread.start(@server.accept) { |socket| open_session_on socket } + def handle_connections(io) + Thread.start(@server.accept) { |socket| open_session_on socket, io } end - def open_session_on(socket) + def open_session_on(socket, io) begin - SocketSession.new(socket, @protocol_table, @delays).start + on_message = lambda { |message| io.puts message } + SocketSession.new(socket, @protocol_table, @delays, on_message).start rescue Exception => e raise e ensure socket.close end end class SocketSession - def initialize(socket, protocol, delays) + def initialize(socket, protocol, delays, on_message) @socket = socket @protocol = protocol @delays = delays + @on_message = on_message end def start while message = @socket.gets handle(message) @@ -48,9 +50,10 @@ private def handle(data) if protocol_entry = response_to(data.strip) sleep delay(data) + @on_message.call(MultiJson.load(protocol_entry['request'])[0]) send_response(protocol_entry['response']) else serialized_exception = { :message => "Not understood: #{data}", :backtrace => [] } send_response(['fail', serialized_exception ].to_json) end