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 | import sys, glob |
Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame] | 23 | from optparse import OptionParser |
| 24 | parser = OptionParser() |
| 25 | parser.add_option('--genpydir', type='string', dest='genpydir', default='gen-py') |
| 26 | options, args = parser.parse_args() |
| 27 | del sys.argv[1:] # clean up hack so unittest doesn't complain |
| 28 | sys.path.insert(0, options.genpydir) |
David Reiss | c16a8f6 | 2007-12-14 23:46:47 +0000 | [diff] [blame] | 29 | sys.path.insert(0, glob.glob('../../lib/py/build/lib.*')[0]) |
| 30 | |
| 31 | from ThriftTest import ThriftTest |
| 32 | from ThriftTest.ttypes import * |
| 33 | from thrift.transport import TTransport |
| 34 | from thrift.transport import TSocket |
| 35 | from thrift.protocol import TBinaryProtocol |
| 36 | import unittest |
| 37 | import time |
| 38 | import socket |
| 39 | import random |
| 40 | from optparse import OptionParser |
| 41 | |
| 42 | class TimeoutTest(unittest.TestCase): |
| 43 | def setUp(self): |
| 44 | for i in xrange(50): |
| 45 | try: |
| 46 | # find a port we can use |
| 47 | self.listen_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 48 | self.port = random.randint(10000, 30000) |
| 49 | self.listen_sock.bind(('localhost', self.port)) |
| 50 | self.listen_sock.listen(5) |
| 51 | break |
| 52 | except: |
| 53 | if i == 49: |
| 54 | raise |
| 55 | |
| 56 | def testConnectTimeout(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 | leaky = [] |
| 61 | for i in xrange(100): |
| 62 | socket = TSocket.TSocket('localhost', self.port) |
| 63 | socket.setTimeout(10) |
| 64 | socket.open() |
| 65 | leaky.append(socket) |
| 66 | except: |
David Reiss | 1cc0c5e | 2008-10-17 19:30:35 +0000 | [diff] [blame] | 67 | self.assert_(time.time() - starttime < 5.0) |
David Reiss | c16a8f6 | 2007-12-14 23:46:47 +0000 | [diff] [blame] | 68 | |
| 69 | def testWriteTimeout(self): |
| 70 | starttime = time.time() |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 71 | |
David Reiss | c16a8f6 | 2007-12-14 23:46:47 +0000 | [diff] [blame] | 72 | try: |
| 73 | socket = TSocket.TSocket('localhost', self.port) |
| 74 | socket.setTimeout(10) |
| 75 | socket.open() |
| 76 | lsock = self.listen_sock.accept() |
| 77 | while True: |
| 78 | socket.write("hi" * 100) |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 79 | |
David Reiss | c16a8f6 | 2007-12-14 23:46:47 +0000 | [diff] [blame] | 80 | except: |
David Reiss | 1cc0c5e | 2008-10-17 19:30:35 +0000 | [diff] [blame] | 81 | self.assert_(time.time() - starttime < 5.0) |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 82 | |
David Reiss | c16a8f6 | 2007-12-14 23:46:47 +0000 | [diff] [blame] | 83 | suite = unittest.TestSuite() |
| 84 | loader = unittest.TestLoader() |
| 85 | |
| 86 | suite.addTest(loader.loadTestsFromTestCase(TimeoutTest)) |
| 87 | |
| 88 | testRunner = unittest.TextTestRunner(verbosity=2) |
| 89 | testRunner.run(suite) |