Пример #1
0
    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()
Пример #2
0
    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()
Пример #3
0
    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)
Пример #4
0
    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)
Пример #5
0
    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)
Пример #6
0
    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()
Пример #7
0
    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)
Пример #8
0
    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()
Пример #9
0
    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()
Пример #10
0
    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()