blob: b693d182419b5f7d8911fd406f97d1a00d535711 [file] [log] [blame]
David Reissc16a8f62007-12-14 23:46:47 +00001#!/usr/bin/env python
2
3import sys, glob
4sys.path.insert(0, './gen-py')
5sys.path.insert(0, glob.glob('../../lib/py/build/lib.*')[0])
6
7from ThriftTest import ThriftTest
8from ThriftTest.ttypes import *
9from thrift.transport import TTransport
10from thrift.transport import TSocket
11from thrift.protocol import TBinaryProtocol
12import unittest
13import time
14import socket
15import random
16from optparse import OptionParser
17
18class 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 Reiss0c90f6f2008-02-06 22:18:40 +000034
David Reissc16a8f62007-12-14 23:46:47 +000035 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 Reiss0c90f6f2008-02-06 22:18:40 +000047
David Reissc16a8f62007-12-14 23:46:47 +000048 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 Reiss0c90f6f2008-02-06 22:18:40 +000055
David Reissc16a8f62007-12-14 23:46:47 +000056 except:
57 assert time.time() - starttime < 5.0
David Reiss0c90f6f2008-02-06 22:18:40 +000058
David Reissc16a8f62007-12-14 23:46:47 +000059suite = unittest.TestSuite()
60loader = unittest.TestLoader()
61
62suite.addTest(loader.loadTestsFromTestCase(TimeoutTest))
63
64testRunner = unittest.TextTestRunner(verbosity=2)
65testRunner.run(suite)