David Reiss | c16a8f6 | 2007-12-14 23:46:47 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | import sys, glob |
| 4 | sys.path.insert(0, './gen-py') |
| 5 | sys.path.insert(0, glob.glob('../../lib/py/build/lib.*')[0]) |
| 6 | |
| 7 | from ThriftTest import ThriftTest |
| 8 | from ThriftTest.ttypes import * |
| 9 | from thrift.transport import TTransport |
| 10 | from thrift.transport import TSocket |
| 11 | from thrift.protocol import TBinaryProtocol |
| 12 | import unittest |
| 13 | import time |
| 14 | import socket |
| 15 | import random |
| 16 | from optparse import OptionParser |
| 17 | |
| 18 | class TimeoutTest(unittest.TestCase): |
| 19 | def setUp(self): |
| 20 | for i in xrange(50): |
| 21 | try: |
| 22 | # find a port we can use |
| 23 | self.listen_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 24 | self.port = random.randint(10000, 30000) |
| 25 | self.listen_sock.bind(('localhost', self.port)) |
| 26 | self.listen_sock.listen(5) |
| 27 | break |
| 28 | except: |
| 29 | if i == 49: |
| 30 | raise |
| 31 | |
| 32 | def testConnectTimeout(self): |
| 33 | starttime = time.time() |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 34 | |
David Reiss | c16a8f6 | 2007-12-14 23:46:47 +0000 | [diff] [blame] | 35 | try: |
| 36 | leaky = [] |
| 37 | for i in xrange(100): |
| 38 | socket = TSocket.TSocket('localhost', self.port) |
| 39 | socket.setTimeout(10) |
| 40 | socket.open() |
| 41 | leaky.append(socket) |
| 42 | except: |
| 43 | assert time.time() - starttime < 5.0 |
| 44 | |
| 45 | def testWriteTimeout(self): |
| 46 | starttime = time.time() |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 47 | |
David Reiss | c16a8f6 | 2007-12-14 23:46:47 +0000 | [diff] [blame] | 48 | try: |
| 49 | socket = TSocket.TSocket('localhost', self.port) |
| 50 | socket.setTimeout(10) |
| 51 | socket.open() |
| 52 | lsock = self.listen_sock.accept() |
| 53 | while True: |
| 54 | socket.write("hi" * 100) |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 55 | |
David Reiss | c16a8f6 | 2007-12-14 23:46:47 +0000 | [diff] [blame] | 56 | except: |
| 57 | assert time.time() - starttime < 5.0 |
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 | suite = unittest.TestSuite() |
| 60 | loader = unittest.TestLoader() |
| 61 | |
| 62 | suite.addTest(loader.loadTestsFromTestCase(TimeoutTest)) |
| 63 | |
| 64 | testRunner = unittest.TextTestRunner(verbosity=2) |
| 65 | testRunner.run(suite) |