summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_timeout.py
blob: c71efa78ac6c0918fee6da2405406e9baa3d0719 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/home/bernie/src/python23/dist/src/python

import unittest

import time
import socket

class creationTestCase(unittest.TestCase):
    """Test Case for socket.gettimeout() and socket.settimeout()"""
    def setUp(self):
        self.__s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    def tearDown(self):
        self.__s.close()

    def testObjectCreation(self):
        "Test Socket creation"
        self.assertEqual(self.__s.gettimeout(), None, 
            "Timeout socket not default to disable (None)")

    def testFloatReturnValue(self):
        "Test return value of getter/setter"
        self.__s.settimeout(7.345)
        self.assertEqual(self.__s.gettimeout(), 7.345, 
            "settimeout() and gettimeout() return different result")

        self.__s.settimeout(3)
        self.assertEqual(self.__s.gettimeout(), 3, 
            "settimeout() and gettimeout() return different result")

    def testReturnType(self):
        "Test return type of getter/setter"
        self.__s.settimeout(1)
        self.assertEqual(type(self.__s.gettimeout()), type(1.0),
            "return type of gettimeout() is not FloatType")

        self.__s.settimeout(3.9)
        self.assertEqual(type(self.__s.gettimeout()), type(1.0),
            "return type of gettimeout() is not FloatType")


class timeoutTestCase(unittest.TestCase):
    """Test Case for socket.socket() timeout functions"""
    def setUp(self):
        self.__s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.__addr_remote = ('www.google.com', 80)
        self.__addr_local  = ('127.0.0.1', 25339)

    def tearDown(self):
        self.__s.close()

    def testConnectTimeout(self):
        "Test connect() timeout"
        _timeout = 0.02
        self.__s.settimeout(_timeout)

        _t1 = time.time()
        self.failUnlessRaises(socket.error, self.__s.connect,
                self.__addr_remote)
        _t2 = time.time()

        _delta = abs(_t1 - _t2)
        self.assert_(_delta < _timeout + 0.5,
                "timeout (%f) is 0.5 seconds more than required (%f)"
                %(_delta, _timeout))

    def testRecvTimeout(self):
        "Test recv() timeout"
        _timeout = 0.02
        self.__s.connect(self.__addr_remote)
        self.__s.settimeout(_timeout)

        _t1 = time.time()
        self.failUnlessRaises(socket.error, self.__s.recv, 1024)
        _t2 = time.time()

        _delta = abs(_t1 - _t2)
        self.assert_(_delta < _timeout + 0.5,
                "timeout (%f) is 0.5 seconds more than required (%f)" 
                %(_delta, _timeout))

    def testAcceptTimeout(self):
        "Test accept() timeout()"
        _timeout = 2
        self.__s.settimeout(_timeout)
        self.__s.bind(self.__addr_local)
        self.__s.listen(5)

        _t1 = time.time()
        self.failUnlessRaises(socket.error, self.__s.accept)
        _t2 = time.time()

        _delta = abs(_t1 - _t2)
        self.assert_(_delta < _timeout + 0.5,
                "timeout (%f) is 0.5 seconds more than required (%f)" 
                %(_delta, _timeout))

    def testRecvfromTimeout(self):
        "Test recvfrom() timeout()"
        _timeout = 2
        self.__s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        self.__s.settimeout(_timeout)
        self.__s.bind(self.__addr_local)

        _t1 = time.time()
        self.failUnlessRaises(socket.error, self.__s.recvfrom, 8192)
        _t2 = time.time()

        _delta = abs(_t1 - _t2)
        self.assert_(_delta < _timeout + 0.5,
                "timeout (%f) is 0.5 seconds more than required (%f)" 
                %(_delta, _timeout))

    def testSend(self):
        "Test send() timeout"
        # couldn't figure out how to test it
        pass

    def testSendto(self):
        "Test sendto() timeout"
        # couldn't figure out how to test it
        pass

    def testSendall(self):
        "Test sendall() timeout"
        # couldn't figure out how to test it
        pass


def suite():
    suite = unittest.TestSuite()

    return suite

if __name__ == "__main__":
    unittest.main()