Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1 | #!/home/bernie/src/python23/dist/src/python |
| 2 | |
| 3 | import unittest |
| 4 | |
| 5 | import time |
| 6 | import socket |
| 7 | |
| 8 | class creationTestCase(unittest.TestCase): |
| 9 | """Test Case for socket.gettimeout() and socket.settimeout()""" |
| 10 | def setUp(self): |
| 11 | self.__s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 12 | |
| 13 | def tearDown(self): |
| 14 | self.__s.close() |
| 15 | |
| 16 | def testObjectCreation(self): |
| 17 | "Test Socket creation" |
| 18 | self.assertEqual(self.__s.gettimeout(), None, |
| 19 | "Timeout socket not default to disable (None)") |
| 20 | |
| 21 | def testFloatReturnValue(self): |
| 22 | "Test return value of getter/setter" |
| 23 | self.__s.settimeout(7.345) |
| 24 | self.assertEqual(self.__s.gettimeout(), 7.345, |
| 25 | "settimeout() and gettimeout() return different result") |
| 26 | |
| 27 | self.__s.settimeout(3) |
| 28 | self.assertEqual(self.__s.gettimeout(), 3, |
| 29 | "settimeout() and gettimeout() return different result") |
| 30 | |
| 31 | def testReturnType(self): |
| 32 | "Test return type of getter/setter" |
| 33 | self.__s.settimeout(1) |
| 34 | self.assertEqual(type(self.__s.gettimeout()), type(1.0), |
| 35 | "return type of gettimeout() is not FloatType") |
| 36 | |
| 37 | self.__s.settimeout(3.9) |
| 38 | self.assertEqual(type(self.__s.gettimeout()), type(1.0), |
| 39 | "return type of gettimeout() is not FloatType") |
| 40 | |
| 41 | |
| 42 | class timeoutTestCase(unittest.TestCase): |
| 43 | """Test Case for socket.socket() timeout functions""" |
| 44 | def setUp(self): |
| 45 | self.__s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 46 | self.__addr_remote = ('www.google.com', 80) |
| 47 | self.__addr_local = ('127.0.0.1', 25339) |
| 48 | |
| 49 | def tearDown(self): |
| 50 | self.__s.close() |
| 51 | |
| 52 | def testConnectTimeout(self): |
| 53 | "Test connect() timeout" |
| 54 | _timeout = 0.02 |
| 55 | self.__s.settimeout(_timeout) |
| 56 | |
| 57 | _t1 = time.time() |
| 58 | self.failUnlessRaises(socket.error, self.__s.connect, |
| 59 | self.__addr_remote) |
| 60 | _t2 = time.time() |
| 61 | |
| 62 | _delta = abs(_t1 - _t2) |
| 63 | self.assert_(_delta < _timeout + 0.5, |
| 64 | "timeout (%f) is 0.5 seconds more than required (%f)" |
| 65 | %(_delta, _timeout)) |
| 66 | |
| 67 | def testRecvTimeout(self): |
| 68 | "Test recv() timeout" |
| 69 | _timeout = 0.02 |
| 70 | self.__s.connect(self.__addr_remote) |
| 71 | self.__s.settimeout(_timeout) |
| 72 | |
| 73 | _t1 = time.time() |
| 74 | self.failUnlessRaises(socket.error, self.__s.recv, 1024) |
| 75 | _t2 = time.time() |
| 76 | |
| 77 | _delta = abs(_t1 - _t2) |
| 78 | self.assert_(_delta < _timeout + 0.5, |
| 79 | "timeout (%f) is 0.5 seconds more than required (%f)" |
| 80 | %(_delta, _timeout)) |
| 81 | |
| 82 | def testAcceptTimeout(self): |
| 83 | "Test accept() timeout()" |
| 84 | _timeout = 2 |
| 85 | self.__s.settimeout(_timeout) |
| 86 | self.__s.bind(self.__addr_local) |
| 87 | self.__s.listen(5) |
| 88 | |
| 89 | _t1 = time.time() |
| 90 | self.failUnlessRaises(socket.error, self.__s.accept) |
| 91 | _t2 = time.time() |
| 92 | |
| 93 | _delta = abs(_t1 - _t2) |
| 94 | self.assert_(_delta < _timeout + 0.5, |
| 95 | "timeout (%f) is 0.5 seconds more than required (%f)" |
| 96 | %(_delta, _timeout)) |
| 97 | |
| 98 | def testRecvfromTimeout(self): |
| 99 | "Test recvfrom() timeout()" |
| 100 | _timeout = 2 |
| 101 | self.__s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
| 102 | self.__s.settimeout(_timeout) |
| 103 | self.__s.bind(self.__addr_local) |
| 104 | |
| 105 | _t1 = time.time() |
| 106 | self.failUnlessRaises(socket.error, self.__s.recvfrom, 8192) |
| 107 | _t2 = time.time() |
| 108 | |
| 109 | _delta = abs(_t1 - _t2) |
| 110 | self.assert_(_delta < _timeout + 0.5, |
| 111 | "timeout (%f) is 0.5 seconds more than required (%f)" |
| 112 | %(_delta, _timeout)) |
| 113 | |
| 114 | def testSend(self): |
| 115 | "Test send() timeout" |
| 116 | # couldn't figure out how to test it |
| 117 | pass |
| 118 | |
| 119 | def testSendto(self): |
| 120 | "Test sendto() timeout" |
| 121 | # couldn't figure out how to test it |
| 122 | pass |
| 123 | |
| 124 | def testSendall(self): |
| 125 | "Test sendall() timeout" |
| 126 | # couldn't figure out how to test it |
| 127 | pass |
| 128 | |
| 129 | |
| 130 | def suite(): |
| 131 | suite = unittest.TestSuite() |
| 132 | |
| 133 | return suite |
| 134 | |
| 135 | if __name__ == "__main__": |
| 136 | unittest.main() |