blob: 2f7353fb1658f49bb780ab368ca0e559b5bbe508 [file] [log] [blame]
Gavin McDonald0b75e1a2010-10-28 02:12:01 +00001#!/usr/bin/env python
2
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
22import sys, glob
23sys.path.insert(0, './gen-py')
24sys.path.insert(0, glob.glob('../../lib/py/build/lib.*')[0])
25
26from ThriftTest import ThriftTest
27from ThriftTest.ttypes import *
28from thrift.transport import TTransport
29from thrift.transport import TSocket
30from thrift.protocol import TBinaryProtocol
31import unittest
32import time
33import socket
34import random
35from optparse import OptionParser
36
37class TimeoutTest(unittest.TestCase):
38 def setUp(self):
39 for i in xrange(50):
40 try:
41 # find a port we can use
42 self.listen_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
43 self.port = random.randint(10000, 30000)
44 self.listen_sock.bind(('localhost', self.port))
45 self.listen_sock.listen(5)
46 break
47 except:
48 if i == 49:
49 raise
50
51 def testConnectTimeout(self):
52 starttime = time.time()
53
54 try:
55 leaky = []
56 for i in xrange(100):
57 socket = TSocket.TSocket('localhost', self.port)
58 socket.setTimeout(10)
59 socket.open()
60 leaky.append(socket)
61 except:
62 self.assert_(time.time() - starttime < 5.0)
63
64 def testWriteTimeout(self):
65 starttime = time.time()
66
67 try:
68 socket = TSocket.TSocket('localhost', self.port)
69 socket.setTimeout(10)
70 socket.open()
71 lsock = self.listen_sock.accept()
72 while True:
73 socket.write("hi" * 100)
74
75 except:
76 self.assert_(time.time() - starttime < 5.0)
77
78suite = unittest.TestSuite()
79loader = unittest.TestLoader()
80
81suite.addTest(loader.loadTestsFromTestCase(TimeoutTest))
82
83testRunner = unittest.TextTestRunner(verbosity=2)
84testRunner.run(suite)