Sha256: f9a7e9fbf393c1380bfb74e1b314d79d4f4655c7e950af9a7854ffbde685d4a6

Contents?: true

Size: 973 Bytes

Versions: 6

Compression:

Stored size: 973 Bytes

Contents

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

6 entries across 6 versions & 1 rubygems

Version Path
ruby_ami-1.2.3 spec/support/mock_server.rb
ruby_ami-1.2.2 spec/support/mock_server.rb
ruby_ami-1.2.1 spec/support/mock_server.rb
ruby_ami-1.2.0 spec/support/mock_server.rb
ruby_ami-1.1.2 spec/support/mock_server.rb
ruby_ami-1.1.1 spec/support/mock_server.rb