blob: b9bdf27eabad5735b2d5af98f33145c25c57c4fe [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 +000022import sys, glob
Roger Meierf4eec7a2011-09-11 18:16:21 +000023from optparse import OptionParser
24parser = OptionParser()
25parser.add_option('--genpydir', type='string', dest='genpydir', default='gen-py')
26options, args = parser.parse_args()
27del sys.argv[1:] # clean up hack so unittest doesn't complain
28sys.path.insert(0, options.genpydir)
David Reissc16a8f62007-12-14 23:46:47 +000029sys.path.insert(0, glob.glob('../../lib/py/build/lib.*')[0])
30
31from ThriftTest import ThriftTest
32from ThriftTest.ttypes import *
33from thrift.transport import TTransport
34from thrift.transport import TSocket
35from thrift.protocol import TBinaryProtocol
36import unittest
37import time
38import socket
39import random
40from optparse import OptionParser
41
42class 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 Reiss0c90f6f2008-02-06 22:18:40 +000058
David Reissc16a8f62007-12-14 23:46:47 +000059 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 Reiss1cc0c5e2008-10-17 19:30:35 +000067 self.assert_(time.time() - starttime < 5.0)
David Reissc16a8f62007-12-14 23:46:47 +000068
69 def testWriteTimeout(self):
70 starttime = time.time()
David Reiss0c90f6f2008-02-06 22:18:40 +000071
David Reissc16a8f62007-12-14 23:46:47 +000072 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 Reiss0c90f6f2008-02-06 22:18:40 +000079
David Reissc16a8f62007-12-14 23:46:47 +000080 except:
David Reiss1cc0c5e2008-10-17 19:30:35 +000081 self.assert_(time.time() - starttime < 5.0)
David Reiss0c90f6f2008-02-06 22:18:40 +000082
David Reissc16a8f62007-12-14 23:46:47 +000083suite = unittest.TestSuite()
84loader = unittest.TestLoader()
85
86suite.addTest(loader.loadTestsFromTestCase(TimeoutTest))
87
88testRunner = unittest.TextTestRunner(verbosity=2)
89testRunner.run(suite)