Show More
@@ -1,271 +1,289 b'' | |||
|
1 | 1 | """test building messages with streamsession""" |
|
2 | 2 | |
|
3 | 3 | #------------------------------------------------------------------------------- |
|
4 | 4 | # Copyright (C) 2011 The IPython Development Team |
|
5 | 5 | # |
|
6 | 6 | # Distributed under the terms of the BSD License. The full license is in |
|
7 | 7 | # the file COPYING, distributed as part of this software. |
|
8 | 8 | #------------------------------------------------------------------------------- |
|
9 | 9 | |
|
10 | 10 | #------------------------------------------------------------------------------- |
|
11 | 11 | # Imports |
|
12 | 12 | #------------------------------------------------------------------------------- |
|
13 | 13 | |
|
14 | 14 | import os |
|
15 | 15 | import uuid |
|
16 | from datetime import datetime | |
|
17 | ||
|
16 | 18 | import zmq |
|
17 | 19 | |
|
18 | 20 | from zmq.tests import BaseZMQTestCase |
|
19 | 21 | from zmq.eventloop.zmqstream import ZMQStream |
|
20 | 22 | |
|
21 | 23 | from IPython.kernel.zmq import session as ss |
|
22 | 24 | |
|
25 | from IPython.testing.decorators import skipif, module_not_available | |
|
26 | from IPython.utils.py3compat import string_types | |
|
27 | from IPython.utils import jsonutil | |
|
28 | ||
|
23 | 29 | def _bad_packer(obj): |
|
24 | 30 | raise TypeError("I don't work") |
|
25 | 31 | |
|
26 | 32 | def _bad_unpacker(bytes): |
|
27 | 33 | raise TypeError("I don't work either") |
|
28 | 34 | |
|
29 | 35 | class SessionTestCase(BaseZMQTestCase): |
|
30 | 36 | |
|
31 | 37 | def setUp(self): |
|
32 | 38 | BaseZMQTestCase.setUp(self) |
|
33 | 39 | self.session = ss.Session() |
|
34 | 40 | |
|
35 | 41 | |
|
36 | 42 | class TestSession(SessionTestCase): |
|
37 | 43 | |
|
38 | 44 | def test_msg(self): |
|
39 | 45 | """message format""" |
|
40 | 46 | msg = self.session.msg('execute') |
|
41 | 47 | thekeys = set('header parent_header metadata content msg_type msg_id'.split()) |
|
42 | 48 | s = set(msg.keys()) |
|
43 | 49 | self.assertEqual(s, thekeys) |
|
44 | 50 | self.assertTrue(isinstance(msg['content'],dict)) |
|
45 | 51 | self.assertTrue(isinstance(msg['metadata'],dict)) |
|
46 | 52 | self.assertTrue(isinstance(msg['header'],dict)) |
|
47 | 53 | self.assertTrue(isinstance(msg['parent_header'],dict)) |
|
48 | 54 | self.assertTrue(isinstance(msg['msg_id'],str)) |
|
49 | 55 | self.assertTrue(isinstance(msg['msg_type'],str)) |
|
50 | 56 | self.assertEqual(msg['header']['msg_type'], 'execute') |
|
51 | 57 | self.assertEqual(msg['msg_type'], 'execute') |
|
52 | 58 | |
|
53 | 59 | def test_serialize(self): |
|
54 | 60 | msg = self.session.msg('execute', content=dict(a=10, b=1.1)) |
|
55 | 61 | msg_list = self.session.serialize(msg, ident=b'foo') |
|
56 | 62 | ident, msg_list = self.session.feed_identities(msg_list) |
|
57 | 63 | new_msg = self.session.unserialize(msg_list) |
|
58 | 64 | self.assertEqual(ident[0], b'foo') |
|
59 | 65 | self.assertEqual(new_msg['msg_id'],msg['msg_id']) |
|
60 | 66 | self.assertEqual(new_msg['msg_type'],msg['msg_type']) |
|
61 | 67 | self.assertEqual(new_msg['header'],msg['header']) |
|
62 | 68 | self.assertEqual(new_msg['content'],msg['content']) |
|
63 | 69 | self.assertEqual(new_msg['parent_header'],msg['parent_header']) |
|
64 | 70 | self.assertEqual(new_msg['metadata'],msg['metadata']) |
|
65 | 71 | # ensure floats don't come out as Decimal: |
|
66 | 72 | self.assertEqual(type(new_msg['content']['b']),type(new_msg['content']['b'])) |
|
67 | 73 | |
|
68 | 74 | def test_send(self): |
|
69 | 75 | ctx = zmq.Context.instance() |
|
70 | 76 | A = ctx.socket(zmq.PAIR) |
|
71 | 77 | B = ctx.socket(zmq.PAIR) |
|
72 | 78 | A.bind("inproc://test") |
|
73 | 79 | B.connect("inproc://test") |
|
74 | 80 | |
|
75 | 81 | msg = self.session.msg('execute', content=dict(a=10)) |
|
76 | 82 | self.session.send(A, msg, ident=b'foo', buffers=[b'bar']) |
|
77 | 83 | |
|
78 | 84 | ident, msg_list = self.session.feed_identities(B.recv_multipart()) |
|
79 | 85 | new_msg = self.session.unserialize(msg_list) |
|
80 | 86 | self.assertEqual(ident[0], b'foo') |
|
81 | 87 | self.assertEqual(new_msg['msg_id'],msg['msg_id']) |
|
82 | 88 | self.assertEqual(new_msg['msg_type'],msg['msg_type']) |
|
83 | 89 | self.assertEqual(new_msg['header'],msg['header']) |
|
84 | 90 | self.assertEqual(new_msg['content'],msg['content']) |
|
85 | 91 | self.assertEqual(new_msg['parent_header'],msg['parent_header']) |
|
86 | 92 | self.assertEqual(new_msg['metadata'],msg['metadata']) |
|
87 | 93 | self.assertEqual(new_msg['buffers'],[b'bar']) |
|
88 | 94 | |
|
89 | 95 | content = msg['content'] |
|
90 | 96 | header = msg['header'] |
|
91 | 97 | parent = msg['parent_header'] |
|
92 | 98 | metadata = msg['metadata'] |
|
93 | 99 | msg_type = header['msg_type'] |
|
94 | 100 | self.session.send(A, None, content=content, parent=parent, |
|
95 | 101 | header=header, metadata=metadata, ident=b'foo', buffers=[b'bar']) |
|
96 | 102 | ident, msg_list = self.session.feed_identities(B.recv_multipart()) |
|
97 | 103 | new_msg = self.session.unserialize(msg_list) |
|
98 | 104 | self.assertEqual(ident[0], b'foo') |
|
99 | 105 | self.assertEqual(new_msg['msg_id'],msg['msg_id']) |
|
100 | 106 | self.assertEqual(new_msg['msg_type'],msg['msg_type']) |
|
101 | 107 | self.assertEqual(new_msg['header'],msg['header']) |
|
102 | 108 | self.assertEqual(new_msg['content'],msg['content']) |
|
103 | 109 | self.assertEqual(new_msg['metadata'],msg['metadata']) |
|
104 | 110 | self.assertEqual(new_msg['parent_header'],msg['parent_header']) |
|
105 | 111 | self.assertEqual(new_msg['buffers'],[b'bar']) |
|
106 | 112 | |
|
107 | 113 | self.session.send(A, msg, ident=b'foo', buffers=[b'bar']) |
|
108 | 114 | ident, new_msg = self.session.recv(B) |
|
109 | 115 | self.assertEqual(ident[0], b'foo') |
|
110 | 116 | self.assertEqual(new_msg['msg_id'],msg['msg_id']) |
|
111 | 117 | self.assertEqual(new_msg['msg_type'],msg['msg_type']) |
|
112 | 118 | self.assertEqual(new_msg['header'],msg['header']) |
|
113 | 119 | self.assertEqual(new_msg['content'],msg['content']) |
|
114 | 120 | self.assertEqual(new_msg['metadata'],msg['metadata']) |
|
115 | 121 | self.assertEqual(new_msg['parent_header'],msg['parent_header']) |
|
116 | 122 | self.assertEqual(new_msg['buffers'],[b'bar']) |
|
117 | 123 | |
|
118 | 124 | A.close() |
|
119 | 125 | B.close() |
|
120 | 126 | ctx.term() |
|
121 | 127 | |
|
122 | 128 | def test_args(self): |
|
123 | 129 | """initialization arguments for Session""" |
|
124 | 130 | s = self.session |
|
125 | 131 | self.assertTrue(s.pack is ss.default_packer) |
|
126 | 132 | self.assertTrue(s.unpack is ss.default_unpacker) |
|
127 | 133 | self.assertEqual(s.username, os.environ.get('USER', u'username')) |
|
128 | 134 | |
|
129 | 135 | s = ss.Session() |
|
130 | 136 | self.assertEqual(s.username, os.environ.get('USER', u'username')) |
|
131 | 137 | |
|
132 | 138 | self.assertRaises(TypeError, ss.Session, pack='hi') |
|
133 | 139 | self.assertRaises(TypeError, ss.Session, unpack='hi') |
|
134 | 140 | u = str(uuid.uuid4()) |
|
135 | 141 | s = ss.Session(username=u'carrot', session=u) |
|
136 | 142 | self.assertEqual(s.session, u) |
|
137 | 143 | self.assertEqual(s.username, u'carrot') |
|
138 | 144 | |
|
139 | 145 | def test_tracking(self): |
|
140 | 146 | """test tracking messages""" |
|
141 | 147 | a,b = self.create_bound_pair(zmq.PAIR, zmq.PAIR) |
|
142 | 148 | s = self.session |
|
143 | 149 | s.copy_threshold = 1 |
|
144 | 150 | stream = ZMQStream(a) |
|
145 | 151 | msg = s.send(a, 'hello', track=False) |
|
146 | 152 | self.assertTrue(msg['tracker'] is ss.DONE) |
|
147 | 153 | msg = s.send(a, 'hello', track=True) |
|
148 | 154 | self.assertTrue(isinstance(msg['tracker'], zmq.MessageTracker)) |
|
149 | 155 | M = zmq.Message(b'hi there', track=True) |
|
150 | 156 | msg = s.send(a, 'hello', buffers=[M], track=True) |
|
151 | 157 | t = msg['tracker'] |
|
152 | 158 | self.assertTrue(isinstance(t, zmq.MessageTracker)) |
|
153 | 159 | self.assertRaises(zmq.NotDone, t.wait, .1) |
|
154 | 160 | del M |
|
155 | 161 | t.wait(1) # this will raise |
|
156 | 162 | |
|
157 | 163 | |
|
158 | # def test_rekey(self): | |
|
159 | # """rekeying dict around json str keys""" | |
|
160 | # d = {'0': uuid.uuid4(), 0:uuid.uuid4()} | |
|
161 | # self.assertRaises(KeyError, ss.rekey, d) | |
|
162 | # | |
|
163 | # d = {'0': uuid.uuid4(), 1:uuid.uuid4(), 'asdf':uuid.uuid4()} | |
|
164 | # d2 = {0:d['0'],1:d[1],'asdf':d['asdf']} | |
|
165 | # rd = ss.rekey(d) | |
|
166 | # self.assertEqual(d2,rd) | |
|
167 | # | |
|
168 | # d = {'1.5':uuid.uuid4(),'1':uuid.uuid4()} | |
|
169 | # d2 = {1.5:d['1.5'],1:d['1']} | |
|
170 | # rd = ss.rekey(d) | |
|
171 | # self.assertEqual(d2,rd) | |
|
172 | # | |
|
173 | # d = {'1.0':uuid.uuid4(),'1':uuid.uuid4()} | |
|
174 | # self.assertRaises(KeyError, ss.rekey, d) | |
|
175 | # | |
|
176 | 164 | def test_unique_msg_ids(self): |
|
177 | 165 | """test that messages receive unique ids""" |
|
178 | 166 | ids = set() |
|
179 | 167 | for i in range(2**12): |
|
180 | 168 | h = self.session.msg_header('test') |
|
181 | 169 | msg_id = h['msg_id'] |
|
182 | 170 | self.assertTrue(msg_id not in ids) |
|
183 | 171 | ids.add(msg_id) |
|
184 | 172 | |
|
185 | 173 | def test_feed_identities(self): |
|
186 | 174 | """scrub the front for zmq IDENTITIES""" |
|
187 | 175 | theids = "engine client other".split() |
|
188 | 176 | content = dict(code='whoda',stuff=object()) |
|
189 | 177 | themsg = self.session.msg('execute',content=content) |
|
190 | 178 | pmsg = theids |
|
191 | 179 | |
|
192 | 180 | def test_session_id(self): |
|
193 | 181 | session = ss.Session() |
|
194 | 182 | # get bs before us |
|
195 | 183 | bs = session.bsession |
|
196 | 184 | us = session.session |
|
197 | 185 | self.assertEqual(us.encode('ascii'), bs) |
|
198 | 186 | session = ss.Session() |
|
199 | 187 | # get us before bs |
|
200 | 188 | us = session.session |
|
201 | 189 | bs = session.bsession |
|
202 | 190 | self.assertEqual(us.encode('ascii'), bs) |
|
203 | 191 | # change propagates: |
|
204 | 192 | session.session = 'something else' |
|
205 | 193 | bs = session.bsession |
|
206 | 194 | us = session.session |
|
207 | 195 | self.assertEqual(us.encode('ascii'), bs) |
|
208 | 196 | session = ss.Session(session='stuff') |
|
209 | 197 | # get us before bs |
|
210 | 198 | self.assertEqual(session.bsession, session.session.encode('ascii')) |
|
211 | 199 | self.assertEqual(b'stuff', session.bsession) |
|
212 | 200 | |
|
213 | 201 | def test_zero_digest_history(self): |
|
214 | 202 | session = ss.Session(digest_history_size=0) |
|
215 | 203 | for i in range(11): |
|
216 | 204 | session._add_digest(uuid.uuid4().bytes) |
|
217 | 205 | self.assertEqual(len(session.digest_history), 0) |
|
218 | 206 | |
|
219 | 207 | def test_cull_digest_history(self): |
|
220 | 208 | session = ss.Session(digest_history_size=100) |
|
221 | 209 | for i in range(100): |
|
222 | 210 | session._add_digest(uuid.uuid4().bytes) |
|
223 | 211 | self.assertTrue(len(session.digest_history) == 100) |
|
224 | 212 | session._add_digest(uuid.uuid4().bytes) |
|
225 | 213 | self.assertTrue(len(session.digest_history) == 91) |
|
226 | 214 | for i in range(9): |
|
227 | 215 | session._add_digest(uuid.uuid4().bytes) |
|
228 | 216 | self.assertTrue(len(session.digest_history) == 100) |
|
229 | 217 | session._add_digest(uuid.uuid4().bytes) |
|
230 | 218 | self.assertTrue(len(session.digest_history) == 91) |
|
231 | 219 | |
|
232 | 220 | def test_bad_pack(self): |
|
233 | 221 | try: |
|
234 | 222 | session = ss.Session(pack=_bad_packer) |
|
235 | 223 | except ValueError as e: |
|
236 | 224 | self.assertIn("could not serialize", str(e)) |
|
237 | 225 | self.assertIn("don't work", str(e)) |
|
238 | 226 | else: |
|
239 | 227 | self.fail("Should have raised ValueError") |
|
240 | 228 | |
|
241 | 229 | def test_bad_unpack(self): |
|
242 | 230 | try: |
|
243 | 231 | session = ss.Session(unpack=_bad_unpacker) |
|
244 | 232 | except ValueError as e: |
|
245 | 233 | self.assertIn("could not handle output", str(e)) |
|
246 | 234 | self.assertIn("don't work either", str(e)) |
|
247 | 235 | else: |
|
248 | 236 | self.fail("Should have raised ValueError") |
|
249 | 237 | |
|
250 | 238 | def test_bad_packer(self): |
|
251 | 239 | try: |
|
252 | 240 | session = ss.Session(packer=__name__ + '._bad_packer') |
|
253 | 241 | except ValueError as e: |
|
254 | 242 | self.assertIn("could not serialize", str(e)) |
|
255 | 243 | self.assertIn("don't work", str(e)) |
|
256 | 244 | else: |
|
257 | 245 | self.fail("Should have raised ValueError") |
|
258 | 246 | |
|
259 | 247 | def test_bad_unpacker(self): |
|
260 | 248 | try: |
|
261 | 249 | session = ss.Session(unpacker=__name__ + '._bad_unpacker') |
|
262 | 250 | except ValueError as e: |
|
263 | 251 | self.assertIn("could not handle output", str(e)) |
|
264 | 252 | self.assertIn("don't work either", str(e)) |
|
265 | 253 | else: |
|
266 | 254 | self.fail("Should have raised ValueError") |
|
267 | 255 | |
|
268 | 256 | def test_bad_roundtrip(self): |
|
269 | 257 | with self.assertRaises(ValueError): |
|
270 |
session= |
|
|
258 | session = ss.Session(unpack=lambda b: 5) | |
|
259 | ||
|
260 | def _datetime_test(self, session): | |
|
261 | content = dict(t=datetime.now()) | |
|
262 | metadata = dict(t=datetime.now()) | |
|
263 | p = session.msg('msg') | |
|
264 | msg = session.msg('msg', content=content, metadata=metadata, parent=p['header']) | |
|
265 | smsg = session.serialize(msg) | |
|
266 | msg2 = session.unserialize(session.feed_identities(smsg)[1]) | |
|
267 | assert isinstance(msg2['header']['date'], datetime) | |
|
268 | self.assertEqual(msg['header'], msg2['header']) | |
|
269 | self.assertEqual(msg['parent_header'], msg2['parent_header']) | |
|
270 | self.assertEqual(msg['parent_header'], msg2['parent_header']) | |
|
271 | assert isinstance(msg['content']['t'], datetime) | |
|
272 | assert isinstance(msg['metadata']['t'], datetime) | |
|
273 | assert isinstance(msg2['content']['t'], string_types) | |
|
274 | assert isinstance(msg2['metadata']['t'], string_types) | |
|
275 | self.assertEqual(msg['content'], jsonutil.extract_dates(msg2['content'])) | |
|
276 | self.assertEqual(msg['content'], jsonutil.extract_dates(msg2['content'])) | |
|
277 | ||
|
278 | def test_datetimes(self): | |
|
279 | self._datetime_test(self.session) | |
|
280 | ||
|
281 | def test_datetimes_pickle(self): | |
|
282 | session = ss.Session(packer='pickle') | |
|
283 | self._datetime_test(session) | |
|
284 | ||
|
285 | @skipif(module_not_available('msgpack')) | |
|
286 | def test_datetimes_msgpack(self): | |
|
287 | session = ss.Session(packer='msgpack.packb', unpacker='msgpack.unpackb') | |
|
288 | self._datetime_test(session) | |
|
271 | 289 |
General Comments 0
You need to be logged in to leave comments.
Login now