Project

General

Profile

Feature #5101 ยป 0001-test-socket-test_socket.rb-add-test-for-Socket.tcp-w.patch

normalperson (Eric Wong), 08/11/2011 05:33 AM

View differences:

test/socket/test_socket.rb
430 430
  ensure
431 431
    server.close
432 432
  end
433

  
434
  def test_connect_timeout
435
    host = "localhost"
436
    server = TCPServer.new(host, 0)
437
    port = server.addr[1]
438
    serv_thread = Thread.new {server.accept}
439
    sock = Socket.tcp(host, port, :connect_timeout => 30)
440
    accepted = serv_thread.value
441
    assert_kind_of TCPSocket, accepted
442
    assert_equal sock, IO.select(nil, [ sock ])[1][0], "not writable"
443
    sock.close
444

  
445
    # some platforms may not timeout when the listener queue overflows,
446
    # but we know Linux does with the default listen backlog of 5 for TCPServer
447
    assert_raises(Errno::ETIMEDOUT) do
448
      200.times do |i|
449
        sock = Socket.tcp(host, port, :connect_timeout => 0)
450
        assert_equal sock, IO.select(nil, [ sock ])[1][0],
451
                     "not writable (#{i})"
452
        sock.close
453
      end
454
    end if RUBY_PLATFORM =~ /linux/
455
  ensure
456
    server.close
457
    accepted.close if accepted
458
    sock.close if sock && ! sock.closed?
459
  end
433 460
end if defined?(Socket)