Skip to content

Commit 6f44716

Browse files
committed
Merge branch 'master' of github.com:msgpack/msgpack-rpc-python
2 parents 0ac60ea + b0099e3 commit 6f44716

2 files changed

Lines changed: 45 additions & 1 deletion

File tree

msgpackrpc/transport/tcp.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
class BaseSocket(object):
1212
def __init__(self, stream, encodings):
1313
self._stream = stream
14-
self._packer = msgpack.Packer(encoding=encodings[0])
14+
self._packer = msgpack.Packer(encoding=encodings[0], default=lambda x: x.to_msgpack())
1515
self._unpacker = msgpack.Unpacker(encoding=encodings[1])
1616

1717
def close(self):

test/test_msgpackrpc.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,30 @@
77

88

99
class TestMessagePackRPC(unittest.TestCase):
10+
11+
class TestArg:
12+
''' this class must know completely how to deserialize '''
13+
def __init__(self, a, b, c):
14+
self.a = a
15+
self.b = b
16+
self.c = c
17+
18+
def to_msgpack(self):
19+
return (self.a, self.b, self.c)
20+
21+
def add(self, rhs):
22+
self.a += rhs.a
23+
self.b -= rhs.b
24+
self.c *= rhs.c
25+
return self
26+
27+
def __eq__(self, rhs):
28+
return (self.a == rhs.a and self.b == rhs.b and self.c == rhs.c)
29+
30+
@staticmethod
31+
def from_msgpack(arg):
32+
return TestMessagePackRPC.TestArg(arg[0], arg[1], arg[2])
33+
1034
class TestServer(object):
1135
def hello(self):
1236
return "world"
@@ -17,6 +41,11 @@ def sum(self, x, y):
1741
def nil(self):
1842
return None
1943

44+
def add_arg(self, arg0, arg1):
45+
lhs = TestMessagePackRPC.TestArg.from_msgpack(arg0)
46+
rhs = TestMessagePackRPC.TestArg.from_msgpack(arg1)
47+
return lhs.add(rhs)
48+
2049
def raise_error(self):
2150
raise Exception('error')
2251

@@ -53,6 +82,21 @@ def test_call(self):
5382
self.assertEqual(result2, 3, "'sum' result is incorrect")
5483
self.assertIsNone(result3, "'nil' result is incorrect")
5584

85+
def test_call_userdefined_arg(self):
86+
client = self.setup_env();
87+
88+
arg = TestMessagePackRPC.TestArg(0, 1, 2)
89+
arg2 = TestMessagePackRPC.TestArg(23, 3, -23)
90+
91+
result1 = TestMessagePackRPC.TestArg.from_msgpack(client.call('add_arg', arg, arg2))
92+
self.assertEqual(result1, arg.add(arg2))
93+
94+
result2 = TestMessagePackRPC.TestArg.from_msgpack(client.call('add_arg', arg2, arg))
95+
self.assertEqual(result2, arg2.add(arg))
96+
97+
result3 = TestMessagePackRPC.TestArg.from_msgpack(client.call('add_arg', result1, result2))
98+
self.assertEqual(result3, result1.add(result2))
99+
56100
def test_call_async(self):
57101
client = self.setup_env();
58102

0 commit comments

Comments
 (0)