44import helper
55import msgpackrpc
66from msgpackrpc import inPy3k
7+ from msgpackrpc import error
78
89
910class TestMessagePackRPC (unittest .TestCase ):
@@ -28,9 +29,6 @@ def _start_server(server):
2829 self ._thread = threading .Thread (target = _start_server , args = (self ._server ,))
2930 self ._thread .start ()
3031
31- import time
32- time .sleep (1 )
33-
3432 self ._client = msgpackrpc .Client (self ._address )
3533 return self ._client ;
3634
@@ -39,17 +37,49 @@ def tearDown(self):
3937 self ._server .stop ();
4038 self ._thread .join ();
4139
42- def test_hello (self ):
40+ def test_call (self ):
4341 client = self .setup_env ();
4442 result = client .call ('hello' )
4543 if inPy3k :
4644 result = result .decode ("utf-8" )
47- self .assertEqual (result , "world" , "hello result is incorrect" )
45+ self .assertEqual (result , "world" , "' hello' result is incorrect" )
4846
49- def test_add (self ):
50- client = self .setup_env ();
5147 result = client .call ('sum' , 1 , 2 )
52- self .assertEqual (result , 3 , "sum result is incorrect" )
48+ self .assertEqual (result , 3 , "'sum' result is incorrect" )
49+
50+ def test_call_async (self ):
51+ client = self .setup_env ();
52+
53+ feture1 = client .call_async ('hello' )
54+ feture2 = client .call_async ('sum' , 1 , 2 )
55+ feture1 .join ()
56+ feture2 .join ()
57+
58+ if inPy3k :
59+ result = feture1 .result .decode ("utf-8" )
60+ else :
61+ result = feture1 .result
62+ self .assertEqual (result , "world" , "'hello' result is incorrect in call_async" )
63+ self .assertEqual (feture2 .result , 3 , "'sum' result is incorrect in call_async" )
64+
65+ def test_notify (self ):
66+ client = self .setup_env ();
67+ result = True
68+ try :
69+ client .notify ('hello' )
70+ client .notify ('sum' , 1 , 2 )
71+ except :
72+ result = False
73+ self .assert_ (result )
74+
75+ def test_unknown_method (self ):
76+ client = self .setup_env ();
77+ self .assertRaises (error .RPCError , lambda : client .call ('unknown' , True ))
78+ try :
79+ client .call ('unknown' , True )
80+ self .assert_ (False )
81+ except error .RPCError as e :
82+ self .assertEqual (e .message , "'unknown' method not found" , "Error message mismatched" )
5383
5484
5585if __name__ == '__main__' :
0 commit comments