blob: a01be85accf2c6c7a6bf10dd2260e4306d4735d1 [file] [log] [blame]
David Reissc16a8f62007-12-14 23:46:47 +00001#!/usr/bin/env python
2
David Reissea2cba82009-03-30 21:35:00 +00003#
4# Licensed to the Apache Software Foundation (ASF) under one
5# or more contributor license agreements. See the NOTICE file
6# distributed with this work for additional information
7# regarding copyright ownership. The ASF licenses this file
8# to you under the Apache License, Version 2.0 (the
9# "License"); you may not use this file except in compliance
10# with the License. You may obtain a copy of the License at
11#
12# http://www.apache.org/licenses/LICENSE-2.0
13#
14# Unless required by applicable law or agreed to in writing,
15# software distributed under the License is distributed on an
16# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17# KIND, either express or implied. See the License for the
18# specific language governing permissions and limitations
19# under the License.
20#
21
David Reissc16a8f62007-12-14 23:46:47 +000022from thrift.transport import TSocket
David Reissc16a8f62007-12-14 23:46:47 +000023import unittest
24import time
25import socket
26import random
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +090027
David Reissc16a8f62007-12-14 23:46:47 +000028
29class TimeoutTest(unittest.TestCase):
30 def setUp(self):
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +090031 for i in range(50):
David Reissc16a8f62007-12-14 23:46:47 +000032 try:
33 # find a port we can use
34 self.listen_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
35 self.port = random.randint(10000, 30000)
36 self.listen_sock.bind(('localhost', self.port))
37 self.listen_sock.listen(5)
38 break
39 except:
40 if i == 49:
41 raise
42
43 def testConnectTimeout(self):
44 starttime = time.time()
David Reiss0c90f6f2008-02-06 22:18:40 +000045
David Reissc16a8f62007-12-14 23:46:47 +000046 try:
47 leaky = []
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +090048 for i in range(100):
David Reissc16a8f62007-12-14 23:46:47 +000049 socket = TSocket.TSocket('localhost', self.port)
50 socket.setTimeout(10)
51 socket.open()
52 leaky.append(socket)
53 except:
David Reiss1cc0c5e2008-10-17 19:30:35 +000054 self.assert_(time.time() - starttime < 5.0)
David Reissc16a8f62007-12-14 23:46:47 +000055
56 def testWriteTimeout(self):
57 starttime = time.time()
David Reiss0c90f6f2008-02-06 22:18:40 +000058
David Reissc16a8f62007-12-14 23:46:47 +000059 try:
60 socket = TSocket.TSocket('localhost', self.port)
61 socket.setTimeout(10)
62 socket.open()
63 lsock = self.listen_sock.accept()
64 while True:
65 socket.write("hi" * 100)
David Reiss0c90f6f2008-02-06 22:18:40 +000066
David Reissc16a8f62007-12-14 23:46:47 +000067 except:
David Reiss1cc0c5e2008-10-17 19:30:35 +000068 self.assert_(time.time() - starttime < 5.0)
David Reiss0c90f6f2008-02-06 22:18:40 +000069
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +090070if __name__ == '__main__':
71 suite = unittest.TestSuite()
72 loader = unittest.TestLoader()
David Reissc16a8f62007-12-14 23:46:47 +000073
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +090074 suite.addTest(loader.loadTestsFromTestCase(TimeoutTest))
David Reissc16a8f62007-12-14 23:46:47 +000075
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +090076 testRunner = unittest.TextTestRunner(verbosity=2)
77 testRunner.run(suite)