Sha256: 66cdc5e23f3926c80ce3de3fdc3ba04287a7d672ebfe9ca9f010a36b7bfe5d85

Contents?: true

Size: 943 Bytes

Versions: 6

Compression:

Stored size: 943 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) }
  rescue EOFError
    Logger.debug "Connection from #{host}:#{port} closed"
  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

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
ruby_fs-1.0.4 spec/support/mock_server.rb
ruby_fs-1.0.3 spec/support/mock_server.rb
ruby_fs-1.0.2 spec/support/mock_server.rb
ruby_fs-1.0.1 spec/support/mock_server.rb
ruby_fs-1.0.0 spec/support/mock_server.rb
ruby_fs-0.3.1 spec/support/mock_server.rb