示例#1
0
    def test_session_by_conn_str_receive_handler_with_autolockrenew(
            self, servicebus_namespace_connection_string, servicebus_queue,
            **kwargs):

        session_id = str(uuid.uuid4())
        queue_client = QueueClient.from_connection_string(
            servicebus_namespace_connection_string,
            name=servicebus_queue.name,
            debug=False)

        with queue_client.get_sender(session=session_id) as sender:
            for i in range(10):
                message = Message("{}".format(i))
                sender.send(message)

        renewer = AutoLockRenew()
        messages = []
        with queue_client.get_receiver(session=session_id,
                                       idle_timeout=5,
                                       mode=ReceiveSettleMode.PeekLock,
                                       prefetch=10) as session:
            renewer.register(session, timeout=60)
            print("Registered lock renew thread", session.locked_until,
                  datetime.now())
            with pytest.raises(SessionLockExpired):
                for message in session:
                    if not messages:
                        print("Starting first sleep")
                        time.sleep(40)
                        print("First sleep {}".format(session.locked_until -
                                                      datetime.now()))
                        assert not session.expired
                        with pytest.raises(TypeError):
                            message.expired
                        assert message.locked_until is None
                        with pytest.raises(TypeError):
                            message.renew_lock()
                        assert message.lock_token is None
                        message.complete()
                        messages.append(message)

                    elif len(messages) == 1:
                        print("Starting second sleep")
                        time.sleep(40)
                        print("Second sleep {}".format(session.locked_until -
                                                       datetime.now()))
                        assert session.expired
                        assert isinstance(session.auto_renew_error,
                                          AutoLockRenewTimeout)
                        try:
                            message.complete()
                            raise AssertionError(
                                "Didn't raise SessionLockExpired")
                        except SessionLockExpired as e:
                            assert isinstance(e.inner_exception,
                                              AutoLockRenewTimeout)
                        messages.append(message)

        renewer.shutdown()
        assert len(messages) == 2
def message_processing(sb_client, queue_name, messages):
    while True:
        try:
            with sb_client.get_queue_session_receiver(
                    queue_name, idle_timeout=1) as receiver:
                renewer = AutoLockRenew()
                renewer.register(receiver.session)
                receiver.session.set_session_state("OPEN")
                for message in receiver:
                    messages.append(message)
                    print("Message: {}".format(message))
                    print("Time to live: {}".format(
                        message.header.time_to_live))
                    print("Sequence number: {}".format(
                        message.sequence_number))
                    print("Enqueue Sequence number: {}".format(
                        message.enqueue_sequence_number))
                    print("Partition ID: {}".format(message.partition_id))
                    print("Partition Key: {}".format(message.partition_key))
                    print("Locked until: {}".format(message.locked_until_utc))
                    print("Lock Token: {}".format(message.lock_token))
                    print("Enqueued time: {}".format(
                        message.enqueued_time_utc))
                    message.complete()
                    if str(message) == 'shutdown':
                        receiver.session.set_session_state("CLOSED")
                renewer.shutdown()
        except NoActiveSession:
            print(
                "There are no non-empty sessions remaining; exiting.  This may present as a UserError in the azure portal."
            )
            return
def test_pqueue_by_queue_client_conn_str_receive_handler_with_autolockrenew(
        live_servicebus_config, partitioned_queue):
    queue_client = QueueClient.from_connection_string(
        live_servicebus_config['conn_str'],
        name=partitioned_queue,
        debug=False)

    with queue_client.get_sender() as sender:
        for i in range(10):
            message = Message("{}".format(i))
            sender.send(message)

    renewer = AutoLockRenew()
    messages = []
    with queue_client.get_receiver(idle_timeout=5,
                                   mode=ReceiveSettleMode.PeekLock,
                                   prefetch=10) as receiver:
        for message in receiver:
            if not messages:
                messages.append(message)
                assert not message.expired
                renewer.register(message, timeout=60)
                print("Registered lock renew thread", message.locked_until,
                      datetime.now())
                time.sleep(50)
                print("Finished first sleep", message.locked_until)
                assert not message.expired
                time.sleep(25)
                print("Finished second sleep", message.locked_until,
                      datetime.now())
                assert message.expired
                try:
                    message.complete()
                    raise AssertionError("Didn't raise MessageLockExpired")
                except MessageLockExpired as e:
                    assert isinstance(e.inner_exception, AutoLockRenewTimeout)
            else:
                if message.expired:
                    print("Remaining messages", message.locked_until,
                          datetime.now())
                    assert message.expired
                    with pytest.raises(MessageLockExpired):
                        message.complete()
                else:
                    assert message.header.delivery_count >= 1
                    print("Remaining messages", message.locked_until,
                          datetime.now())
                    messages.append(message)
                    message.complete()
    renewer.shutdown()
    assert len(messages) == 11
def test_session_by_conn_str_receive_handler_with_autolockrenew(live_servicebus_config, session_queue):
    session_id = str(uuid.uuid4())
    queue_client = QueueClient.from_connection_string(
        live_servicebus_config['conn_str'],
        name=session_queue,
        debug=True)

    with queue_client.get_sender(session=session_id) as sender:
        for i in range(10):
            message = Message("{}".format(i))
            sender.send(message)

    renewer = AutoLockRenew()
    messages = []
    with queue_client.get_receiver(session=session_id, idle_timeout=5, mode=ReceiveSettleMode.PeekLock, prefetch=10) as session:
        renewer.register(session, timeout=60)
        print("Registered lock renew thread", session.locked_until, datetime.now())
        with pytest.raises(SessionLockExpired):
            for message in session:
                if not messages:
                    print("Starting first sleep")
                    time.sleep(40)
                    print("First sleep {}".format(session.locked_until - datetime.now()))
                    assert not session.expired
                    with pytest.raises(TypeError):
                        message.expired
                    assert message.locked_until is None
                    with pytest.raises(TypeError):
                        message.renew_lock()
                    assert message.lock_token is None
                    message.complete()
                    messages.append(message)

                elif len(messages) == 1:
                    print("Starting second sleep")
                    time.sleep(40)
                    print("Second sleep {}".format(session.locked_until - datetime.now()))
                    assert session.expired
                    assert isinstance(session.auto_renew_error, AutoLockRenewTimeout)
                    try:
                        message.complete()
                        raise AssertionError("Didn't raise SessionLockExpired")
                    except SessionLockExpired as e:
                        assert isinstance(e.inner_exception, AutoLockRenewTimeout)
                    messages.append(message)

    renewer.shutdown()
    assert len(messages) == 2
示例#5
0
    def test_session_by_conn_str_receive_handler_with_autolockrenew(self, servicebus_namespace_connection_string, servicebus_queue, **kwargs):

        session_id = str(uuid.uuid4())
        with ServiceBusClient.from_connection_string(
            servicebus_namespace_connection_string, logging_enable=False) as sb_client:

            with sb_client.get_queue_sender(servicebus_queue.name) as sender:
                for i in range(10):
                    message = Message("{}".format(i), session_id=session_id)
                    sender.send(message)

            renewer = AutoLockRenew()
            messages = []
            with sb_client.get_queue_session_receiver(servicebus_queue.name, session_id=session_id, idle_timeout=5, mode=ReceiveSettleMode.PeekLock, prefetch=10) as receiver:
                renewer.register(receiver.session, timeout=60)
                print("Registered lock renew thread", receiver.session._locked_until_utc, utc_now())
                with pytest.raises(SessionLockExpired):
                    for message in receiver:
                        if not messages:
                            print("Starting first sleep")
                            time.sleep(40)
                            print("First sleep {}".format(receiver.session._locked_until_utc - utc_now()))
                            assert not receiver.session.expired
                            with pytest.raises(TypeError):
                                message.expired
                            assert message.locked_until_utc is None
                            with pytest.raises(TypeError):
                                message.renew_lock()
                            assert message.lock_token is not None
                            message.complete()
                            messages.append(message)

                        elif len(messages) == 1:
                            print("Starting second sleep")
                            time.sleep(40) # ensure renewer expires
                            print("Second sleep {}".format(receiver.session._locked_until_utc - utc_now()))
                            sleep_until_expired(receiver.session) # and then ensure it didn't slip a renew under the wire.
                            assert receiver.session.expired
                            assert isinstance(receiver.session.auto_renew_error, AutoLockRenewTimeout)
                            try:
                                message.complete()
                                raise AssertionError("Didn't raise SessionLockExpired")
                            except SessionLockExpired as e:
                                assert isinstance(e.inner_exception, AutoLockRenewTimeout)
                            messages.append(message)

            renewer.shutdown()
            assert len(messages) == 2
def test_pqueue_by_queue_client_conn_str_receive_handler_with_autolockrenew(live_servicebus_config, partitioned_queue):
    queue_client = QueueClient.from_connection_string(
        live_servicebus_config['conn_str'],
        name=partitioned_queue,
        debug=True)

    with queue_client.get_sender() as sender:
        for i in range(10):
            message = Message("{}".format(i))
            sender.send(message)

    renewer = AutoLockRenew()
    messages = []
    with queue_client.get_receiver(idle_timeout=5, mode=ReceiveSettleMode.PeekLock, prefetch=10) as receiver:
        for message in receiver:
            if not messages:
                messages.append(message)
                assert not message.expired
                renewer.register(message, timeout=60)
                print("Registered lock renew thread", message.locked_until, datetime.now())
                time.sleep(50)
                print("Finished first sleep", message.locked_until)
                assert not message.expired
                time.sleep(25)
                print("Finished second sleep", message.locked_until, datetime.now())
                assert message.expired
                try:
                    message.complete()
                    raise AssertionError("Didn't raise MessageLockExpired")
                except MessageLockExpired as e:
                    assert isinstance(e.inner_exception, AutoLockRenewTimeout)
            else:
                if message.expired:
                    print("Remaining messages", message.locked_until, datetime.now())
                    assert message.expired
                    with pytest.raises(MessageLockExpired):
                        message.complete()
                else:
                    assert message.header.delivery_count >= 1
                    print("Remaining messages", message.locked_until, datetime.now())
                    messages.append(message)
                    message.complete()
    renewer.shutdown()
    assert len(messages) == 11
示例#7
0
def renew_lock_on_session_of_the_sessionful_entity():
    servicebus_client = ServiceBusClient.from_connection_string(
        conn_str=CONNECTION_STR)

    with servicebus_client:

        with servicebus_client.get_queue_sender(
                queue_name=SESSION_QUEUE_NAME) as sender:
            msgs_to_send = [
                Message("session message: {}".format(i), session_id='SESSION')
                for i in range(10)
            ]
            sender.send(msgs_to_send)
            print('Send messages to sessionful queue.')

        renewer = AutoLockRenew()

        with servicebus_client.get_queue_session_receiver(
                queue_name=SESSION_QUEUE_NAME, session_id='SESSION',
                prefetch=10) as receiver:

            # automatically renew the lock on the session for 100 seconds
            renewer.register(receiver.session, timeout=100)
            print('Register session into AutoLockRenew.')

            received_msgs = receiver.receive(max_batch_size=10,
                                             max_wait_time=5)
            time.sleep(
                100
            )  # message handling for long period (E.g. application logic)

            for msg in received_msgs:
                msg.complete()

            print('Complete messages.')

        renewer.shutdown()
示例#8
0
def renew_lock_on_message_received_from_non_sessionful_entity():
    servicebus_client = ServiceBusClient.from_connection_string(
        conn_str=CONNECTION_STR)

    with servicebus_client:
        with servicebus_client.get_queue_sender(
                queue_name=QUEUE_NAME) as sender:
            msgs_to_send = [
                Message("message: {}".format(i)) for i in range(10)
            ]
            sender.send(msgs_to_send)
            print('Send messages to non-sessionful queue.')

        # Can also be called via "with AutoLockRenew() as renewer" to automate shutdown.
        renewer = AutoLockRenew()

        with servicebus_client.get_queue_receiver(queue_name=QUEUE_NAME,
                                                  prefetch=10) as receiver:
            received_msgs = receiver.receive(max_batch_size=10,
                                             max_wait_time=5)

            for msg in received_msgs:
                # automatically renew the lock on each message for 100 seconds
                renewer.register(msg, timeout=100)
            print('Register messages into AutoLockRenew done.')

            time.sleep(
                100
            )  # message handling for long period (E.g. application logic)

            for msg in received_msgs:
                msg.complete(
                )  # Settling the message deregisters it from the AutoLockRenewer
            print('Complete messages.')

        renewer.shutdown()