##// END OF EJS Templates
More tests for Session.send/recv.
Brian E. Granger -
Show More
@@ -244,7 +244,7 b' class Session(Configurable):'
244 def _session_default(self):
244 def _session_default(self):
245 return bytes(uuid.uuid4())
245 return bytes(uuid.uuid4())
246
246
247 username = Unicode(os.environ.get('USER','username'), config=True,
247 username = Unicode(os.environ.get('USER',u'username'), config=True,
248 help="""Username for the Session. Default is your system username.""")
248 help="""Username for the Session. Default is your system username.""")
249
249
250 # message signature related traits:
250 # message signature related traits:
@@ -455,7 +455,8 b' class Session(Configurable):'
455 The socket-like object used to send the data.
455 The socket-like object used to send the data.
456 msg_or_type : str or Message/dict
456 msg_or_type : str or Message/dict
457 Normally, msg_or_type will be a msg_type unless a message is being
457 Normally, msg_or_type will be a msg_type unless a message is being
458 sent more than once.
458 sent more than once. If a header is supplied, this can be set to
459 None and the msg_type will be pulled from the header.
459
460
460 content : dict or None
461 content : dict or None
461 The content of the message (ignored if msg_or_type is a message).
462 The content of the message (ignored if msg_or_type is a message).
@@ -26,6 +26,22 b' class SessionTestCase(BaseZMQTestCase):'
26 BaseZMQTestCase.setUp(self)
26 BaseZMQTestCase.setUp(self)
27 self.session = ss.Session()
27 self.session = ss.Session()
28
28
29
30 class MockSocket(zmq.Socket):
31
32 def __init__(self, *args, **kwargs):
33 super(MockSocket,self).__init__(*args,**kwargs)
34 self.data = []
35
36 def send_multipart(self, msgparts, *args, **kwargs):
37 self.data.extend(msgparts)
38
39 def send(self, part, *args, **kwargs):
40 self.data.append(part)
41
42 def recv_multipart(self, *args, **kwargs):
43 return self.data
44
29 class TestSession(SessionTestCase):
45 class TestSession(SessionTestCase):
30
46
31 def test_msg(self):
47 def test_msg(self):
@@ -40,7 +56,7 b' class TestSession(SessionTestCase):'
40 self.assertEquals(msg['header']['msg_type'], 'execute')
56 self.assertEquals(msg['header']['msg_type'], 'execute')
41
57
42 def test_serialize(self):
58 def test_serialize(self):
43 msg = self.session.msg('execute')
59 msg = self.session.msg('execute',content=dict(a=10))
44 msg_list = self.session.serialize(msg, ident=b'foo')
60 msg_list = self.session.serialize(msg, ident=b'foo')
45 ident, msg_list = self.session.feed_identities(msg_list)
61 ident, msg_list = self.session.feed_identities(msg_list)
46 new_msg = self.session.unserialize(msg_list)
62 new_msg = self.session.unserialize(msg_list)
@@ -49,22 +65,63 b' class TestSession(SessionTestCase):'
49 self.assertEquals(new_msg['content'],msg['content'])
65 self.assertEquals(new_msg['content'],msg['content'])
50 self.assertEquals(new_msg['parent_header'],msg['parent_header'])
66 self.assertEquals(new_msg['parent_header'],msg['parent_header'])
51
67
68 def test_send(self):
69 socket = MockSocket(zmq.Context.instance(),zmq.PAIR)
70
71 msg = self.session.msg('execute', content=dict(a=10))
72 self.session.send(socket, msg, ident=b'foo', buffers=[b'bar'])
73 ident, msg_list = self.session.feed_identities(socket.data)
74 new_msg = self.session.unserialize(msg_list)
75 self.assertEquals(ident[0], b'foo')
76 self.assertEquals(new_msg['header'],msg['header'])
77 self.assertEquals(new_msg['content'],msg['content'])
78 self.assertEquals(new_msg['parent_header'],msg['parent_header'])
79 self.assertEquals(new_msg['buffers'],[b'bar'])
80
81 socket.data = []
82
83 content = msg['content']
84 header = msg['header']
85 parent = msg['parent_header']
86 msg_type = header['msg_type']
87 self.session.send(socket, None, content=content, parent=parent,
88 header=header, ident=b'foo', buffers=[b'bar'])
89 ident, msg_list = self.session.feed_identities(socket.data)
90 new_msg = self.session.unserialize(msg_list)
91 self.assertEquals(ident[0], b'foo')
92 self.assertEquals(new_msg['header'],msg['header'])
93 self.assertEquals(new_msg['content'],msg['content'])
94 self.assertEquals(new_msg['parent_header'],msg['parent_header'])
95 self.assertEquals(new_msg['buffers'],[b'bar'])
96
97 socket.data = []
98
99 self.session.send(socket, msg, ident=b'foo', buffers=[b'bar'])
100 ident, new_msg = self.session.recv(socket)
101 self.assertEquals(ident[0], b'foo')
102 self.assertEquals(new_msg['header'],msg['header'])
103 self.assertEquals(new_msg['content'],msg['content'])
104 self.assertEquals(new_msg['parent_header'],msg['parent_header'])
105 self.assertEquals(new_msg['buffers'],[b'bar'])
106
107 socket.close()
108
52 def test_args(self):
109 def test_args(self):
53 """initialization arguments for Session"""
110 """initialization arguments for Session"""
54 s = self.session
111 s = self.session
55 self.assertTrue(s.pack is ss.default_packer)
112 self.assertTrue(s.pack is ss.default_packer)
56 self.assertTrue(s.unpack is ss.default_unpacker)
113 self.assertTrue(s.unpack is ss.default_unpacker)
57 self.assertEquals(s.username, os.environ.get('USER', 'username'))
114 self.assertEquals(s.username, os.environ.get('USER', u'username'))
58
115
59 s = ss.Session()
116 s = ss.Session()
60 self.assertEquals(s.username, os.environ.get('USER', 'username'))
117 self.assertEquals(s.username, os.environ.get('USER', u'username'))
61
118
62 self.assertRaises(TypeError, ss.Session, pack='hi')
119 self.assertRaises(TypeError, ss.Session, pack='hi')
63 self.assertRaises(TypeError, ss.Session, unpack='hi')
120 self.assertRaises(TypeError, ss.Session, unpack='hi')
64 u = str(uuid.uuid4())
121 u = str(uuid.uuid4())
65 s = ss.Session(username='carrot', session=u)
122 s = ss.Session(username=u'carrot', session=u)
66 self.assertEquals(s.session, u)
123 self.assertEquals(s.session, u)
67 self.assertEquals(s.username, 'carrot')
124 self.assertEquals(s.username, u'carrot')
68
125
69 def test_tracking(self):
126 def test_tracking(self):
70 """test tracking messages"""
127 """test tracking messages"""
General Comments 0
You need to be logged in to leave comments. Login now