##// END OF EJS Templates
test websocket-friendly binary message roundtrip...
MinRK -
Show More
@@ -0,0 +1,26 b''
1 """Test serialize/deserialize messages with buffers"""
2
3 import os
4
5 import nose.tools as nt
6
7 from IPython.kernel.zmq.session import Session
8 from ..base.zmqhandlers import (
9 serialize_binary_message,
10 deserialize_binary_message,
11 )
12
13 def test_serialize_binary():
14 s = Session()
15 msg = s.msg('data_pub', content={'a': 'b'})
16 msg['buffers'] = [ os.urandom(3) for i in range(3) ]
17 bmsg = serialize_binary_message(msg)
18 nt.assert_is_instance(bmsg, bytes)
19
20 def test_deserialize_binary():
21 s = Session()
22 msg = s.msg('data_pub', content={'a': 'b'})
23 msg['buffers'] = [ os.urandom(2) for i in range(3) ]
24 bmsg = serialize_binary_message(msg)
25 msg2 = deserialize_binary_message(bmsg)
26 nt.assert_equal(msg2, msg)
@@ -23,7 +23,7 b' from tornado import web'
23 from tornado import websocket
23 from tornado import websocket
24
24
25 from IPython.kernel.zmq.session import Session
25 from IPython.kernel.zmq.session import Session
26 from IPython.utils.jsonutil import date_default
26 from IPython.utils.jsonutil import date_default, extract_dates
27 from IPython.utils.py3compat import PY3, cast_unicode
27 from IPython.utils.py3compat import PY3, cast_unicode
28
28
29 from .handlers import IPythonHandler
29 from .handlers import IPythonHandler
@@ -45,7 +45,9 b' def serialize_binary_message(msg):'
45 The message serialized to bytes.
45 The message serialized to bytes.
46
46
47 """
47 """
48 buffers = msg.pop('buffers')
48 # don't modify msg or buffer list in-place
49 msg = msg.copy()
50 buffers = list(msg.pop('buffers'))
49 bmsg = json.dumps(msg, default=date_default).encode('utf8')
51 bmsg = json.dumps(msg, default=date_default).encode('utf8')
50 buffers.insert(0, bmsg)
52 buffers.insert(0, bmsg)
51 nbufs = len(buffers)
53 nbufs = len(buffers)
@@ -72,13 +74,15 b' def deserialize_binary_message(bmsg):'
72
74
73 message dictionary
75 message dictionary
74 """
76 """
75 nbufs = struct.unpack('i', bmsg[:4])[0]
77 nbufs = struct.unpack('!i', bmsg[:4])[0]
76 offsets = list(struct.unpack('!' + 'i' * nbufs, bmsg[4:4*(nbufs+1)]))
78 offsets = list(struct.unpack('!' + 'i' * nbufs, bmsg[4:4*(nbufs+1)]))
77 offsets.append(None)
79 offsets.append(None)
78 bufs = []
80 bufs = []
79 for start, stop in zip(offsets[:-1], offsets[1:]):
81 for start, stop in zip(offsets[:-1], offsets[1:]):
80 bufs.append(bmsg[start:stop])
82 bufs.append(bmsg[start:stop])
81 msg = json.loads(bufs[0])
83 msg = json.loads(bufs[0].decode('utf8'))
84 msg['header'] = extract_dates(msg['header'])
85 msg['parent_header'] = extract_dates(msg['parent_header'])
82 msg['buffers'] = bufs[1:]
86 msg['buffers'] = bufs[1:]
83 return msg
87 return msg
84
88
@@ -139,14 +143,6 b' class ZMQStreamHandler(websocket.WebSocketHandler):'
139 """
143 """
140 idents, msg_list = self.session.feed_identities(msg_list)
144 idents, msg_list = self.session.feed_identities(msg_list)
141 msg = self.session.deserialize(msg_list)
145 msg = self.session.deserialize(msg_list)
142 try:
143 msg['header'].pop('date')
144 except KeyError:
145 pass
146 try:
147 msg['parent_header'].pop('date')
148 except KeyError:
149 pass
150 if msg['buffers']:
146 if msg['buffers']:
151 buf = serialize_binary_message(msg)
147 buf = serialize_binary_message(msg)
152 return buf
148 return buf
General Comments 0
You need to be logged in to leave comments. Login now