|
| 1 | +from time import sleep |
1 | 2 | import threading |
2 | 3 | try: |
3 | 4 | import unittest2 as unittest |
|
10 | 11 |
|
11 | 12 |
|
12 | 13 | class TestMessagePackRPC(unittest.TestCase): |
| 14 | + ENABLE_TIMEOUT_TEST = False |
13 | 15 |
|
14 | 16 | class TestArg: |
15 | 17 | ''' this class must know completely how to deserialize ''' |
@@ -52,6 +54,9 @@ def add_arg(self, arg0, arg1): |
52 | 54 | def raise_error(self): |
53 | 55 | raise Exception('error') |
54 | 56 |
|
| 57 | + def long_exec(self): |
| 58 | + sleep(3) |
| 59 | + return 'finish!' |
55 | 60 |
|
56 | 61 | def setUp(self): |
57 | 62 | self._address = msgpackrpc.Address('localhost', helper.unused_port()) |
@@ -141,5 +146,30 @@ def test_unknown_method(self): |
141 | 146 | message = e.args[0] |
142 | 147 | self.assertEqual(message, "'unknown' method not found", "Error message mismatched") |
143 | 148 |
|
| 149 | + def test_connect_failed(self): |
| 150 | + client = self.setup_env(); |
| 151 | + client = msgpackrpc.Client(msgpackrpc.Address('localhost', self._address.port - 10), unpack_encoding='utf-8') |
| 152 | + self.assertRaises(error.TransportError, lambda: client.call('hello')) |
| 153 | + |
| 154 | + def test_timeout(self): |
| 155 | + client = self.setup_env(); |
| 156 | + |
| 157 | + if self.__class__.ENABLE_TIMEOUT_TEST: |
| 158 | + self.assertEqual(client.call('long_exec'), 'finish!', "'long_exec' result is incorrect") |
| 159 | + |
| 160 | + client = msgpackrpc.Client(self._address, timeout=1, unpack_encoding='utf-8') |
| 161 | + self.assertRaises(error.TimeoutError, lambda: client.call('long_exec')) |
| 162 | + else: |
| 163 | + print("Skip test_timeout") |
| 164 | + |
| 165 | + |
144 | 166 | if __name__ == '__main__': |
| 167 | + import sys |
| 168 | + |
| 169 | + try: |
| 170 | + sys.argv.remove('--timeout-test') |
| 171 | + TestMessagePackRPC.ENABLE_TIMEOUT_TEST = True |
| 172 | + except: |
| 173 | + pass |
| 174 | + |
145 | 175 | unittest.main() |
0 commit comments