You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: test_socket.rb
    path of file: ./repos/truffleruby/test/mri/tests/socket
    
    the code of the file until where you have to start completion: # frozen_string_literal: true

begin
  require "socket"
  require "tmpdir"
  require "fcntl"
  require "etc"
  require "test/unit"
rescue LoadError
end

class TestSocket < Test::Unit::TestCase
  def test_socket_new
    begin
      s = Socket.new(:INET, :STREAM)
      assert_kind_of(Socket, s)
    ensure
      s.close
    end
  end

  def test_socket_new_cloexec
    return unless defined? Fcntl::FD_CLOEXEC
    begin
      s = Socket.new(:INET, :STREAM)
      assert(s.close_on_exec?)
    ensure
      s.close
    end
  end

  def test_unpack_sockaddr
    sockaddr_in = Socket.sockaddr_in(80, "")
    assert_raise(ArgumentError) { Socket.unpack_sockaddr_un(sockaddr_in) }
    sockaddr_un = Socket.sockaddr_un("/testdir/s")
    assert_raise(ArgumentError) { Socket.unpack_sockaddr_in(sockaddr_un) }
    assert_raise(ArgumentError) { Socket.unpack_sockaddr_in("") }
    assert_raise(ArgumentError) { Socket.unpack_sockaddr_un("") }
  end if Socket.respond_to?(:sockaddr_un)

  def test_sysaccept
    serv = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
    serv.bind(Socket.sockaddr_in(0, "127.0.0.1"))
    serv.listen 5
    c = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
    c.connect(serv.getsockname)
    fd, peeraddr = serv.sysaccept
    assert_equal(c.getsockname, peeraddr.to_sockaddr)
  ensure
    serv.close if serv
    c.close if c
    IO.for_fd(fd).close if fd
  end

  def test_initialize
    Socket.open(Socket::AF_INET, Socket::SOCK_STREAM, 0) {|s|
      s.bind(Socket.sockaddr_in(0, "127.0.0.1"))
      addr = s.getsockname
      assert_nothing_raised { Socket.unpack_sockaddr_in(addr) }
      assert_raise(ArgumentError, NoMethodError) { Socket.unpack_sockaddr_un(addr) }
    }
    Socket.open("AF_INET", "SOCK_STREAM", 0) {|s|
      s.bind(Socket.sockaddr_in(0, "127.0.0.1"))
      addr = s.getsockname
      assert_nothing_raised { Socket.unpack_sockaddr_in(addr) }
   
    
  def test_closed_read
    require 'timeout'
    require 'socket'
    bug4390 = '[ruby-core:35203]'
    server = TCPServer.new("localhost", 0)
    serv_thread = Thread.new {server.accept}
    begin sleep(0.1) end until serv_thread.stop?
    sock = TCPSocket.new("localhost", server.addr[1])
    client_thread = Thread.new do
      assert_raise(IOError, bug4390) {
        sock.readline
      }
    end
    begin sleep(0.1) end until client_thread.stop?
    Timeout.timeout(1) do
      sock.close
      sock = nil
      client_thread.join
    end
  ensure
    serv_thread.value.close
    server.close
  end unless RUBY_PLATFORM.include?("freebsd")

  def test_connect_timeout
    host = "127.0.0.1"
    server = TCPServer.new(host, 0)
    port = server.addr[1]
    serv_thread = Thread.new {server.accept}
    sock = Socket.tcp(host, port, :connect_timeout => 30)
    accepted = serv_thread.value
    assert_kind_of TCPSocket, accepted
    assert_equal sock, IO.select(nil, [ sock ])[1][0], "not writable"
    sock.close

    # some platforms may not timeout when the listener queue overflows,
    # but we know Linux does with the default listen backlog of SOMAXCONN for
    # TCPServer.
    assert_raise(Errno::ETIMEDOUT) do
      (Socket::SOMAXCONN*2).times do |i|
        sock = Socket.tcp(host, port, :connect_timeout => 0)
        assert_equal sock, IO.select(nil, [ sock ])[1][0],
                     "not writable (#{i})"
        sock.close
      end
    end if RUBY_PLATFORM =~ /linux/
  ensure
    server.close
    accepted.close if accepted
    sock.close if sock && ! sock.closed?
  end

  def test_getifaddrs
    begin
      list = Socket.getifaddrs
    rescue NotImplementedError
      return
    end
    list.each {|ifaddr|
      assert_instance_of(Socket::Ifaddr, ifaddr)
    }
  end

  def test_connect_in_rescue
    serv = Addrinfo.tcp(nil, 0).listen
    addr = serv.connect_address
    begin
      raise "dummy error"
    rescue
      s = addr.connect
      assert(!s.closed?)
    end
  ensure
    serv.close if serv && !serv.closed?
    s.close if s && !s.closed?
  end

  def test_bind_in_rescue
    begin
      raise "dummy error"
    rescue
      s = Addrinfo.tcp(nil, 0).bind
      assert(!s.closed?)
    end
  ensure
    s.close if s && !s.closed?
  end