blob: 3cec9aa4c298c5fa21b3baf862c3043c376e07e5 [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
Carel Combrinkf8ce26c2025-11-12 14:24:55 +020027import sys
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +090028
David Reissc16a8f62007-12-14 23:46:47 +000029
30class TimeoutTest(unittest.TestCase):
31 def setUp(self):
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +090032 for i in range(50):
David Reissc16a8f62007-12-14 23:46:47 +000033 try:
34 # find a port we can use
35 self.listen_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
36 self.port = random.randint(10000, 30000)
37 self.listen_sock.bind(('localhost', self.port))
38 self.listen_sock.listen(5)
39 break
James E. King, III350fe752017-10-25 09:57:18 -040040 except Exception:
David Reissc16a8f62007-12-14 23:46:47 +000041 if i == 49:
42 raise
43
44 def testConnectTimeout(self):
45 starttime = time.time()
David Reiss0c90f6f2008-02-06 22:18:40 +000046
David Reissc16a8f62007-12-14 23:46:47 +000047 try:
48 leaky = []
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +090049 for i in range(100):
David Reissc16a8f62007-12-14 23:46:47 +000050 socket = TSocket.TSocket('localhost', self.port)
51 socket.setTimeout(10)
52 socket.open()
53 leaky.append(socket)
James E. King, III350fe752017-10-25 09:57:18 -040054 except Exception:
Carel Combrinkf8ce26c2025-11-12 14:24:55 +020055 self.assertTrue(time.time() - starttime < 5.0)
David Reissc16a8f62007-12-14 23:46:47 +000056
57 def testWriteTimeout(self):
58 starttime = time.time()
David Reiss0c90f6f2008-02-06 22:18:40 +000059
David Reissc16a8f62007-12-14 23:46:47 +000060 try:
61 socket = TSocket.TSocket('localhost', self.port)
62 socket.setTimeout(10)
63 socket.open()
64 lsock = self.listen_sock.accept()
65 while True:
Nobuaki Sukegawa47084092016-02-04 18:26:10 +090066 lsock.write("hi" * 100)
David Reiss0c90f6f2008-02-06 22:18:40 +000067
James E. King, III350fe752017-10-25 09:57:18 -040068 except Exception:
Carel Combrinkf8ce26c2025-11-12 14:24:55 +020069 self.assertTrue(time.time() - starttime < 5.0)
David Reiss0c90f6f2008-02-06 22:18:40 +000070
James E. King, III0ad20bd2017-09-30 15:44:16 -070071
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +090072if __name__ == '__main__':
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090073 suite = unittest.TestSuite()
74 loader = unittest.TestLoader()
David Reissc16a8f62007-12-14 23:46:47 +000075
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090076 suite.addTest(loader.loadTestsFromTestCase(TimeoutTest))
David Reissc16a8f62007-12-14 23:46:47 +000077
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090078 testRunner = unittest.TextTestRunner(verbosity=2)
Carel Combrinkf8ce26c2025-11-12 14:24:55 +020079 result = testRunner.run(suite)
80
81 # Exit with non-zero code if tests failed
82 if result.failures or result.errors:
83 sys.exit(1)
84 else:
85 sys.exit(0)