Skip to content

Commit c4dd3bf

Browse files
committed
Fixed a bug can not get the return value when None is returned from the server.
1 parent 9e074ea commit c4dd3bf

2 files changed

Lines changed: 47 additions & 18 deletions

File tree

msgpackrpc/future.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,37 @@ def __init__(self, loop, timeout, callback=None):
1111
self._loop = loop
1212
self._error = None
1313
self._result = None
14+
self._set_flag = False
1415
self._timeout = timeout
1516
self._callback = callback
1617
self._error_handler = None
1718
self._result_handler = None
1819

1920
def join(self):
20-
while (self._error is None and self._result is None):
21+
while (not self._set_flag):
2122
self._loop.start()
2223

2324
def get(self):
2425
self.join()
26+
27+
assert self._set_flag == True
28+
if not self._set_flag:
29+
# TODO: should be designed error !!
30+
raise error.RPCError(128)
31+
2532
if self._result is not None:
2633
if self._result_handler is None:
2734
return self._result
2835
else:
2936
self._result_handler(self._result)
30-
31-
if self._error_handler is not None:
32-
self._error_handler(self._error)
33-
raise error.RPCError(self._error)
37+
else:
38+
if self._error is not None:
39+
if self._error_handler is not None:
40+
self._error_handler(self._error)
41+
else:
42+
raise error.RPCError(self._error)
43+
else:
44+
return self._result
3445

3546
def set(self, error=None, result=None):
3647
self._error = error
@@ -44,16 +55,16 @@ def result(self):
4455
return self._result
4556

4657
def set_result(self, result):
47-
assert result is not None
4858
self.set(result=result)
59+
self._set_flag = True
4960

5061
@property
5162
def error(self):
5263
return self._error
5364

5465
def set_error(self, error):
55-
assert error is not None
5666
self.set(error=error)
67+
self._set_flag = True
5768

5869
def attach_callback(self, callback):
5970
self._callback = callback

test/test_msgpackrpc.py

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ def hello(self):
1515
def sum(self, x, y):
1616
return x + y
1717

18+
def nil(self):
19+
return None
20+
21+
def raise_error(self):
22+
raise Exception('error')
23+
1824

1925
def setUp(self):
2026
self._address = msgpackrpc.Address('localhost', helper.unused_port())
@@ -39,33 +45,46 @@ def tearDown(self):
3945

4046
def test_call(self):
4147
client = self.setup_env();
42-
result = client.call('hello')
43-
self.assertEqual(result, "world", "'hello' result is incorrect")
4448

45-
result = client.call('sum', 1, 2)
46-
self.assertEqual(result, 3, "'sum' result is incorrect")
49+
result1 = client.call('hello')
50+
result2 = client.call('sum', 1, 2)
51+
result3 = client.call('nil')
52+
53+
self.assertEqual(result1, "world", "'hello' result is incorrect")
54+
self.assertEqual(result2, 3, "'sum' result is incorrect")
55+
self.assertIsNone(result3, "'nil' result is incorrect")
4756

4857
def test_call_async(self):
4958
client = self.setup_env();
5059

51-
feture1 = client.call_async('hello')
52-
feture2 = client.call_async('sum', 1, 2)
53-
feture1.join()
54-
feture2.join()
60+
future1 = client.call_async('hello')
61+
future2 = client.call_async('sum', 1, 2)
62+
future3 = client.call_async('nil')
63+
future1.join()
64+
future2.join()
65+
future3.join()
5566

56-
self.assertEqual(feture1.result, "world", "'hello' result is incorrect in call_async")
57-
self.assertEqual(feture2.result, 3, "'sum' result is incorrect in call_async")
67+
self.assertEqual(future1.result, "world", "'hello' result is incorrect in call_async")
68+
self.assertEqual(future2.result, 3, "'sum' result is incorrect in call_async")
69+
self.assertIsNone(future3.result, "'nil' result is incorrect in call_async")
5870

5971
def test_notify(self):
6072
client = self.setup_env();
73+
6174
result = True
6275
try:
6376
client.notify('hello')
6477
client.notify('sum', 1, 2)
78+
client.notify('nil')
6579
except:
6680
result = False
81+
6782
self.assertTrue(result)
6883

84+
def test_raise_error(self):
85+
client = self.setup_env();
86+
self.assertRaises(error.RPCError, lambda: client.call('raise_error'))
87+
6988
def test_unknown_method(self):
7089
client = self.setup_env();
7190
self.assertRaises(error.RPCError, lambda: client.call('unknown', True))
@@ -76,6 +95,5 @@ def test_unknown_method(self):
7695
message = e.args[0]
7796
self.assertEqual(message, "'unknown' method not found", "Error message mismatched")
7897

79-
8098
if __name__ == '__main__':
8199
unittest.main()

0 commit comments

Comments
 (0)