##// END OF EJS Templates
remove MockSocket in test_session...
MinRK -
Show More
@@ -1,220 +1,207 b''
1 """test building messages with streamsession"""
1 """test building messages with streamsession"""
2
2
3 #-------------------------------------------------------------------------------
3 #-------------------------------------------------------------------------------
4 # Copyright (C) 2011 The IPython Development Team
4 # Copyright (C) 2011 The IPython Development Team
5 #
5 #
6 # Distributed under the terms of the BSD License. The full license is in
6 # Distributed under the terms of the BSD License. The full license is in
7 # the file COPYING, distributed as part of this software.
7 # the file COPYING, distributed as part of this software.
8 #-------------------------------------------------------------------------------
8 #-------------------------------------------------------------------------------
9
9
10 #-------------------------------------------------------------------------------
10 #-------------------------------------------------------------------------------
11 # Imports
11 # Imports
12 #-------------------------------------------------------------------------------
12 #-------------------------------------------------------------------------------
13
13
14 import os
14 import os
15 import uuid
15 import uuid
16 import zmq
16 import zmq
17
17
18 from zmq.tests import BaseZMQTestCase
18 from zmq.tests import BaseZMQTestCase
19 from zmq.eventloop.zmqstream import ZMQStream
19 from zmq.eventloop.zmqstream import ZMQStream
20
20
21 from IPython.zmq import session as ss
21 from IPython.zmq import session as ss
22
22
23 class SessionTestCase(BaseZMQTestCase):
23 class SessionTestCase(BaseZMQTestCase):
24
24
25 def setUp(self):
25 def setUp(self):
26 BaseZMQTestCase.setUp(self)
26 BaseZMQTestCase.setUp(self)
27 self.session = ss.Session()
27 self.session = ss.Session()
28
28
29
29
30 class MockSocket(zmq.Socket):
31 data = None
32
33 def __init__(self, *args, **kwargs):
34 super(MockSocket,self).__init__(*args,**kwargs)
35 self.data = []
36
37 def send_multipart(self, msgparts, *args, **kwargs):
38 self.data.extend(msgparts)
39
40 def send(self, part, *args, **kwargs):
41 self.data.append(part)
42
43 def recv_multipart(self, *args, **kwargs):
44 return self.data
45
46 class TestSession(SessionTestCase):
30 class TestSession(SessionTestCase):
47
31
48 def test_msg(self):
32 def test_msg(self):
49 """message format"""
33 """message format"""
50 msg = self.session.msg('execute')
34 msg = self.session.msg('execute')
51 thekeys = set('header parent_header metadata content msg_type msg_id'.split())
35 thekeys = set('header parent_header metadata content msg_type msg_id'.split())
52 s = set(msg.keys())
36 s = set(msg.keys())
53 self.assertEqual(s, thekeys)
37 self.assertEqual(s, thekeys)
54 self.assertTrue(isinstance(msg['content'],dict))
38 self.assertTrue(isinstance(msg['content'],dict))
55 self.assertTrue(isinstance(msg['metadata'],dict))
39 self.assertTrue(isinstance(msg['metadata'],dict))
56 self.assertTrue(isinstance(msg['header'],dict))
40 self.assertTrue(isinstance(msg['header'],dict))
57 self.assertTrue(isinstance(msg['parent_header'],dict))
41 self.assertTrue(isinstance(msg['parent_header'],dict))
58 self.assertTrue(isinstance(msg['msg_id'],str))
42 self.assertTrue(isinstance(msg['msg_id'],str))
59 self.assertTrue(isinstance(msg['msg_type'],str))
43 self.assertTrue(isinstance(msg['msg_type'],str))
60 self.assertEqual(msg['header']['msg_type'], 'execute')
44 self.assertEqual(msg['header']['msg_type'], 'execute')
61 self.assertEqual(msg['msg_type'], 'execute')
45 self.assertEqual(msg['msg_type'], 'execute')
62
46
63 def test_serialize(self):
47 def test_serialize(self):
64 msg = self.session.msg('execute', content=dict(a=10, b=1.1))
48 msg = self.session.msg('execute', content=dict(a=10, b=1.1))
65 msg_list = self.session.serialize(msg, ident=b'foo')
49 msg_list = self.session.serialize(msg, ident=b'foo')
66 ident, msg_list = self.session.feed_identities(msg_list)
50 ident, msg_list = self.session.feed_identities(msg_list)
67 new_msg = self.session.unserialize(msg_list)
51 new_msg = self.session.unserialize(msg_list)
68 self.assertEqual(ident[0], b'foo')
52 self.assertEqual(ident[0], b'foo')
69 self.assertEqual(new_msg['msg_id'],msg['msg_id'])
53 self.assertEqual(new_msg['msg_id'],msg['msg_id'])
70 self.assertEqual(new_msg['msg_type'],msg['msg_type'])
54 self.assertEqual(new_msg['msg_type'],msg['msg_type'])
71 self.assertEqual(new_msg['header'],msg['header'])
55 self.assertEqual(new_msg['header'],msg['header'])
72 self.assertEqual(new_msg['content'],msg['content'])
56 self.assertEqual(new_msg['content'],msg['content'])
73 self.assertEqual(new_msg['parent_header'],msg['parent_header'])
57 self.assertEqual(new_msg['parent_header'],msg['parent_header'])
74 self.assertEqual(new_msg['metadata'],msg['metadata'])
58 self.assertEqual(new_msg['metadata'],msg['metadata'])
75 # ensure floats don't come out as Decimal:
59 # ensure floats don't come out as Decimal:
76 self.assertEqual(type(new_msg['content']['b']),type(new_msg['content']['b']))
60 self.assertEqual(type(new_msg['content']['b']),type(new_msg['content']['b']))
77
61
78 def test_send(self):
62 def test_send(self):
79 socket = MockSocket(zmq.Context.instance(),zmq.PAIR)
63 ctx = zmq.Context.instance()
64 A = ctx.socket(zmq.PAIR)
65 B = ctx.socket(zmq.PAIR)
66 A.bind("inproc://test")
67 B.connect("inproc://test")
80
68
81 msg = self.session.msg('execute', content=dict(a=10))
69 msg = self.session.msg('execute', content=dict(a=10))
82 self.session.send(socket, msg, ident=b'foo', buffers=[b'bar'])
70 self.session.send(A, msg, ident=b'foo', buffers=[b'bar'])
83 ident, msg_list = self.session.feed_identities(socket.data)
71
72 ident, msg_list = self.session.feed_identities(B.recv_multipart())
84 new_msg = self.session.unserialize(msg_list)
73 new_msg = self.session.unserialize(msg_list)
85 self.assertEqual(ident[0], b'foo')
74 self.assertEqual(ident[0], b'foo')
86 self.assertEqual(new_msg['msg_id'],msg['msg_id'])
75 self.assertEqual(new_msg['msg_id'],msg['msg_id'])
87 self.assertEqual(new_msg['msg_type'],msg['msg_type'])
76 self.assertEqual(new_msg['msg_type'],msg['msg_type'])
88 self.assertEqual(new_msg['header'],msg['header'])
77 self.assertEqual(new_msg['header'],msg['header'])
89 self.assertEqual(new_msg['content'],msg['content'])
78 self.assertEqual(new_msg['content'],msg['content'])
90 self.assertEqual(new_msg['parent_header'],msg['parent_header'])
79 self.assertEqual(new_msg['parent_header'],msg['parent_header'])
91 self.assertEqual(new_msg['metadata'],msg['metadata'])
80 self.assertEqual(new_msg['metadata'],msg['metadata'])
92 self.assertEqual(new_msg['buffers'],[b'bar'])
81 self.assertEqual(new_msg['buffers'],[b'bar'])
93
82
94 socket.data = []
95
96 content = msg['content']
83 content = msg['content']
97 header = msg['header']
84 header = msg['header']
98 parent = msg['parent_header']
85 parent = msg['parent_header']
99 metadata = msg['metadata']
86 metadata = msg['metadata']
100 msg_type = header['msg_type']
87 msg_type = header['msg_type']
101 self.session.send(socket, None, content=content, parent=parent,
88 self.session.send(A, None, content=content, parent=parent,
102 header=header, metadata=metadata, ident=b'foo', buffers=[b'bar'])
89 header=header, metadata=metadata, ident=b'foo', buffers=[b'bar'])
103 ident, msg_list = self.session.feed_identities(socket.data)
90 ident, msg_list = self.session.feed_identities(B.recv_multipart())
104 new_msg = self.session.unserialize(msg_list)
91 new_msg = self.session.unserialize(msg_list)
105 self.assertEqual(ident[0], b'foo')
92 self.assertEqual(ident[0], b'foo')
106 self.assertEqual(new_msg['msg_id'],msg['msg_id'])
93 self.assertEqual(new_msg['msg_id'],msg['msg_id'])
107 self.assertEqual(new_msg['msg_type'],msg['msg_type'])
94 self.assertEqual(new_msg['msg_type'],msg['msg_type'])
108 self.assertEqual(new_msg['header'],msg['header'])
95 self.assertEqual(new_msg['header'],msg['header'])
109 self.assertEqual(new_msg['content'],msg['content'])
96 self.assertEqual(new_msg['content'],msg['content'])
110 self.assertEqual(new_msg['metadata'],msg['metadata'])
97 self.assertEqual(new_msg['metadata'],msg['metadata'])
111 self.assertEqual(new_msg['parent_header'],msg['parent_header'])
98 self.assertEqual(new_msg['parent_header'],msg['parent_header'])
112 self.assertEqual(new_msg['buffers'],[b'bar'])
99 self.assertEqual(new_msg['buffers'],[b'bar'])
113
100
114 socket.data = []
101 self.session.send(A, msg, ident=b'foo', buffers=[b'bar'])
115
102 ident, new_msg = self.session.recv(B)
116 self.session.send(socket, msg, ident=b'foo', buffers=[b'bar'])
117 ident, new_msg = self.session.recv(socket)
118 self.assertEqual(ident[0], b'foo')
103 self.assertEqual(ident[0], b'foo')
119 self.assertEqual(new_msg['msg_id'],msg['msg_id'])
104 self.assertEqual(new_msg['msg_id'],msg['msg_id'])
120 self.assertEqual(new_msg['msg_type'],msg['msg_type'])
105 self.assertEqual(new_msg['msg_type'],msg['msg_type'])
121 self.assertEqual(new_msg['header'],msg['header'])
106 self.assertEqual(new_msg['header'],msg['header'])
122 self.assertEqual(new_msg['content'],msg['content'])
107 self.assertEqual(new_msg['content'],msg['content'])
123 self.assertEqual(new_msg['metadata'],msg['metadata'])
108 self.assertEqual(new_msg['metadata'],msg['metadata'])
124 self.assertEqual(new_msg['parent_header'],msg['parent_header'])
109 self.assertEqual(new_msg['parent_header'],msg['parent_header'])
125 self.assertEqual(new_msg['buffers'],[b'bar'])
110 self.assertEqual(new_msg['buffers'],[b'bar'])
126
111
127 socket.close()
112 A.close()
113 B.close()
114 ctx.term()
128
115
129 def test_args(self):
116 def test_args(self):
130 """initialization arguments for Session"""
117 """initialization arguments for Session"""
131 s = self.session
118 s = self.session
132 self.assertTrue(s.pack is ss.default_packer)
119 self.assertTrue(s.pack is ss.default_packer)
133 self.assertTrue(s.unpack is ss.default_unpacker)
120 self.assertTrue(s.unpack is ss.default_unpacker)
134 self.assertEqual(s.username, os.environ.get('USER', u'username'))
121 self.assertEqual(s.username, os.environ.get('USER', u'username'))
135
122
136 s = ss.Session()
123 s = ss.Session()
137 self.assertEqual(s.username, os.environ.get('USER', u'username'))
124 self.assertEqual(s.username, os.environ.get('USER', u'username'))
138
125
139 self.assertRaises(TypeError, ss.Session, pack='hi')
126 self.assertRaises(TypeError, ss.Session, pack='hi')
140 self.assertRaises(TypeError, ss.Session, unpack='hi')
127 self.assertRaises(TypeError, ss.Session, unpack='hi')
141 u = str(uuid.uuid4())
128 u = str(uuid.uuid4())
142 s = ss.Session(username=u'carrot', session=u)
129 s = ss.Session(username=u'carrot', session=u)
143 self.assertEqual(s.session, u)
130 self.assertEqual(s.session, u)
144 self.assertEqual(s.username, u'carrot')
131 self.assertEqual(s.username, u'carrot')
145
132
146 def test_tracking(self):
133 def test_tracking(self):
147 """test tracking messages"""
134 """test tracking messages"""
148 a,b = self.create_bound_pair(zmq.PAIR, zmq.PAIR)
135 a,b = self.create_bound_pair(zmq.PAIR, zmq.PAIR)
149 s = self.session
136 s = self.session
150 s.copy_threshold = 1
137 s.copy_threshold = 1
151 stream = ZMQStream(a)
138 stream = ZMQStream(a)
152 msg = s.send(a, 'hello', track=False)
139 msg = s.send(a, 'hello', track=False)
153 self.assertTrue(msg['tracker'] is ss.DONE)
140 self.assertTrue(msg['tracker'] is ss.DONE)
154 msg = s.send(a, 'hello', track=True)
141 msg = s.send(a, 'hello', track=True)
155 self.assertTrue(isinstance(msg['tracker'], zmq.MessageTracker))
142 self.assertTrue(isinstance(msg['tracker'], zmq.MessageTracker))
156 M = zmq.Message(b'hi there', track=True)
143 M = zmq.Message(b'hi there', track=True)
157 msg = s.send(a, 'hello', buffers=[M], track=True)
144 msg = s.send(a, 'hello', buffers=[M], track=True)
158 t = msg['tracker']
145 t = msg['tracker']
159 self.assertTrue(isinstance(t, zmq.MessageTracker))
146 self.assertTrue(isinstance(t, zmq.MessageTracker))
160 self.assertRaises(zmq.NotDone, t.wait, .1)
147 self.assertRaises(zmq.NotDone, t.wait, .1)
161 del M
148 del M
162 t.wait(1) # this will raise
149 t.wait(1) # this will raise
163
150
164
151
165 # def test_rekey(self):
152 # def test_rekey(self):
166 # """rekeying dict around json str keys"""
153 # """rekeying dict around json str keys"""
167 # d = {'0': uuid.uuid4(), 0:uuid.uuid4()}
154 # d = {'0': uuid.uuid4(), 0:uuid.uuid4()}
168 # self.assertRaises(KeyError, ss.rekey, d)
155 # self.assertRaises(KeyError, ss.rekey, d)
169 #
156 #
170 # d = {'0': uuid.uuid4(), 1:uuid.uuid4(), 'asdf':uuid.uuid4()}
157 # d = {'0': uuid.uuid4(), 1:uuid.uuid4(), 'asdf':uuid.uuid4()}
171 # d2 = {0:d['0'],1:d[1],'asdf':d['asdf']}
158 # d2 = {0:d['0'],1:d[1],'asdf':d['asdf']}
172 # rd = ss.rekey(d)
159 # rd = ss.rekey(d)
173 # self.assertEqual(d2,rd)
160 # self.assertEqual(d2,rd)
174 #
161 #
175 # d = {'1.5':uuid.uuid4(),'1':uuid.uuid4()}
162 # d = {'1.5':uuid.uuid4(),'1':uuid.uuid4()}
176 # d2 = {1.5:d['1.5'],1:d['1']}
163 # d2 = {1.5:d['1.5'],1:d['1']}
177 # rd = ss.rekey(d)
164 # rd = ss.rekey(d)
178 # self.assertEqual(d2,rd)
165 # self.assertEqual(d2,rd)
179 #
166 #
180 # d = {'1.0':uuid.uuid4(),'1':uuid.uuid4()}
167 # d = {'1.0':uuid.uuid4(),'1':uuid.uuid4()}
181 # self.assertRaises(KeyError, ss.rekey, d)
168 # self.assertRaises(KeyError, ss.rekey, d)
182 #
169 #
183 def test_unique_msg_ids(self):
170 def test_unique_msg_ids(self):
184 """test that messages receive unique ids"""
171 """test that messages receive unique ids"""
185 ids = set()
172 ids = set()
186 for i in range(2**12):
173 for i in range(2**12):
187 h = self.session.msg_header('test')
174 h = self.session.msg_header('test')
188 msg_id = h['msg_id']
175 msg_id = h['msg_id']
189 self.assertTrue(msg_id not in ids)
176 self.assertTrue(msg_id not in ids)
190 ids.add(msg_id)
177 ids.add(msg_id)
191
178
192 def test_feed_identities(self):
179 def test_feed_identities(self):
193 """scrub the front for zmq IDENTITIES"""
180 """scrub the front for zmq IDENTITIES"""
194 theids = "engine client other".split()
181 theids = "engine client other".split()
195 content = dict(code='whoda',stuff=object())
182 content = dict(code='whoda',stuff=object())
196 themsg = self.session.msg('execute',content=content)
183 themsg = self.session.msg('execute',content=content)
197 pmsg = theids
184 pmsg = theids
198
185
199 def test_session_id(self):
186 def test_session_id(self):
200 session = ss.Session()
187 session = ss.Session()
201 # get bs before us
188 # get bs before us
202 bs = session.bsession
189 bs = session.bsession
203 us = session.session
190 us = session.session
204 self.assertEqual(us.encode('ascii'), bs)
191 self.assertEqual(us.encode('ascii'), bs)
205 session = ss.Session()
192 session = ss.Session()
206 # get us before bs
193 # get us before bs
207 us = session.session
194 us = session.session
208 bs = session.bsession
195 bs = session.bsession
209 self.assertEqual(us.encode('ascii'), bs)
196 self.assertEqual(us.encode('ascii'), bs)
210 # change propagates:
197 # change propagates:
211 session.session = 'something else'
198 session.session = 'something else'
212 bs = session.bsession
199 bs = session.bsession
213 us = session.session
200 us = session.session
214 self.assertEqual(us.encode('ascii'), bs)
201 self.assertEqual(us.encode('ascii'), bs)
215 session = ss.Session(session='stuff')
202 session = ss.Session(session='stuff')
216 # get us before bs
203 # get us before bs
217 self.assertEqual(session.bsession, session.session.encode('ascii'))
204 self.assertEqual(session.bsession, session.session.encode('ascii'))
218 self.assertEqual(b'stuff', session.bsession)
205 self.assertEqual(b'stuff', session.bsession)
219
206
220
207
General Comments 0
You need to be logged in to leave comments. Login now