Here are the examples of the python api pizco.protocol.Protocol taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
6 Examples
3
Example 1
Project: pizco Source File: test_pizco.py
def test_agent_rep(self):
agent = Agent()
req = self.create_socket(zmq.REQ)
req.connect(agent.rep_endpoint)
prot = Protocol()
msg = prot.format('friend', 'bla', (None, 123, 'Test'))
req.send_multipart(msg)
ret = req.recv_multipart()
sender, topic, content, msgid = prot.parse(ret)
self.assertEqual(sender, agent.rep_endpoint)
self.assertEqual(topic, 'bla')
self.assertEqual(content, (None, 123, 'Test'))
agent.stop()
time.sleep(.1)
3
Example 2
def test_protocol(self):
prot = Protocol()
self.assertRaises(ValueError, prot.parse, [])
msg = prot.format('friend', 'bla', 'here goes the content')
sender, topic, content, msgid = prot.parse(msg)
self.assertEqual(sender, 'friend')
self.assertEqual(topic, 'bla')
self.assertEqual(content, 'here goes the content')
real_id = msg[1]
msg[1] = 'newid'.encode('utf-8')
self.assertRaises(ValueError, prot.parse, msg, check_msgid='wrong id')
self.assertRaises(ValueError, prot.parse, msg, check_sender='another')
msg[-1] = 'fake signature'.encode('utf-8')
msg[1] = real_id
self.assertEqual(sender, 'friend')
self.assertEqual(topic, 'bla')
self.assertEqual(content, 'here goes the content')
3
Example 3
def test_protocol_key(self):
prot = Protocol(hmac_key='have a key')
msg = prot.format('friend', 'bla', 'here goes the content')
sender, topic, content, msgid = prot.parse(msg)
self.assertEqual(sender, 'friend')
self.assertEqual(topic, 'bla')
self.assertEqual(content, 'here goes the content')
real_id = msg[1]
msg[1] = 'newid'.encode('utf-8')
self.assertRaises(ValueError, prot.parse, msg, check_msgid='wrong id')
self.assertRaises(ValueError, prot.parse, msg, check_sender='another')
msg[-1] = 'fake signature'.encode('utf-8')
msg[1] = real_id
self.assertRaises(ValueError, prot.parse, msg)
0
Example 4
Project: pizco Source File: test_pizco.py
def test_agent_publish(self):
prot = Protocol()
agent = Agent()
topic1 = 'topic1'
topic2 = 'topic2'
sub = self.create_socket(zmq.SUB)
sub.connect(agent.pub_endpoint)
sub.setsockopt(zmq.SUBSCRIBE, prot.format(agent.rep_endpoint, topic1, just_header=True))
time.sleep(SLEEP_SECS)
self.assertTrue(topic1 in agent.subscribers)
self.assertEqual(agent.subscribers[topic1], 1)
self.assertFalse(topic2 in agent.subscribers)
agent.publish(topic1, 'message')
sender, topic, content, msgid = prot.parse(sub.recv_multipart())
self.assertEqual(content, 'message')
agent.publish(topic2, 'message')
time.sleep(SLEEP_SECS)
self.assertRaises(zmq.ZMQError, sub.recv_multipart, flags=zmq.NOBLOCK)
agent.publish('top', 'message')
time.sleep(SLEEP_SECS)
self.assertRaises(zmq.ZMQError, sub.recv_multipart, flags=zmq.NOBLOCK)
sub.close()
agent.stop()
0
Example 5
Project: pizco Source File: test_pizco.py
def test_agent_subscribe(self):
pub = self.create_socket(zmq.PUB)
port = pub.bind_to_random_port('tcp://127.0.0.1')
endpoint = 'tcp://127.0.0.1:{}'.format(port)
agent = Agent()
topic1 = 'topic1'
topic2 = 'topic2'
class MemMethod(object):
def __init__(self_):
self_.called = 0
def __call__(self_, sender, topic, content, msgid):
self_.called += 1
self.assertEqual(sender, endpoint)
self.assertEqual(topic, topic1)
self.assertEqual(content, 'you should know that')
fun = MemMethod()
prot = Protocol()
agent.subscribe(endpoint, topic1, fun, endpoint)
time.sleep(SLEEP_SECS)
pub.send_multipart(prot.format(endpoint, topic1, 'you should know that'))
time.sleep(4 * SLEEP_SECS)
self.assertEqual(fun.called, 1)
pub.send_multipart(prot.format(endpoint, topic2, 'you should know that'))
time.sleep(SLEEP_SECS)
self.assertEqual(fun.called, 1)
agent.stop()
pub.close()
0
Example 6
Project: pizco Source File: test_pizco.py
def test_agent_subscribe_default(self):
pub = self.create_socket(zmq.PUB)
port = pub.bind_to_random_port('tcp://127.0.0.1')
endpoint = 'tcp://127.0.0.1:{}'.format(port)
class DefNot(Agent):
called = 0
def on_notification(self, sender, topic, msgid, content):
self.called += 1
agent = DefNot()
topic1 = 'topic1'
topic2 = 'topic2'
topic3 = 'topic3'
def do_nothing(sender, topic, msgid, content):
pass
prot = Protocol()
agent.subscribe(endpoint, topic1, None, endpoint)
agent.subscribe(endpoint, topic2, do_nothing, endpoint)
time.sleep(SLEEP_SECS)
pub.send_multipart(prot.format(endpoint, topic1, 'some'))
time.sleep(SLEEP_SECS)
self.assertEqual(agent.called, 1)
pub.send_multipart(prot.format(endpoint, topic2, 'more news'))
time.sleep(SLEEP_SECS)
self.assertEqual(agent.called, 1)
pub.send_multipart(prot.format(endpoint, topic3, 'you should know that'))
time.sleep(SLEEP_SECS)
self.assertEqual(agent.called, 1)
pub.close()
agent.stop()