88
99
1010class TestMessagePackRPC (unittest .TestCase ):
11+
12+ class TestArg :
13+ ''' this class must know completely how to deserialize '''
14+ def __init__ (self , a , b , c ):
15+ self .a = a
16+ self .b = b
17+ self .c = c
18+
19+ def to_msgpack (self ):
20+ return (self .a , self .b , self .c )
21+
22+ def add (self , rhs ):
23+ self .a += rhs .a
24+ self .b -= rhs .b
25+ self .c *= rhs .c
26+ return self
27+
28+ def __eq__ (self , rhs ):
29+ return (self .a == rhs .a and self .b == rhs .b and self .c == rhs .c )
30+
31+ @staticmethod
32+ def from_msgpack (arg ):
33+ return TestMessagePackRPC .TestArg (arg [0 ], arg [1 ], arg [2 ])
34+
1135 class TestServer (object ):
1236 def hello (self ):
1337 return "world"
@@ -18,6 +42,11 @@ def sum(self, x, y):
1842 def nil (self ):
1943 return None
2044
45+ def add_arg (self , arg0 , arg1 ):
46+ lhs = TestMessagePackRPC .TestArg .from_msgpack (arg0 )
47+ rhs = TestMessagePackRPC .TestArg .from_msgpack (arg1 )
48+ return lhs .add (rhs )
49+
2150 def raise_error (self ):
2251 raise Exception ('error' )
2352
@@ -54,6 +83,21 @@ def test_call(self):
5483 self .assertEqual (result2 , 3 , "'sum' result is incorrect" )
5584 self .assertIsNone (result3 , "'nil' result is incorrect" )
5685
86+ def test_call_userdefined_arg (self ):
87+ client = self .setup_env ();
88+
89+ arg = TestMessagePackRPC .TestArg (0 , 1 , 2 )
90+ arg2 = TestMessagePackRPC .TestArg (23 , 3 , - 23 )
91+
92+ result1 = TestMessagePackRPC .TestArg .from_msgpack (client .call ('add_arg' ,arg , arg2 ))
93+ self .assertEqual (result1 , arg .add (arg2 ))
94+
95+ result2 = TestMessagePackRPC .TestArg .from_msgpack (client .call ('add_arg' ,arg2 , arg ))
96+ self .assertEqual (result2 , arg2 .add (arg ))
97+
98+ result3 = TestMessagePackRPC .TestArg .from_msgpack (client .call ('add_arg' ,result1 , result2 ))
99+ self .assertEqual (result3 , result1 .add (result2 ))
100+
57101 def test_call_async (self ):
58102 client = self .setup_env ();
59103
0 commit comments