Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
30 ashish 1
#!/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
 
22
import sys, glob
23
sys.path.insert(0, './gen-py')
24
sys.path.insert(0, glob.glob('../../lib/py/build/lib.*')[0])
25
 
26
from ThriftTest import ThriftTest
27
from ThriftTest.ttypes import *
28
from thrift.transport import TTransport
29
from thrift.transport import TSocket
30
from thrift.protocol import TBinaryProtocol
31
import unittest
32
import time
33
import socket
34
import random
35
from optparse import OptionParser
36
 
37
class 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
 
78
suite = unittest.TestSuite()
79
loader = unittest.TestLoader()
80
 
81
suite.addTest(loader.loadTestsFromTestCase(TimeoutTest))
82
 
83
testRunner = unittest.TextTestRunner(verbosity=2)
84
testRunner.run(suite)