Sha256: df9520e55cf2bcca1c61447b403f986a9f62df56c69a5bdfd13e24b23a6e012c

Contents?: true

Size: 991 Bytes

Versions: 5

Compression:

Stored size: 991 Bytes

Contents

# encoding: utf-8
MockServer = Class.new

class ServerMock
  include Celluloid::IO

  def initialize(host, port, mock_target = MockServer.new)
    puts "*** Starting echo server on #{host}:#{port}"
    @server = TCPServer.new host, port
    @mock_target = mock_target
    @clients = []
    run!
  end

  def finalize
    Logger.debug "ServerMock finalizing"
    @server.close if @server
    @clients.each(&:close)
  end

  def run
    after(0.5) { terminate }
    loop { handle_connection! @server.accept }
  end

  def handle_connection(socket)
    @clients << socket
    _, port, host = socket.peeraddr
    puts "*** Received connection from #{host}:#{port}"
    loop { receive_data socket.readpartial(4096) }
  end

  def receive_data(data)
    Logger.debug "ServerMock receiving data: #{data}"
    @mock_target.receive_data data, self
  end

  def send_data(data)
    @clients.each { |client| client.write data.gsub("\n", "\r\n") }
  end
end

def client
  @client ||= mock('Client')
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
ruby_ami-1.3.1 spec/support/mock_server.rb
ruby_ami-1.3.0 spec/support/mock_server.rb
ruby_ami-1.2.6 spec/support/mock_server.rb
ruby_ami-1.2.5 spec/support/mock_server.rb
ruby_ami-1.2.4 spec/support/mock_server.rb