test_session.py
219 lines
| 8.4 KiB
| text/x-python
|
PythonLexer
MinRK
|
r3970 | """test building messages with streamsession""" | ||
#------------------------------------------------------------------------------- | ||||
# Copyright (C) 2011 The IPython Development Team | ||||
# | ||||
# Distributed under the terms of the BSD License. The full license is in | ||||
# the file COPYING, distributed as part of this software. | ||||
#------------------------------------------------------------------------------- | ||||
#------------------------------------------------------------------------------- | ||||
# Imports | ||||
#------------------------------------------------------------------------------- | ||||
import os | ||||
import uuid | ||||
import zmq | ||||
from zmq.tests import BaseZMQTestCase | ||||
from zmq.eventloop.zmqstream import ZMQStream | ||||
MinRK
|
r4006 | |||
from IPython.zmq import session as ss | ||||
MinRK
|
r3970 | |||
class SessionTestCase(BaseZMQTestCase): | ||||
Bernardo B. Marques
|
r4872 | |||
MinRK
|
r3970 | def setUp(self): | ||
BaseZMQTestCase.setUp(self) | ||||
MinRK
|
r4006 | self.session = ss.Session() | ||
MinRK
|
r3970 | |||
Brian E. Granger
|
r4234 | |||
class MockSocket(zmq.Socket): | ||||
def __init__(self, *args, **kwargs): | ||||
super(MockSocket,self).__init__(*args,**kwargs) | ||||
self.data = [] | ||||
def send_multipart(self, msgparts, *args, **kwargs): | ||||
self.data.extend(msgparts) | ||||
def send(self, part, *args, **kwargs): | ||||
self.data.append(part) | ||||
def recv_multipart(self, *args, **kwargs): | ||||
return self.data | ||||
MinRK
|
r3970 | class TestSession(SessionTestCase): | ||
Bernardo B. Marques
|
r4872 | |||
MinRK
|
r3970 | def test_msg(self): | ||
"""message format""" | ||||
msg = self.session.msg('execute') | ||||
Jason Grout
|
r7955 | thekeys = set('header parent_header metadata content msg_type msg_id'.split()) | ||
MinRK
|
r3970 | s = set(msg.keys()) | ||
Bradley M. Froehle
|
r7874 | self.assertEqual(s, thekeys) | ||
MinRK
|
r3970 | self.assertTrue(isinstance(msg['content'],dict)) | ||
Jason Grout
|
r7955 | self.assertTrue(isinstance(msg['metadata'],dict)) | ||
MinRK
|
r3970 | self.assertTrue(isinstance(msg['header'],dict)) | ||
self.assertTrue(isinstance(msg['parent_header'],dict)) | ||||
MinRK
|
r4711 | self.assertTrue(isinstance(msg['msg_id'],str)) | ||
self.assertTrue(isinstance(msg['msg_type'],str)) | ||||
Bradley M. Froehle
|
r7874 | self.assertEqual(msg['header']['msg_type'], 'execute') | ||
self.assertEqual(msg['msg_type'], 'execute') | ||||
Brian E. Granger
|
r4233 | |||
def test_serialize(self): | ||||
MinRK
|
r6060 | msg = self.session.msg('execute', content=dict(a=10, b=1.1)) | ||
Brian E. Granger
|
r4233 | msg_list = self.session.serialize(msg, ident=b'foo') | ||
ident, msg_list = self.session.feed_identities(msg_list) | ||||
new_msg = self.session.unserialize(msg_list) | ||||
Bradley M. Froehle
|
r7874 | self.assertEqual(ident[0], b'foo') | ||
self.assertEqual(new_msg['msg_id'],msg['msg_id']) | ||||
self.assertEqual(new_msg['msg_type'],msg['msg_type']) | ||||
self.assertEqual(new_msg['header'],msg['header']) | ||||
self.assertEqual(new_msg['content'],msg['content']) | ||||
self.assertEqual(new_msg['parent_header'],msg['parent_header']) | ||||
Jason Grout
|
r7955 | self.assertEqual(new_msg['metadata'],msg['metadata']) | ||
MinRK
|
r6060 | # ensure floats don't come out as Decimal: | ||
Bradley M. Froehle
|
r7874 | self.assertEqual(type(new_msg['content']['b']),type(new_msg['content']['b'])) | ||
Brian E. Granger
|
r4233 | |||
Brian E. Granger
|
r4234 | def test_send(self): | ||
socket = MockSocket(zmq.Context.instance(),zmq.PAIR) | ||||
msg = self.session.msg('execute', content=dict(a=10)) | ||||
self.session.send(socket, msg, ident=b'foo', buffers=[b'bar']) | ||||
ident, msg_list = self.session.feed_identities(socket.data) | ||||
new_msg = self.session.unserialize(msg_list) | ||||
Bradley M. Froehle
|
r7874 | self.assertEqual(ident[0], b'foo') | ||
self.assertEqual(new_msg['msg_id'],msg['msg_id']) | ||||
self.assertEqual(new_msg['msg_type'],msg['msg_type']) | ||||
self.assertEqual(new_msg['header'],msg['header']) | ||||
self.assertEqual(new_msg['content'],msg['content']) | ||||
self.assertEqual(new_msg['parent_header'],msg['parent_header']) | ||||
Jason Grout
|
r7955 | self.assertEqual(new_msg['metadata'],msg['metadata']) | ||
Bradley M. Froehle
|
r7874 | self.assertEqual(new_msg['buffers'],[b'bar']) | ||
Brian E. Granger
|
r4234 | |||
socket.data = [] | ||||
content = msg['content'] | ||||
header = msg['header'] | ||||
parent = msg['parent_header'] | ||||
Jason Grout
|
r7955 | metadata = msg['metadata'] | ||
Brian E. Granger
|
r4234 | msg_type = header['msg_type'] | ||
self.session.send(socket, None, content=content, parent=parent, | ||||
Jason Grout
|
r7955 | header=header, metadata=metadata, ident=b'foo', buffers=[b'bar']) | ||
Brian E. Granger
|
r4234 | ident, msg_list = self.session.feed_identities(socket.data) | ||
new_msg = self.session.unserialize(msg_list) | ||||
Bradley M. Froehle
|
r7874 | self.assertEqual(ident[0], b'foo') | ||
self.assertEqual(new_msg['msg_id'],msg['msg_id']) | ||||
self.assertEqual(new_msg['msg_type'],msg['msg_type']) | ||||
self.assertEqual(new_msg['header'],msg['header']) | ||||
self.assertEqual(new_msg['content'],msg['content']) | ||||
Jason Grout
|
r7955 | self.assertEqual(new_msg['metadata'],msg['metadata']) | ||
Bradley M. Froehle
|
r7874 | self.assertEqual(new_msg['parent_header'],msg['parent_header']) | ||
self.assertEqual(new_msg['buffers'],[b'bar']) | ||||
Brian E. Granger
|
r4234 | |||
socket.data = [] | ||||
self.session.send(socket, msg, ident=b'foo', buffers=[b'bar']) | ||||
ident, new_msg = self.session.recv(socket) | ||||
Bradley M. Froehle
|
r7874 | self.assertEqual(ident[0], b'foo') | ||
self.assertEqual(new_msg['msg_id'],msg['msg_id']) | ||||
self.assertEqual(new_msg['msg_type'],msg['msg_type']) | ||||
self.assertEqual(new_msg['header'],msg['header']) | ||||
self.assertEqual(new_msg['content'],msg['content']) | ||||
Jason Grout
|
r7955 | self.assertEqual(new_msg['metadata'],msg['metadata']) | ||
Bradley M. Froehle
|
r7874 | self.assertEqual(new_msg['parent_header'],msg['parent_header']) | ||
self.assertEqual(new_msg['buffers'],[b'bar']) | ||||
Brian E. Granger
|
r4234 | |||
socket.close() | ||||
MinRK
|
r3970 | def test_args(self): | ||
MinRK
|
r4006 | """initialization arguments for Session""" | ||
MinRK
|
r3970 | s = self.session | ||
self.assertTrue(s.pack is ss.default_packer) | ||||
self.assertTrue(s.unpack is ss.default_unpacker) | ||||
Bradley M. Froehle
|
r7874 | self.assertEqual(s.username, os.environ.get('USER', u'username')) | ||
Bernardo B. Marques
|
r4872 | |||
MinRK
|
r4006 | s = ss.Session() | ||
Bradley M. Froehle
|
r7874 | self.assertEqual(s.username, os.environ.get('USER', u'username')) | ||
Bernardo B. Marques
|
r4872 | |||
MinRK
|
r4006 | self.assertRaises(TypeError, ss.Session, pack='hi') | ||
self.assertRaises(TypeError, ss.Session, unpack='hi') | ||||
MinRK
|
r3970 | u = str(uuid.uuid4()) | ||
Brian E. Granger
|
r4234 | s = ss.Session(username=u'carrot', session=u) | ||
Bradley M. Froehle
|
r7874 | self.assertEqual(s.session, u) | ||
self.assertEqual(s.username, u'carrot') | ||||
Bernardo B. Marques
|
r4872 | |||
MinRK
|
r3970 | def test_tracking(self): | ||
"""test tracking messages""" | ||||
a,b = self.create_bound_pair(zmq.PAIR, zmq.PAIR) | ||||
s = self.session | ||||
MinRK
|
r7973 | s.copy_threshold = 1 | ||
MinRK
|
r3970 | stream = ZMQStream(a) | ||
msg = s.send(a, 'hello', track=False) | ||||
MinRK
|
r7974 | self.assertTrue(msg['tracker'] is ss.DONE) | ||
MinRK
|
r3970 | msg = s.send(a, 'hello', track=True) | ||
self.assertTrue(isinstance(msg['tracker'], zmq.MessageTracker)) | ||||
M = zmq.Message(b'hi there', track=True) | ||||
msg = s.send(a, 'hello', buffers=[M], track=True) | ||||
t = msg['tracker'] | ||||
self.assertTrue(isinstance(t, zmq.MessageTracker)) | ||||
self.assertRaises(zmq.NotDone, t.wait, .1) | ||||
del M | ||||
t.wait(1) # this will raise | ||||
Bernardo B. Marques
|
r4872 | |||
MinRK
|
r3970 | # def test_rekey(self): | ||
# """rekeying dict around json str keys""" | ||||
# d = {'0': uuid.uuid4(), 0:uuid.uuid4()} | ||||
# self.assertRaises(KeyError, ss.rekey, d) | ||||
Bernardo B. Marques
|
r4872 | # | ||
MinRK
|
r3970 | # d = {'0': uuid.uuid4(), 1:uuid.uuid4(), 'asdf':uuid.uuid4()} | ||
# d2 = {0:d['0'],1:d[1],'asdf':d['asdf']} | ||||
# rd = ss.rekey(d) | ||||
Bradley M. Froehle
|
r7874 | # self.assertEqual(d2,rd) | ||
Bernardo B. Marques
|
r4872 | # | ||
MinRK
|
r3970 | # d = {'1.5':uuid.uuid4(),'1':uuid.uuid4()} | ||
# d2 = {1.5:d['1.5'],1:d['1']} | ||||
# rd = ss.rekey(d) | ||||
Bradley M. Froehle
|
r7874 | # self.assertEqual(d2,rd) | ||
Bernardo B. Marques
|
r4872 | # | ||
MinRK
|
r3970 | # d = {'1.0':uuid.uuid4(),'1':uuid.uuid4()} | ||
# self.assertRaises(KeyError, ss.rekey, d) | ||||
Bernardo B. Marques
|
r4872 | # | ||
MinRK
|
r3970 | def test_unique_msg_ids(self): | ||
"""test that messages receive unique ids""" | ||||
ids = set() | ||||
for i in range(2**12): | ||||
h = self.session.msg_header('test') | ||||
msg_id = h['msg_id'] | ||||
self.assertTrue(msg_id not in ids) | ||||
ids.add(msg_id) | ||||
Bernardo B. Marques
|
r4872 | |||
MinRK
|
r3970 | def test_feed_identities(self): | ||
"""scrub the front for zmq IDENTITIES""" | ||||
theids = "engine client other".split() | ||||
content = dict(code='whoda',stuff=object()) | ||||
themsg = self.session.msg('execute',content=content) | ||||
pmsg = theids | ||||
Bernardo B. Marques
|
r4872 | |||
MinRK
|
r4770 | def test_session_id(self): | ||
session = ss.Session() | ||||
# get bs before us | ||||
bs = session.bsession | ||||
us = session.session | ||||
Bradley M. Froehle
|
r7874 | self.assertEqual(us.encode('ascii'), bs) | ||
MinRK
|
r4770 | session = ss.Session() | ||
# get us before bs | ||||
us = session.session | ||||
bs = session.bsession | ||||
Bradley M. Froehle
|
r7874 | self.assertEqual(us.encode('ascii'), bs) | ||
MinRK
|
r4770 | # change propagates: | ||
session.session = 'something else' | ||||
bs = session.bsession | ||||
us = session.session | ||||
Bradley M. Froehle
|
r7874 | self.assertEqual(us.encode('ascii'), bs) | ||
MinRK
|
r4770 | session = ss.Session(session='stuff') | ||
# get us before bs | ||||
Bradley M. Froehle
|
r7874 | self.assertEqual(session.bsession, session.session.encode('ascii')) | ||
self.assertEqual(b'stuff', session.bsession) | ||||
Bernardo B. Marques
|
r4872 | |||
Brian E. Granger
|
r4233 | |||