Example #1
0
        def test_link_receive_multiple(self):
            """
            Test receiving from multiple connections
            """

            link_rep = link.Link()
            link_rep.wire("first", receive=self.listeners[0])
            link_rep.wire("second", receive=self.listeners[1])

            link_req = link.Link()
            link_req.wire("first", send=self.connections[0])
            link_req.wire("second", send=self.connections[1])

            self.assertEqual(isinstance(link_req.first, link.Wire), True)
            self.assertEqual(isinstance(link_req.second, link.Wire), True)
            self.assertEqual(isinstance(link_rep.first, link.Wire), True)
            self.assertEqual(isinstance(link_rep.second, link.Wire), True)

            status = {}

            def rep_receive(**kwargs):
                message = kwargs.get("message")
                status[message.data] = True

            link_rep.on("receive", rep_receive)
            link_req.first.send("test", Message("First!"))
            link_req.second.send("test", Message("Second!"))

            while status.get("First!") != True or status.get(
                    "Second!") != True:
                time.sleep(0.01)
Example #2
0
 def receive(self):
     message = super(REP_Connection, self).receive()
     self._waiting = False
     if not message.responded:
         self.respond(message, Message(None))
     self._waiting = True
     return message
Example #3
0
        def test_link_send_and_receive(self):
            """
            Test sending and receiving via links (one connection each)
            """

            link_rep = link.Link()
            link_rep.wire("main", receive=self.listeners[0])

            link_req = link.Link()
            link_req.wire("main", send=self.connections[0])

            self.assertEqual(isinstance(link_req.main, link.Wire), True)
            self.assertEqual(isinstance(link_rep.main, link.Wire), True)

            status = {}

            def rep_receive(**kwargs):
                message = kwargs.get("message")
                status[message.data] = True

            link_rep.on("receive", rep_receive, once=True)
            link_req.main.send("test", Message("Ping!"))

            while status.get("Ping!") != True:
                time.sleep(0.01)
Example #4
0
    def test_rep_req(self):
        """
        Tests a rep / req exchange with a manually manufactored
        response
        """

        tester = self
        counter = []

        def handle_receive_listener(**kwargs):
            message = kwargs.get("message")
            event_origin = kwargs.get("event_origin")
            self.assertEqual(message.data, "Hello")
            event_origin.respond(message, Message("World"))
            counter.append(event_origin)

        def handle_receive_connection(**kwargs):
            message = kwargs.get("message")
            event_origin = kwargs.get("event_origin")
            self.assertEqual(message.data, "World")
            counter.append(event_origin)

        self.listeners[0].on("receive", handle_receive_listener, once=True)
        self.connections[0].on("receive", handle_receive_connection, once=True)

        self.connections[0].send(Message("Hello"))

        while len(counter) != 2:
            time.sleep(0.01)
Example #5
0
    def test_rep_req_auto_response(self):
        """
        Tests a rep / req exchange with an automatically manufactured
        null response from the rep connection
        """

        tester = self
        counter = []

        def handle_receive_listener(**kwargs):
            message = kwargs.get("message")
            event_origin = kwargs.get("event_origin")
            self.assertEqual(message.data, "Hello")
            counter.append(event_origin)

        def handle_receive_connection(**kwargs):
            message = kwargs.get("message")
            event_origin = kwargs.get("event_origin")
            self.assertEqual(message.data, None)
            counter.append(event_origin)

        self.listeners[0].on("receive", handle_receive_listener, once=True)
        self.connections[0].on("receive", handle_receive_connection, once=True)
        self.connections[0].send(Message("Hello"))

        while len(counter) != 2:
            time.sleep(0.01)
Example #6
0
    def test_push_pull(self):

        tester = self
        status = {}

        def handle_receive_connection(**kwargs):
            message = kwargs.get("message")
            event_origin = kwargs.get("event_origin")
            self.assertIn(message.data, ["first", "second"])
            status[message.data] = True

        for conn in self.connections:
            conn.on("receive", handle_receive_connection, once=True)

        self.listeners[0].send(Message("first"))
        self.listeners[0].send(Message("second"))

        while "first" not in status or "second" not in status:
            time.sleep(0.01)
Example #7
0
    def receive(self):
        self.log_debug("Attempting to receive ...")

        try:
            data = self.socket.recv_string()
        except zmq.ZMQError as inst:
            if str(inst).find("operation on non-socket") > -1:
                self.destroy()
                return Message(None)
            raise

        if self.topic:
            data = data.lstrip("%s " % self.topic)
        return super(Connection, self).receive(data)
Example #8
0
        def test_link_send_receive_respond(self):
            """
            Tests responding to a received message
            """

            link_rep = link.Link()
            link_rep.wire("main",
                          receive=self.listeners[0],
                          respond=self.listeners[0])

            link_req = link.Link()
            link_req.wire("main",
                          receive=self.connections[0],
                          send=self.connections[0])

            self.assertEqual(isinstance(link_req.main, link.Wire), True)
            self.assertEqual(isinstance(link_rep.main, link.Wire), True)

            status = {}

            def rep_receive_ping(**kwargs):
                message = kwargs.get("message")
                wire = kwargs.get("wire")
                wire.respond(message, Message("Pong!"))
                status[message.data] = True

            def msg_response(message, **kwargs):
                status[message.data] = True

            ping_message = Message("Ping!")
            ping_message.on("response", msg_response)

            link_rep.on("receive_ping", rep_receive_ping, once=True)
            link_req.main.send("ping", ping_message)

            while status.get("Ping!") != True or status.get("Pong!") != True:
                time.sleep(0.01)
Example #9
0
    def make_message(self, data):
        """
        Create a Message instance from data, data will be loaded
        via munge according to the codec specified in the
        transport_content_type attribute

        Returns:

            Message: message object
        """

        data = self.codec.loads(data)
        msg = Message(data.get("data"), *data.get("args", []),
                      **data.get("kwargs", {}))
        msg.meta.update(data.get("meta"))
        self.trigger("make_message", data, msg)
        return msg
Example #10
0
    def test_pub_sub(self):
        """
        Tests a pub / sub exchange
        """

        tester = self
        counter = []

        def handle_receive_connection(**kwargs):
            message = kwargs.get("message")
            event_origin = kwargs.get("event_origin")
            tester.assertEqual(message.data, "Ping!")
            counter.append(event_origin)

        for conn in self.connections:
            conn.on("receive", handle_receive_connection, once=True)

        self.listeners[0].send(Message("Ping!"))

        while len(counter) < len(self.connections):
            time.sleep(0.01)
Example #11
0
 def rep_receive_ping(**kwargs):
     message = kwargs.get("message")
     wire = kwargs.get("wire")
     wire.respond(message, Message("Pong!"))
     status[message.data] = True
Example #12
0
 def handle_receive_listener(**kwargs):
     message = kwargs.get("message")
     event_origin = kwargs.get("event_origin")
     self.assertEqual(message.data, "Hello")
     event_origin.respond(message, Message("World"))
     counter.append(event_origin)