Sha256: 9428e42661c8a6699f10e521d2fa989e5bd73eb25f163545a8724d35cbeeb782

Contents?: true

Size: 1.2 KB

Versions: 10

Compression:

Stored size: 1.2 KB

Contents

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

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

  def listen
    @port ||= 10001
    @server = TCPServer.new(port)
    Thread.new do
      begin
        # puts "listening"
        loop do
          socket = @server.accept
          Thread.new do
            @connect_count += 1
            @connections << socket
            # puts "connection received"
            loop do
              command = socket.gets.strip
              # puts "got: #{command}"
              commands << command
            end
          end
        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

10 entries across 10 versions & 1 rubygems

Version Path
instrumental_agent-0.8.1 spec/test_server.rb
instrumental_agent-0.8.0 spec/test_server.rb
instrumental_agent-0.7.2 spec/test_server.rb
instrumental_agent-0.7.1 spec/test_server.rb
instrumental_agent-0.7 spec/test_server.rb
instrumental_agent-0.6.1 spec/test_server.rb
instrumental_agent-0.6.0 spec/test_server.rb
instrumental_agent-0.5.1 spec/test_server.rb
instrumental_agent-0.5.0 spec/test_server.rb
instrumental_agent-0.4.0 spec/test_server.rb