1begin 2 require "socket" 3 require "test/unit" 4rescue LoadError 5end 6 7 8class TestSocket_TCPSocket < Test::Unit::TestCase 9 def test_recvfrom 10 svr = TCPServer.new("localhost", 0) 11 th = Thread.new { 12 c = svr.accept 13 c.write "foo" 14 c.close 15 } 16 addr = svr.addr 17 sock = TCPSocket.open(addr[3], addr[1]) 18 assert_equal(["foo", nil], sock.recvfrom(0x10000)) 19 ensure 20 th.kill if th 21 th.join if th 22 end 23 24 def test_encoding 25 svr = TCPServer.new("localhost", 0) 26 th = Thread.new { 27 c = svr.accept 28 c.write "foo\r\n" 29 c.close 30 } 31 addr = svr.addr 32 sock = TCPSocket.open(addr[3], addr[1]) 33 assert_equal(true, sock.binmode?) 34 s = sock.gets 35 assert_equal("foo\r\n", s) 36 assert_equal(Encoding.find("ASCII-8BIT"), s.encoding) 37 ensure 38 th.kill if th 39 th.join if th 40 sock.close if sock 41 end 42end if defined?(TCPSocket) 43