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()
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()
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)
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)
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)