Ejemplo n.º 1
0
    def test_message_timeout_1(self):
        """ Test if a response message to a request results in the handler being called correctly.
        """

        conn = MockedConnection()
        chan = Channel(conn)

        conn.mock_connection_ready(True)

        req = chan.request('name', 'payload')
        handler = Mock()
        req.add_handler(handler, MessageStatus.ANY)
        reqi = req.send()

        conn.mock_downstream_verb(verbs.MessageVerb(
            messageref=1,
            status=verbs.MessageVerb.STATUS_TIMEOUT,
            payload=b'response'
        ))

        self.__verify_handler_call(
            handler,
            Message,
            status=MessageStatus.TIMEOUT,
            payload=None,
            source=reqi,
        )
Ejemplo n.º 2
0
    def test_interest_2(self):
        """ Test if the sessions interest handler is called when an interest packet is received.
        """

        conn = MockedConnection()
        chan = Channel(conn)

        conn.mock_connection_ready(True)

        session = chan.session('name')
        handler = Mock()
        session.add_interest_handler(handler)

        conn.mock_downstream_verb(verbs.InterestVerb(
            postref=1,
            name=b'name',
            status=verbs.InterestVerb.STATUS_NO_INTEREST,
            topic=b'topic'
        ))

        self.__verify_handler_call(
            handler,
            Interest,
            status=InterestStatus.NO_INTEREST,
            name='name',
            topic='topic',
            source=session,
        )
Ejemplo n.º 3
0
    def test_subscribe_unsubscribe_1(self):
        """ Test if both an subscribe and unsubscribe verb is pushed when the
        connection is ready before creation of the subscription.
        """

        conn = MockedConnection()
        chan = Channel(conn)

        conn.mock_connection_ready(True)

        sub = chan.subscribe('name', 'topic')

        conn.assert_upstream_verb(verbs.SubscribeVerb(
            name=b'name',
            topic=b'topic',
            messageref=1,
        ))

        sub.cancel()

        conn.assert_upstream_verb(verbs.UnsubscribeVerb(
            name=b'name',
            topic=b'topic',
        ))

        conn.assert_upstream_verb(None)
Ejemplo n.º 4
0
    def test_login_logout_2(self):
        """ Test if both the login and logout verb are pushed when the connection becomes ready
        after the session is created.
        """

        conn = MockedConnection()
        chan = Channel(conn)

        conn.mock_connection_ready(False)

        session = chan.session('name')

        conn.mock_connection_ready(True)

        conn.assert_upstream_verb(verbs.LoginVerb(
            name=b'name',
            enforce=False,
            standby=False,
            persist=False,
        ))

        session.cancel()

        conn.assert_upstream_verb(verbs.LogoutVerb(
            name=b'name',
        ))

        conn.assert_upstream_verb(None)
Ejemplo n.º 5
0
    def test_call_1(self):
        """ Test if the sessions call handler is called when an unidirectional call packet is
        received.
        """

        conn = MockedConnection()
        chan = Channel(conn)

        conn.mock_connection_ready(True)

        session = chan.session('name')
        handler = Mock()
        session.add_call_handler(handler)

        conn.mock_downstream_verb(verbs.CallVerb(
            unidirectional=False,
            postref=1,
            name=b'name',
            payload=b'payload',
        ))

        self.__verify_handler_call(
            handler,
            Call,
            unidirectional=False,
            name='name',
            postref=1,
            payload='payload',
        )
Ejemplo n.º 6
0
    def test_backlog_no_ttl(self):
        """ Test if the backlog discards requests with no ttl at all.
        """

        conn = MockedConnection()
        chan = Channel(conn)

        conn.mock_connection_ready(False)

        chan.request('name', 'payload0').send(ttl=0)
        chan.request('name', 'payload1').send(ttl=4.0)
        chan.request('name', 'payload2').send(ttl=0)
        chan.request('name', 'payload3').send(ttl=4.0)

        conn.mock_connection_ready(True)

        conn.assert_upstream_verb(verbs.RequestVerb(
            name=b'name',
            unidirectional=True,
            messageref=None,
            timeout=5.0,
            payload=b'payload1'
        ))

        conn.assert_upstream_verb(verbs.RequestVerb(
            name=b'name',
            unidirectional=True,
            messageref=None,
            timeout=5.0,
            payload=b'payload3'
        ))
Ejemplo n.º 7
0
    def test_subscribe_unsubscribe_2(self):
        """ Test if subscribe and unusbscribe verbs is pushed even when the connection only
        becomes ready after the subscription is created.
        """

        conn = MockedConnection()
        chan = Channel(conn)

        conn.mock_connection_ready(False)

        sub = chan.subscribe('name', 'topic')

        conn.mock_connection_ready(True)

        conn.assert_upstream_verb(verbs.SubscribeVerb(
            name=b'name',
            topic=b'topic',
            messageref=1,
        ))

        sub.cancel()

        conn.assert_upstream_verb(verbs.UnsubscribeVerb(
            name=b'name',
            topic=b'topic',
        ))

        conn.assert_upstream_verb(None)
Ejemplo n.º 8
0
    def test_post_4(self):
        """ Test if a post is discarded when connection becomes ready after the ttl is expired.
        """

        conn = MockedConnection()
        chan = Channel(conn)

        conn.mock_connection_ready(False)

        with patch_time() as time:
            chan.request('name', 'payload').send(ttl=5.0)

            time.sleep(5.001)

            conn.mock_connection_ready(True)

        conn.assert_upstream_verb(None)
Ejemplo n.º 9
0
    def test_subscribe_unsubscribe_3(self):
        """ Test if no verbs at all are pushed if the subscription is created and canceled before
        the connection was ready.
        """

        conn = MockedConnection()
        chan = Channel(conn)

        conn.mock_connection_ready(False)

        sub = chan.subscribe('name', 'topic')

        sub.cancel()

        conn.mock_connection_ready(True)

        conn.assert_upstream_verb(None)
Ejemplo n.º 10
0
    def test_login_logout_3(self):
        """ Test if no verbs are pushed at all if the session is created and canceled before the
        connection became ready.
        """

        conn = MockedConnection()
        chan = Channel(conn)

        conn.mock_connection_ready(False)

        session = chan.session('name')

        session.cancel()

        conn.mock_connection_ready(True)

        conn.assert_upstream_verb(None)
Ejemplo n.º 11
0
    def test_session_verb_logging_3(self):
        conn = MockedConnection()
        chan = Channel(conn)

        with self.assertLogs(channel.logger, level='INFO'):
            conn.mock_downstream_verb(verbs.SessionVerb(
                name=b'name',
                state=verbs.SessionVerb.STATE_ENDED,
            ))
Ejemplo n.º 12
0
    def test_request_1(self):
        """ Test if a request verb is pushed when the connection is ready immediately.
        """

        conn = MockedConnection()
        chan = Channel(conn)

        conn.mock_connection_ready(True)

        chan.request('name', 'payload').send()

        conn.assert_upstream_verb(verbs.RequestVerb(
            name=b'name',
            unidirectional=True,
            messageref=None,
            timeout=5.0,
            payload=b'payload'
        ))
Ejemplo n.º 13
0
    def test_request_2(self):
        """ Test if the pushed request verb is unidirectional and has no messageref when no
        handlers were set.
        """

        conn = MockedConnection()
        chan = Channel(conn)

        conn.mock_connection_ready(True)

        req = chan.request('name', 'payload')
        req.send()

        conn.assert_upstream_verb(verbs.RequestVerb(
            name=b'name',
            unidirectional=True,
            messageref=None,
            timeout=5.0,
            payload=b'payload'
        ))
Ejemplo n.º 14
0
    def test_request_3(self):
        """ Test if the pushed request verb is directional and has a messsageref when a handler
        is set.
        """

        conn = MockedConnection()
        chan = Channel(conn)

        conn.mock_connection_ready(True)

        req = chan.request('name', 'payload')
        req.add_handler(Mock())
        req.send()

        conn.assert_upstream_verb(verbs.RequestVerb(
            name=b'name',
            unidirectional=False,
            messageref=1,
            timeout=5.0,
            payload=b'payload'
        ))
Ejemplo n.º 15
0
def create_channel(uri, loop=None):
    """ Creates Channel instance based on the given uri.
    If no mainloop is given a new Mainloop will be created.
    Returns a (mainloop, channel) tuple.
    """

    if not loop:
        loop = Mainloop()

    connection = create_connection(loop, uri)
    chan = Channel(connection)
    return loop, chan
Ejemplo n.º 16
0
    def test_backlog_different_ttls(self):
        """ Test if the backlog discards requests whos ttl have expired but keeps
        others.
        """

        conn = MockedConnection()
        chan = Channel(conn)

        with patch_time() as time:
            conn.mock_connection_ready(False)

            chan.request('name', 'payload0').send(ttl=2.0)
            chan.request('name', 'payload1').send(ttl=4.0)
            chan.request('name', 'payload2').send(ttl=2.0)
            chan.request('name', 'payload3').send(ttl=4.0)

            time.sleep(3.0)

            conn.mock_connection_ready(True)

        conn.assert_upstream_verb(verbs.RequestVerb(
            name=b'name',
            unidirectional=True,
            messageref=None,
            timeout=5.0,
            payload=b'payload1'
        ))

        conn.assert_upstream_verb(verbs.RequestVerb(
            name=b'name',
            unidirectional=True,
            messageref=None,
            timeout=5.0,
            payload=b'payload3'
        ))
Ejemplo n.º 17
0
    def test_subscribe_message_1(self):
        """ Test that the handler is called when a message is received for a subscription.
        """

        conn = MockedConnection()
        chan = Channel(conn)

        conn.mock_connection_ready(True)

        handler = unittest.mock.Mock()
        sub = chan.subscribe('name', 'topic')
        sub.add_handler(handler)

        conn.mock_downstream_verb(verbs.MessageVerb(
            messageref=1,
            status=verbs.MessageVerb.STATUS_OK,
            payload=b'payload1',
        ))

        self.__verify_handler_call(
            handler,
            Message,
            status=MessageStatus.OK,
            payload='payload1',
        )

        conn.mock_downstream_verb(verbs.MessageVerb(
            messageref=1,
            status=verbs.MessageVerb.STATUS_OK,
            payload=b'payload2',
        ))

        self.__verify_handler_call(
            handler,
            Message,
            status=MessageStatus.OK,
            payload='payload2',
        )
Ejemplo n.º 18
0
    def test_interest_connection_lost_1(self):
        """ Test if the interest is lost when the connection is lost.
        """

        conn = MockedConnection()
        chan = Channel(conn)

        conn.mock_connection_ready(True)

        session = chan.session('name')
        handler = Mock()
        session.add_interest_handler(handler)

        conn.mock_downstream_verb(verbs.InterestVerb(
            postref=1,
            name=b'name',
            status=verbs.InterestVerb.STATUS_INTEREST,
            topic=b'topic'
        ))

        self.__verify_handler_call(
            handler,
            Interest,
            status=InterestStatus.INTEREST,
            topic='topic',
            source=session,
        )

        conn.mock_connection_ready(False)

        self.__verify_handler_call(
            handler,
            Interest,
            status=InterestStatus.NO_INTEREST,
            topic='topic',
            source=session,
        )
Ejemplo n.º 19
0
    def test_request_4(self):
        """ Test if a request is still pushed when the connection becomes ready just before
        the ttl is expires.
        """

        conn = MockedConnection()
        chan = Channel(conn)

        with patch_time() as time:
            conn.mock_connection_ready(False)

            chan.request('name', 'payload').send(ttl=5.0)

            time.sleep(4.999)

            conn.mock_connection_ready(True)

            conn.assert_upstream_verb(verbs.RequestVerb(
                name=b'name',
                unidirectional=True,
                messageref=None,
                timeout=5.0,
                payload=b'payload'
            ))
Ejemplo n.º 20
0
    def test_post_1(self):
        """ Test if a post verb is pushed when posted and connection was ready.
        """

        conn = MockedConnection()
        chan = Channel(conn)

        conn.mock_connection_ready(True)

        post = Post(chan.core, postref=1, payload='payload', ttl=5.0)

        conn.assert_upstream_verb(verbs.PostVerb(
            postref=1,
            payload=b'payload',
        ))
Ejemplo n.º 21
0
    def test_call_post_warning(self):
        """ Test if a warning is logged when a post is attempted on an unidirectional call.
        """

        conn = MockedConnection()
        chan = Channel(conn)

        verb = verbs.CallVerb(
            unidirectional=True,
            postref=None,
            name=b'name',
            payload=b'payload'
        )

        call = Call(chan.core, verb, None)

        with self.assertLogs(channel.logger, level='WARNING'):
            call.post('payload')
Ejemplo n.º 22
0
    def test_interest_post_warning(self):
        """ Test if a warning is logged when a post is attempted on a lost interest.
        """

        conn = MockedConnection()
        chan = Channel(conn)

        verb = verbs.InterestVerb(
            postref=1,
            name=b'name',
            status=verbs.InterestVerb.STATUS_NO_INTEREST,
            topic=b'topic',
        )

        interest = Interest(chan.core, verb, None)

        with self.assertLogs(channel.logger, level='WARNING'):
            interest.post('payload')
Ejemplo n.º 23
0
    def test_post_3(self):
        """ Test if a post is still pushed when the connection becomes ready just before
        the ttl is expires.
        """

        conn = MockedConnection()
        chan = Channel(conn)

        with patch_time() as time:
            conn.mock_connection_ready(False)

            post = Post(chan.core, postref=1, payload='payload', ttl=5.0)

            time.sleep(4.999)

            conn.mock_connection_ready(True)

            conn.assert_upstream_verb(verbs.PostVerb(
                postref=1,
                payload=b'payload',
            ))
Ejemplo n.º 24
0
    def test_backlog_order(self):
        """ Test if the order of requests is preserved when queued in the backlog.
        """

        conn = MockedConnection()
        chan = Channel(conn)

        conn.mock_connection_ready(False)

        chan.request('name', 'payload0').send(ttl=5.0)
        chan.request('name', 'payload1').send(ttl=5.0)
        chan.request('name', 'payload2').send(ttl=5.0)

        conn.mock_connection_ready(True)

        conn.assert_upstream_verb(verbs.RequestVerb(
            name=b'name',
            unidirectional=True,
            messageref=None,
            timeout=5.0,
            payload=b'payload0'
        ))

        conn.assert_upstream_verb(verbs.RequestVerb(
            name=b'name',
            unidirectional=True,
            messageref=None,
            timeout=5.0,
            payload=b'payload1'
        ))

        conn.assert_upstream_verb(verbs.RequestVerb(
            name=b'name',
            unidirectional=True,
            messageref=None,
            timeout=5.0,
            payload=b'payload2'
        ))