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_agent_stop(self): agent = Agent() agent_ctrl = Agent() ret = agent_ctrl.request(agent.rep_endpoint, 3) self.assertEqual(3, ret) ret = agent_ctrl.request(agent.rep_endpoint, 'stop') self.assertEqual('stopping', ret) agent_ctrl.stop()
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)
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()
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()