Sha256: 0d09ba3c3548d1575bf26ef5920fce4c58a87938d68ba9fad547b13dd1ef59c1

Contents?: true

Size: 912 Bytes

Versions: 1

Compression:

Stored size: 912 Bytes

Contents

MockServer = Class.new

class ServerMock
  include Celluloid::IO

  def initialize(host, port, mock_target = MockServer.new)
    @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(1) { terminate }
    loop { handle_connection! @server.accept }
  end

  def handle_connection(socket)
    @clients << socket
    _, port, host = socket.peeraddr
    Logger.debug "MockServer 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 }
  end
end

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ruby_fs-0.1.0 spec/support/mock_server.rb