David Reiss | c16a8f6 | 2007-12-14 23:46:47 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 3 | # |
| 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 Reiss | c16a8f6 | 2007-12-14 23:46:47 +0000 | [diff] [blame] | 22 | from thrift.transport import TSocket |
David Reiss | c16a8f6 | 2007-12-14 23:46:47 +0000 | [diff] [blame] | 23 | import unittest |
| 24 | import time |
| 25 | import socket |
| 26 | import random |
Nobuaki Sukegawa | cacce2f | 2015-11-08 23:43:55 +0900 | [diff] [blame] | 27 | |
David Reiss | c16a8f6 | 2007-12-14 23:46:47 +0000 | [diff] [blame] | 28 | |
| 29 | class TimeoutTest(unittest.TestCase): |
| 30 | def setUp(self): |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 31 | for i in range(50): |
David Reiss | c16a8f6 | 2007-12-14 23:46:47 +0000 | [diff] [blame] | 32 | 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 |
James E. King, III | 350fe75 | 2017-10-25 09:57:18 -0400 | [diff] [blame] | 39 | except Exception: |
David Reiss | c16a8f6 | 2007-12-14 23:46:47 +0000 | [diff] [blame] | 40 | if i == 49: |
| 41 | raise |
| 42 | |
| 43 | def testConnectTimeout(self): |
| 44 | starttime = time.time() |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 45 | |
David Reiss | c16a8f6 | 2007-12-14 23:46:47 +0000 | [diff] [blame] | 46 | try: |
| 47 | leaky = [] |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 48 | for i in range(100): |
David Reiss | c16a8f6 | 2007-12-14 23:46:47 +0000 | [diff] [blame] | 49 | socket = TSocket.TSocket('localhost', self.port) |
| 50 | socket.setTimeout(10) |
| 51 | socket.open() |
| 52 | leaky.append(socket) |
James E. King, III | 350fe75 | 2017-10-25 09:57:18 -0400 | [diff] [blame] | 53 | except Exception: |
David Reiss | 1cc0c5e | 2008-10-17 19:30:35 +0000 | [diff] [blame] | 54 | self.assert_(time.time() - starttime < 5.0) |
David Reiss | c16a8f6 | 2007-12-14 23:46:47 +0000 | [diff] [blame] | 55 | |
| 56 | def testWriteTimeout(self): |
| 57 | starttime = time.time() |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 58 | |
David Reiss | c16a8f6 | 2007-12-14 23:46:47 +0000 | [diff] [blame] | 59 | try: |
| 60 | socket = TSocket.TSocket('localhost', self.port) |
| 61 | socket.setTimeout(10) |
| 62 | socket.open() |
| 63 | lsock = self.listen_sock.accept() |
| 64 | while True: |
Nobuaki Sukegawa | 4708409 | 2016-02-04 18:26:10 +0900 | [diff] [blame] | 65 | lsock.write("hi" * 100) |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 66 | |
James E. King, III | 350fe75 | 2017-10-25 09:57:18 -0400 | [diff] [blame] | 67 | except Exception: |
David Reiss | 1cc0c5e | 2008-10-17 19:30:35 +0000 | [diff] [blame] | 68 | self.assert_(time.time() - starttime < 5.0) |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 69 | |
James E. King, III | 0ad20bd | 2017-09-30 15:44:16 -0700 | [diff] [blame] | 70 | |
Nobuaki Sukegawa | cacce2f | 2015-11-08 23:43:55 +0900 | [diff] [blame] | 71 | if __name__ == '__main__': |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 72 | suite = unittest.TestSuite() |
| 73 | loader = unittest.TestLoader() |
David Reiss | c16a8f6 | 2007-12-14 23:46:47 +0000 | [diff] [blame] | 74 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 75 | suite.addTest(loader.loadTestsFromTestCase(TimeoutTest)) |
David Reiss | c16a8f6 | 2007-12-14 23:46:47 +0000 | [diff] [blame] | 76 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 77 | testRunner = unittest.TextTestRunner(verbosity=2) |
| 78 | testRunner.run(suite) |