Sha256: 328faae23557a3750d030ef7d773ce49433a9e699d36cffae52fbcd01b9b1f5a

Contents?: true

Size: 1.08 KB

Versions: 2

Compression:

Stored size: 1.08 KB

Contents

class TestServer
  attr_accessor :host, :port, :connect_count, :commands

  def initialize
    @connect_count = 0
    @connections = []
    @commands = []
    listen
  end

  def listen
    @port ||= 10001
    @server = TCPServer.new(port)
    Thread.new do
      begin
        # puts "listening"
        socket = @server.accept
        @connect_count += 1
        @connections << socket
        # puts "connection received"
        loop do
          command = socket.gets.strip
          # puts "got: #{command}"
          commands << command
        end
      rescue Exception => err
        unless @stopping
          # puts "EXCEPTION:", err unless @stopping
          retry
        end
      end
    end
    # puts "server up"
  rescue Exception => err
    # FIXME: doesn't seem to be detecting failures of listen
    puts "failed to get port"
    @port += 1
    retry
  end

  def host_and_port
    "#{host}:#{port}"
  end

  def stop
    @stopping = true
    disconnect_all
    @server.close # FIXME: necessary?
  end

  def disconnect_all
    @connections.each { |c| c.close rescue false }
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
instrumental_agent-0.3.0 spec/test_server.rb
instrumental_agent-0.2.0 spec/test_server.rb