async def test_async_session_message_expiry(self, servicebus_namespace_connection_string, servicebus_queue, **kwargs):
        async with ServiceBusClient.from_connection_string(
            servicebus_namespace_connection_string, logging_enable=False) as sb_client:

            session_id = str(uuid.uuid4())

            async with sb_client.get_queue_sender(servicebus_queue.name) as sender:
                message = Message("Testing expired messages")
                message.session_id = session_id
                await sender.send_messages(message)

            async with sb_client.get_queue_session_receiver(servicebus_queue.name, session_id=session_id) as receiver:
                messages = await receiver.receive_messages(max_wait_time=10)
                assert len(messages) == 1
                print_message(_logger, messages[0])
                await asyncio.sleep(60) #TODO: Was 30, but then lock isn't expired.
                with pytest.raises(TypeError):
                    messages[0]._lock_expired
                with pytest.raises(TypeError):
                    await messages[0].renew_lock()
                assert receiver.session._lock_expired
                with pytest.raises(SessionLockExpired):
                    await messages[0].complete()
                with pytest.raises(SessionLockExpired):
                    await receiver.session.renew_lock()

            async with sb_client.get_queue_session_receiver(servicebus_queue.name, session_id=session_id) as receiver:
                messages = await receiver.receive_messages(max_wait_time=30)
                assert len(messages) == 1
                print_message(_logger, messages[0])
                assert messages[0].delivery_count
                await messages[0].complete()
    def test_session_by_session_client_conn_str_receive_handler_peeklock(
            self, servicebus_namespace_connection_string, servicebus_queue,
            **kwargs):
        with ServiceBusClient.from_connection_string(
                servicebus_namespace_connection_string,
                logging_enable=False) as sb_client:

            session_id = str(uuid.uuid4())
            with sb_client.get_queue_sender(servicebus_queue.name) as sender:
                for i in range(3):
                    message = Message("Handler message no. {}".format(i),
                                      session_id=session_id)
                    sender.send(message)

            with pytest.raises(ServiceBusConnectionError):
                session = sb_client.get_queue_receiver(
                    servicebus_queue.name, idle_timeout=5)._open_with_retry()

            with sb_client.get_queue_receiver(servicebus_queue.name,
                                              session_id=session_id,
                                              idle_timeout=5) as session:
                count = 0
                for message in session:
                    print_message(_logger, message)
                    assert message.session_id == session_id
                    count += 1
                    message.complete()

                assert count == 3
Пример #3
0
    def test_session_message_expiry(self, servicebus_namespace_connection_string, servicebus_queue, **kwargs):

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

            with sb_client.get_queue_sender(servicebus_queue.name) as sender:
                message = Message("Testing expired messages")
                message.session_id = session_id
                sender.send(message)

            with sb_client.get_queue_session_receiver(servicebus_queue.name, session_id=session_id) as receiver:
                messages = receiver.receive(max_wait_time=10)
                assert len(messages) == 1
                print_message(_logger, messages[0])
                time.sleep(60)
                with pytest.raises(TypeError):
                    messages[0].expired
                with pytest.raises(TypeError):
                    messages[0].renew_lock()
                    #TODO: Bug: Why was this 30s sleep before?  compare with T1.
                assert receiver.session.expired
                with pytest.raises(SessionLockExpired):
                    messages[0].complete()
                with pytest.raises(SessionLockExpired):
                    receiver.session.renew_lock()

            with sb_client.get_queue_session_receiver(servicebus_queue.name, session_id=session_id) as receiver:
                messages = receiver.receive(max_wait_time=30)
                assert len(messages) == 1
                print_message(_logger, messages[0])
                #assert messages[0].header.delivery_count  # TODO confirm this with service
                messages[0].complete()
    async def test_async_session_schedule_message(self, servicebus_namespace_connection_string, servicebus_queue, **kwargs):
        async with ServiceBusClient.from_connection_string(
            servicebus_namespace_connection_string, logging_enable=False) as sb_client:
            import uuid
            session_id = str(uuid.uuid4())
            enqueue_time = (utc_now() + timedelta(minutes=2)).replace(microsecond=0)
            async with sb_client.get_queue_sender(servicebus_queue.name) as sender:
                content = str(uuid.uuid4())
                message_id = uuid.uuid4()
                message = Message(content, session_id=session_id)
                message.message_id = message_id
                message.scheduled_enqueue_time_utc = enqueue_time
                await sender.send_messages(message)

            messages = []
            renewer = AutoLockRenew()
            async with sb_client.get_queue_session_receiver(servicebus_queue.name, session_id=session_id) as receiver:
                renewer.register(receiver.session, timeout=140)
                messages.extend(await receiver.receive_messages(max_wait_time=120))
                messages.extend(await receiver.receive_messages(max_wait_time=5))
                if messages:
                    data = str(messages[0])
                    assert data == content
                    assert messages[0].message_id == message_id
                    assert messages[0].scheduled_enqueue_time_utc == enqueue_time
                    assert messages[0].scheduled_enqueue_time_utc == messages[0].enqueued_time_utc.replace(microsecond=0)
                    assert len(messages) == 1
                else:
                    raise Exception("Failed to receive schdeduled message.")
            await renewer.close()
    async def test_async_session_cancel_scheduled_messages(self, servicebus_namespace_connection_string, servicebus_queue, **kwargs):
        async with ServiceBusClient.from_connection_string(
            servicebus_namespace_connection_string, logging_enable=False) as sb_client:

            session_id = str(uuid.uuid4())
            enqueue_time = (utc_now() + timedelta(minutes=2)).replace(microsecond=0)
            async with sb_client.get_queue_sender(servicebus_queue.name) as sender:
                message_a = Message("Test scheduled message", session_id=session_id)
                message_b = Message("Test scheduled message", session_id=session_id)
                tokens = await sender.schedule_messages([message_a, message_b], enqueue_time)
                assert len(tokens) == 2
                await sender.cancel_scheduled_messages(tokens)

            renewer = AutoLockRenew()
            messages = []
            async with sb_client.get_queue_session_receiver(servicebus_queue.name, session_id=session_id) as receiver:
                renewer.register(receiver.session, timeout=140)
                messages.extend(await receiver.receive_messages(max_wait_time=120))
                messages.extend(await receiver.receive_messages(max_wait_time=5))
                try:
                    assert len(messages) == 0
                except AssertionError:
                    for m in messages:
                        print(str(m))
                        await m.complete()
                    raise
            await renewer.close()
    def test_session_cancel_scheduled_messages(
            self, servicebus_namespace_connection_string, servicebus_queue,
            **kwargs):

        with ServiceBusClient.from_connection_string(
                servicebus_namespace_connection_string,
                logging_enable=False) as sb_client:

            session_id = str(uuid.uuid4())
            enqueue_time = (utc_now() +
                            timedelta(minutes=2)).replace(microsecond=0)

            with sb_client.get_queue_sender(servicebus_queue.name) as sender:
                message_a = Message("Test scheduled message",
                                    session_id=session_id)
                message_b = Message("Test scheduled message",
                                    session_id=session_id)
                tokens = sender.schedule(enqueue_time, message_a, message_b)
                assert len(tokens) == 2
                sender.cancel_scheduled_messages(*tokens)

            with sb_client.get_queue_receiver(
                    servicebus_queue.name, session_id=session_id) as receiver:
                messages = []
                count = 0
                while not messages and count < 13:
                    messages = receiver.receive(max_wait_time=10)
                    receiver.session.renew_lock()
                    count += 1
                assert len(messages) == 0
    async def test_async_session_schedule_multiple_messages(self, servicebus_namespace_connection_string, servicebus_queue, **kwargs):
        async with ServiceBusClient.from_connection_string(
            servicebus_namespace_connection_string, logging_enable=False) as sb_client:
            import uuid
            session_id = str(uuid.uuid4())
            enqueue_time = (utc_now() + timedelta(minutes=2)).replace(microsecond=0)
            messages = []
            async with sb_client.get_queue_sender(servicebus_queue.name) as sender:
                content = str(uuid.uuid4())
                message_id_a = uuid.uuid4()
                message_a = Message(content, session_id=session_id)
                message_a.properties.message_id = message_id_a
                message_id_b = uuid.uuid4()
                message_b = Message(content, session_id=session_id)
                message_b.properties.message_id = message_id_b
                tokens = await sender.schedule_messages([message_a, message_b], enqueue_time)
                assert len(tokens) == 2

            renewer = AutoLockRenew()
            async with sb_client.get_queue_session_receiver(servicebus_queue.name, session_id=session_id, prefetch=20) as receiver:
                renewer.register(receiver.session, timeout=140)
                messages.extend(await receiver.receive_messages(max_wait_time=120))
                messages.extend(await receiver.receive_messages(max_wait_time=5))
                if messages:
                    data = str(messages[0])
                    assert data == content
                    assert messages[0].properties.message_id in (message_id_a, message_id_b)
                    assert messages[0].scheduled_enqueue_time_utc == enqueue_time
                    assert messages[0].scheduled_enqueue_time_utc == messages[0].enqueued_time_utc.replace(microsecond=0)
                    assert len(messages) == 2
                else:
                    raise Exception("Failed to receive schdeduled message.")
            await renewer.shutdown()
Пример #8
0
    async def test_async_session_by_servicebus_client_browse_messages_with_receiver(
            self, servicebus_namespace_connection_string, servicebus_queue,
            **kwargs):
        async with ServiceBusClient.from_connection_string(
                servicebus_namespace_connection_string,
                logging_enable=False) as sb_client:

            session_id = str(uuid.uuid4())
            async with sb_client.get_queue_session_receiver(
                    servicebus_queue.name, idle_timeout=5,
                    session_id=session_id) as receiver:
                async with sb_client.get_queue_sender(
                        servicebus_queue.name) as sender:
                    for i in range(5):
                        message = Message("Test message no. {}".format(i),
                                          session_id=session_id)
                        await sender.send(message)

                messages = await receiver.peek(5)
                assert len(messages) > 0
                assert all(isinstance(m, PeekMessage) for m in messages)
                for message in messages:
                    print_message(_logger, message)
                    with pytest.raises(AttributeError):
                        message.complete()
Пример #9
0
    def test_session_schedule_multiple_messages(self, servicebus_namespace_connection_string, servicebus_queue, **kwargs):

        with ServiceBusClient.from_connection_string(
            servicebus_namespace_connection_string, logging_enable=False) as sb_client:

            session_id = str(uuid.uuid4())
            enqueue_time = (utc_now() + timedelta(minutes=2)).replace(microsecond=0)

            with sb_client.get_queue_session_receiver(servicebus_queue.name, session_id=session_id, prefetch=20) as receiver:
                with sb_client.get_queue_sender(servicebus_queue.name) as sender:
                    content = str(uuid.uuid4())
                    message_id_a = uuid.uuid4()
                    message_a = Message(content, session_id=session_id)
                    message_a.properties.message_id = message_id_a
                    message_id_b = uuid.uuid4()
                    message_b = Message(content, session_id=session_id)
                    message_b.properties.message_id = message_id_b
                    tokens = sender.schedule(enqueue_time, message_a, message_b)
                    assert len(tokens) == 2

                messages = []
                count = 0
                while len(messages) < 2 and count < 12:
                    receiver.session.renew_lock()
                    messages = receiver.receive(max_wait_time=15)
                    time.sleep(5)
                    count += 1

                data = str(messages[0])
                assert data == content
                assert messages[0].properties.message_id in (message_id_a, message_id_b)
                assert messages[0].scheduled_enqueue_time_utc == enqueue_time
                assert messages[0].scheduled_enqueue_time_utc == messages[0].enqueued_time_utc.replace(microsecond=0)
                assert len(messages) == 2
Пример #10
0
    def test_sb_client_incorrect_queue_conn_str(self, servicebus_queue_authorization_rule_connection_string, servicebus_queue, wrong_queue, **kwargs):
        
        client = ServiceBusClient.from_connection_string(servicebus_queue_authorization_rule_connection_string)
        with client:
            # Validate that the wrong queue with the right credentials fails.
            with pytest.raises(ServiceBusAuthenticationError):
                with client.get_queue_sender(wrong_queue.name) as sender:
                    sender.send_messages(Message("test"))

            # But that the correct one works.
            with client.get_queue_sender(servicebus_queue.name) as sender:
                sender.send_messages(Message("test")) 

            # Now do the same but with direct connstr initialization.
            with pytest.raises(ServiceBusAuthenticationError):
                with ServiceBusSender.from_connection_string(
                    servicebus_queue_authorization_rule_connection_string,
                    queue_name=wrong_queue.name,
                ) as sender:
                    sender.send_messages(Message("test"))

            with ServiceBusSender.from_connection_string(
                servicebus_queue_authorization_rule_connection_string,
                queue_name=servicebus_queue.name,
            ) as sender:
                sender.send_messages(Message("test"))
Пример #11
0
    def test_session_schedule_message(self, servicebus_namespace_connection_string, servicebus_queue, **kwargs):

        with ServiceBusClient.from_connection_string(
            servicebus_namespace_connection_string, logging_enable=False) as sb_client:

            session_id = str(uuid.uuid4())
            enqueue_time = (utc_now() + timedelta(minutes=2)).replace(microsecond=0)
            with sb_client.get_queue_session_receiver(servicebus_queue.name, session_id=session_id) as receiver:
                with sb_client.get_queue_sender(servicebus_queue.name) as sender:
                    content = str(uuid.uuid4())
                    message_id = uuid.uuid4()
                    message = Message(content, session_id=session_id)
                    message.properties.message_id = message_id
                    message.schedule(enqueue_time)
                    sender.send(message)

                messages = []
                count = 0
                while not messages and count < 12:
                    messages = receiver.receive(max_wait_time=10)
                    receiver.session.renew_lock()
                    count += 1

                data = str(messages[0])
                assert data == content
                assert messages[0].properties.message_id == message_id
                assert messages[0].scheduled_enqueue_time_utc == enqueue_time
                assert messages[0].scheduled_enqueue_time_utc == messages[0].enqueued_time_utc.replace(microsecond=0)
                assert len(messages) == 1
Пример #12
0
    def test_session_by_queue_client_conn_str_receive_handler_receiveanddelete(self, servicebus_namespace_connection_string, servicebus_queue, **kwargs):
        with ServiceBusClient.from_connection_string(
            servicebus_namespace_connection_string, logging_enable=False) as sb_client:

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

            messages = []
            with sb_client.get_queue_session_receiver(servicebus_queue.name, 
                                              session_id=session_id, 
                                              mode=ReceiveSettleMode.ReceiveAndDelete, 
                                              idle_timeout=5) as session:
                for message in session:
                    messages.append(message)
                    assert session_id == session._session_id
                    assert session_id == message.session_id
                    with pytest.raises(MessageAlreadySettled):
                        message.complete()

            assert not session._running
            assert len(messages) == 10
            time.sleep(30)

            messages = []
            with sb_client.get_queue_session_receiver(servicebus_queue.name, session_id=session_id, mode=ReceiveSettleMode.ReceiveAndDelete, idle_timeout=5) as session:
                for message in session:
                    messages.append(message)
                assert len(messages) == 0
    async def test_async_session_connection_failure_is_idempotent(self, servicebus_namespace_connection_string, servicebus_queue, **kwargs):
        #Technically this validates for all senders/receivers, not just session, but since it uses session to generate a recoverable failure, putting it in here.
        async with ServiceBusClient.from_connection_string(
            servicebus_namespace_connection_string, logging_enable=False) as sb_client:
    
            # First let's just try the naive failure cases.
            receiver = sb_client.get_queue_receiver("THIS_IS_WRONG_ON_PURPOSE")
            with pytest.raises(ServiceBusAuthenticationError):
                await receiver._open_with_retry()
            assert not receiver._running
            assert not receiver._handler
    
            sender = sb_client.get_queue_sender("THIS_IS_WRONG_ON_PURPOSE")
            with pytest.raises(ServiceBusAuthenticationError):
                await sender._open_with_retry()
            assert not receiver._running
            assert not receiver._handler

            # Then let's try a case we can recover from to make sure everything works on reestablishment.
            receiver = sb_client.get_queue_session_receiver(servicebus_queue.name, session_id=NEXT_AVAILABLE)
            with pytest.raises(NoActiveSession):
                await receiver._open_with_retry()

            session_id = str(uuid.uuid4())
            async with sb_client.get_queue_sender(servicebus_queue.name) as sender:
                await sender.send_messages(Message("test session sender", session_id=session_id))

            async with sb_client.get_queue_session_receiver(servicebus_queue.name, session_id=NEXT_AVAILABLE, max_wait_time=5) as receiver:
                messages = []
                async for message in receiver:
                    messages.append(message)
                assert len(messages) == 1
    async def test_async_session_by_servicebus_client_session_pool(self, servicebus_namespace_connection_string, servicebus_queue, **kwargs):
        
        messages = []
        errors = []
        async def message_processing(sb_client):
            while True:
                try:
                    async with sb_client.get_queue_session_receiver(servicebus_queue.name, session_id=NEXT_AVAILABLE, max_wait_time=5) as session:
                        async for message in session:
                            print("Message: {}".format(message))
                            messages.append(message)
                            await message.complete()
                except NoActiveSession:
                    return
                except Exception as e:
                    errors.append(e)
                    raise

        concurrent_receivers = 5
        sessions = [str(uuid.uuid4()) for i in range(concurrent_receivers)]
        async with ServiceBusClient.from_connection_string(
            servicebus_namespace_connection_string, logging_enable=False) as sb_client:

            for session_id in sessions:
                async with sb_client.get_queue_sender(servicebus_queue.name) as sender:
                    await asyncio.gather(*[sender.send_messages(Message("Sample message no. {}".format(i), session_id=session_id)) for i in range(20)])

            receive_sessions = [message_processing(sb_client) for _ in range(concurrent_receivers)]
            await asyncio.gather(*receive_sessions, return_exceptions=True)

            assert not errors
            assert len(messages) == 100
    async def test_async_session_by_servicebus_client_fetch_next_with_retrieve_deadletter(self, servicebus_namespace_connection_string, servicebus_queue, **kwargs):
        async with ServiceBusClient.from_connection_string(
            servicebus_namespace_connection_string, logging_enable=False) as sb_client:

            session_id = str(uuid.uuid4())
            async with sb_client.get_queue_session_receiver(servicebus_queue.name, session_id=session_id, max_wait_time=5, prefetch_count=10) as receiver:

                async with sb_client.get_queue_sender(servicebus_queue.name) as sender:
                    for i in range(10):
                        message = Message("Dead lettered message no. {}".format(i), session_id=session_id)
                        await sender.send_messages(message)

                count = 0
                messages = await receiver.receive_messages()
                while messages:
                    for message in messages:
                        print_message(_logger, message)
                        await message.dead_letter(reason="Testing reason", error_description="Testing description")
                        count += 1
                    messages = await receiver.receive_messages()
            assert count == 10

            async with sb_client.get_queue_receiver(servicebus_queue.name, 
                                                    sub_queue = SubQueue.DeadLetter,
                                                    max_wait_time=5) as session:
                count = 0
                async for message in session:
                    print_message(_logger, message)
                    assert message.dead_letter_reason == 'Testing reason'
                    assert message.dead_letter_error_description == 'Testing description'
                    assert message.properties[b'DeadLetterReason'] == b'Testing reason'
                    assert message.properties[b'DeadLetterErrorDescription'] == b'Testing description'
                    await message.complete()
                    count += 1
            assert count == 10
    async def test_async_session_by_servicebus_client_iter_messages_with_retrieve_deferred_receiver_complete(self, servicebus_namespace_connection_string, servicebus_queue, **kwargs):
        async with ServiceBusClient.from_connection_string(
            servicebus_namespace_connection_string, logging_enable=False) as sb_client:

            deferred_messages = []
            session_id = str(uuid.uuid4())
            async with sb_client.get_queue_sender(servicebus_queue.name) as sender:
                for message in [Message("Deferred message no. {}".format(i), session_id=session_id) for i in range(10)]:
                    await sender.send_messages(message)

            count = 0
            async with sb_client.get_queue_session_receiver(servicebus_queue.name, session_id=session_id, max_wait_time=5) as session:
                async for message in session:
                    deferred_messages.append(message.sequence_number)
                    print_message(_logger, message)
                    count += 1
                    await message.defer()

            assert count == 10

            async with sb_client.get_queue_session_receiver(servicebus_queue.name, session_id=session_id, max_wait_time=5) as session:
                deferred = await session.receive_deferred_messages(deferred_messages)
                assert len(deferred) == 10
                for message in deferred:
                    assert isinstance(message, ReceivedMessage)
                    assert message.lock_token
                    assert not message.locked_until_utc
                    assert message._receiver
                    with pytest.raises(TypeError):
                        await message.renew_lock()
                    await message.complete()
    async def test_async_session_by_session_client_conn_str_receive_handler_with_stop(self, servicebus_namespace_connection_string, servicebus_queue, **kwargs):
        async with ServiceBusClient.from_connection_string(
            servicebus_namespace_connection_string, logging_enable=False) as sb_client:

            session_id = str(uuid.uuid4())
            async with sb_client.get_queue_sender(servicebus_queue.name) as sender:
                for i in range(10):
                    message = Message("Stop message no. {}".format(i), session_id=session_id)
                    await sender.send_messages(message)

            messages = []
            session = sb_client.get_queue_session_receiver(servicebus_queue.name, session_id=session_id, max_wait_time=5)
            async with session:
                async for message in session:
                    assert session_id == session.session.session_id
                    assert session_id == message.session_id
                    messages.append(message)
                    await message.complete()
                    if len(messages) >= 5:
                        break

                assert session._running
                assert len(messages) == 5

            async with session:
                async for message in session:
                    assert session_id == session.session.session_id
                    assert session_id == message.session_id
                    messages.append(message)
                    await message.complete()
                    if len(messages) >= 5:
                        break

            assert not session._running
            assert len(messages) == 6
Пример #18
0
    async def test_async_session_by_servicebus_client_list_sessions_with_client(
            self, servicebus_namespace_connection_string, servicebus_queue,
            **kwargs):
        async with ServiceBusClient.from_connection_string(
                servicebus_namespace_connection_string,
                logging_enable=False) as sb_client:

            sessions = []
            start_time = utc_now()
            for i in range(5):
                sessions.append(str(uuid.uuid4()))

            for session in sessions:
                async with sb_client.get_queue_sender(
                        servicebus_queue.name) as sender:
                    for i in range(5):
                        message = Message("Test message no. {}".format(i),
                                          session_id=session)
                        await sender.send(message)
            for session in sessions:
                async with sb_client.get_queue_session_receiver(
                        servicebus_queue.name, session_id=session) as receiver:
                    await receiver.session.set_session_state(
                        "SESSION {}".format(session))

            current_sessions = await sb_client.list_sessions(
                updated_since=start_time)
            assert len(current_sessions) == 5
            assert current_sessions == sessions
Пример #19
0
    async def test_async_session_get_set_state_with_receiver(
            self, servicebus_namespace_connection_string, servicebus_queue,
            **kwargs):
        async with ServiceBusClient.from_connection_string(
                servicebus_namespace_connection_string,
                logging_enable=False) as sb_client:

            session_id = str(uuid.uuid4())
            async with sb_client.get_queue_sender(
                    servicebus_queue.name) as sender:
                for i in range(3):
                    message = Message("Handler message no. {}".format(i),
                                      session_id=session_id)
                    await sender.send(message)

            async with sb_client.get_queue_session_receiver(
                    servicebus_queue.name, session_id=session_id,
                    idle_timeout=5) as session:
                assert await session.session.get_session_state() == None
                await session.session.set_session_state("first_state")
                count = 0
                async for m in session:
                    assert m.properties.group_id == session_id.encode('utf-8')
                    count += 1
                await session.session.get_session_state()
            assert count == 3
Пример #20
0
    def test_session_by_servicebus_client_iter_messages_with_retrieve_deferred_receiver_deletemode(self, servicebus_namespace_connection_string, servicebus_queue, **kwargs):
        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:
                deferred_messages = []
                session_id = str(uuid.uuid4())
                messages = [Message("Deferred message no. {}".format(i), session_id=session_id) for i in range(10)]
                for message in messages:
                    sender.send(message)

            count = 0
            with sb_client.get_queue_session_receiver(servicebus_queue.name, session_id=session_id, idle_timeout=5) as session:
                for message in session:
                    deferred_messages.append(message.sequence_number)
                    print_message(_logger, message)
                    count += 1
                    message.defer()

            assert count == 10
            with sb_client.get_queue_session_receiver(servicebus_queue.name, 
                                              session_id=session_id, 
                                              idle_timeout=5,
                                              mode=ReceiveSettleMode.ReceiveAndDelete) as session:
                deferred = session.receive_deferred_messages(deferred_messages)
                assert len(deferred) == 10
                for message in deferred:
                    assert isinstance(message, ReceivedMessage)
                    with pytest.raises(MessageAlreadySettled):
                        message.complete()
                with pytest.raises(ServiceBusError):
                    deferred = session.receive_deferred_messages(deferred_messages)
    async def test_async_session_by_servicebus_client_renew_client_locks(self, servicebus_namespace_connection_string, servicebus_queue, **kwargs):
        async with ServiceBusClient.from_connection_string(
            servicebus_namespace_connection_string, logging_enable=False) as sb_client:

            session_id = str(uuid.uuid4())
            messages = []
            locks = 3
            async with sb_client.get_queue_session_receiver(servicebus_queue.name, session_id=session_id, prefetch_count=10) as receiver:
                async with sb_client.get_queue_sender(servicebus_queue.name) as sender:
                    for i in range(locks):
                        message = Message("Test message no. {}".format(i), session_id=session_id)
                        await sender.send_messages(message)

                messages.extend(await receiver.receive_messages())
                recv = True
                while recv:
                    recv = await receiver.receive_messages(max_wait_time=5)
                    messages.extend(recv)

                try:
                    for m in messages:
                        with pytest.raises(TypeError):
                            expired = m._lock_expired
                        assert m.locked_until_utc is None
                        assert m.lock_token is not None
                    time.sleep(5)
                    initial_expiry = receiver.session.locked_until_utc
                    await receiver.session.renew_lock()
                    assert (receiver.session.locked_until_utc - initial_expiry) >= timedelta(seconds=5)
                finally:
                    await messages[0].complete()
                    await messages[1].complete()
                    time.sleep(70) #TODO: BUG: Was 40
                    with pytest.raises(SessionLockExpired):
                        await messages[2].complete()
Пример #22
0
    async def test_async_session_by_servicebus_client_iter_messages_with_retrieve_deferred_client(
            self, servicebus_namespace_connection_string, servicebus_queue,
            **kwargs):
        async with ServiceBusClient.from_connection_string(
                servicebus_namespace_connection_string,
                logging_enable=False) as sb_client:

            deferred_messages = []
            session_id = str(uuid.uuid4())
            async with sb_client.get_queue_sender(
                    servicebus_queue.name) as sender:
                for i in range(10):
                    message = Message("Deferred message no. {}".format(i),
                                      session_id=session_id)
                    await sender.send(message)

            session = sb_client.get_queue_session_receiver(
                servicebus_queue.name, session_id=session_id, idle_timeout=5)
            count = 0
            async for message in session:
                deferred_messages.append(message.sequence_number)
                print_message(_logger, message)
                count += 1
                await message.defer()

            assert count == 10

            with pytest.raises(MessageSettleFailed):
                await message.complete()
    def test_session_by_servicebus_client_list_sessions_with_receiver(
            self, servicebus_namespace_connection_string, servicebus_queue,
            **kwargs):

        with ServiceBusClient.from_connection_string(
                servicebus_namespace_connection_string,
                logging_enable=False) as sb_client:

            sessions = []
            start_time = utc_now()
            for i in range(5):
                sessions.append(str(uuid.uuid4()))

            for session_id in sessions:
                with sb_client.get_queue_sender(
                        servicebus_queue.name) as sender:
                    for i in range(5):
                        message = Message("Test message no. {}".format(i),
                                          session_id=session_id)
                        sender.send(message)
            for session_id in sessions:
                with sb_client.get_queue_receiver(
                        servicebus_queue.name,
                        session_id=session_id) as receiver:
                    receiver.set_session_state("SESSION {}".format(session_id))

            with sb_client.get_queue_receiver(
                    servicebus_queue.name,
                    session_id=NEXT_AVAILABLE,
                    idle_timeout=5,
                    mode=ReceiveSettleMode.PeekLock) as receiver:
                current_sessions = receiver.list_sessions(
                    updated_since=start_time)
                assert len(current_sessions) == 5
                assert current_sessions == sessions
 def test_sb_client_incorrect_queue_conn_str(self, servicebus_queue_authorization_rule_connection_string, wrong_queue, **kwargs):
     
     client = ServiceBusClient.from_connection_string(servicebus_queue_authorization_rule_connection_string)
     with client:
         with pytest.raises(ServiceBusError):
             with client.get_queue_sender(wrong_queue.name) as sender:
                 sender.send_messages(Message("test"))
Пример #25
0
    def test_client_sas_credential(self,
                                   servicebus_queue,
                                   servicebus_namespace,
                                   servicebus_namespace_key_name,
                                   servicebus_namespace_primary_key,
                                   servicebus_namespace_connection_string,
                                   **kwargs):
        # This should "just work" to validate known-good.
        credential = ServiceBusSharedKeyCredential(servicebus_namespace_key_name, servicebus_namespace_primary_key)
        hostname = "{}.servicebus.windows.net".format(servicebus_namespace.name)
        auth_uri = "sb://{}/{}".format(hostname, servicebus_queue.name)
        token = credential.get_token(auth_uri).token

        # Finally let's do it with SAS token + conn str
        token_conn_str = "Endpoint=sb://{}/;SharedAccessSignature={};".format(hostname, token.decode())

        client = ServiceBusClient.from_connection_string(token_conn_str)
        with client:
            assert len(client._handlers) == 0
            with client.get_queue_sender(servicebus_queue.name) as sender:
                sender.send_messages(Message("foo"))

        # This is disabled pending UAMQP fix https://github.com/Azure/azure-uamqp-python/issues/170
        #
        #token_conn_str_without_se = token_conn_str.split('se=')[0] + token_conn_str.split('se=')[1].split('&')[1]
        #
        #client = ServiceBusClient.from_connection_string(token_conn_str_without_se)
        #with client:
        #    assert len(client._handlers) == 0
        #    with client.get_queue_sender(servicebus_queue.name) as sender:
        #        sender.send_messages(Message("foo"))
Пример #26
0
    def test_session_by_session_client_conn_str_receive_handler_peeklock_abandon(self, servicebus_namespace_connection_string, servicebus_queue, **kwargs):
        with ServiceBusClient.from_connection_string(
            servicebus_namespace_connection_string, logging_enable=False) as sb_client:

            session_id = str(uuid.uuid4())
            with sb_client.get_queue_sender(servicebus_queue.name) as sender:
                for i in range(3):
                    message = Message("Handler message no. {}".format(i), session_id=session_id)
                    sender.send(message)

            with sb_client.get_queue_session_receiver(servicebus_queue.name, session_id=session_id, prefetch=0) as receiver:
                message = receiver.next()
                assert message.sequence_number == 1
                message.abandon()
                second_message = receiver.next()
                assert second_message.sequence_number == 1
Пример #27
0
    async def test_async_session_by_conn_str_receive_handler_with_autolockrenew(
            self, servicebus_namespace_connection_string, servicebus_queue,
            **kwargs):
        async with ServiceBusClient.from_connection_string(
                servicebus_namespace_connection_string,
                logging_enable=False) as sb_client:
            session_id = str(uuid.uuid4())

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

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

                        elif len(messages) == 1:
                            await asyncio.sleep(45)
                            print("Second sleep {}".format(
                                session.session.locked_until_utc - utc_now()))
                            assert session.session.expired
                            assert isinstance(session.session.auto_renew_error,
                                              AutoLockRenewTimeout)
                            try:
                                await message.complete()
                                raise AssertionError(
                                    "Didn't raise SessionLockExpired")
                            except SessionLockExpired as e:
                                assert isinstance(e.inner_exception,
                                                  AutoLockRenewTimeout)
                            messages.append(message)

            await renewer.shutdown()
            assert len(messages) == 2
    async def test_async_session_by_conn_str_receive_handler_with_autolockrenew(self, servicebus_namespace_connection_string, servicebus_queue, **kwargs):
        async with ServiceBusClient.from_connection_string(
            servicebus_namespace_connection_string, logging_enable=False) as sb_client:
            session_id = str(uuid.uuid4())

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

            results = []
            async def lock_lost_callback(renewable, error):
                results.append(renewable)

            renewer = AutoLockRenew()
            messages = []
            async with sb_client.get_queue_session_receiver(servicebus_queue.name, session_id=session_id, max_wait_time=5, receive_mode=ReceiveMode.PeekLock, prefetch_count=20) as session:
                renewer.register(session.session, timeout=60)
                print("Registered lock renew thread", session.session.locked_until_utc, utc_now())
                with pytest.raises(SessionLockExpired):
                    async for message in session:
                        if not messages:
                            await asyncio.sleep(45)
                            print("First sleep {}".format(session.session.locked_until_utc - utc_now()))
                            assert not session.session._lock_expired
                            with pytest.raises(TypeError):
                                message._lock_expired
                            assert message.locked_until_utc is None
                            with pytest.raises(TypeError):
                                await message.renew_lock()
                            assert message.lock_token is not None
                            await message.complete()
                            messages.append(message)

                        elif len(messages) == 1:
                            assert not results
                            await asyncio.sleep(45)
                            print("Second sleep {}".format(session.session.locked_until_utc - utc_now()))
                            assert session.session._lock_expired
                            assert isinstance(session.session.auto_renew_error, AutoLockRenewTimeout)
                            try:
                                await message.complete()
                                raise AssertionError("Didn't raise SessionLockExpired")
                            except SessionLockExpired as e:
                                assert isinstance(e.inner_exception, AutoLockRenewTimeout)
                            messages.append(message)

            # While we're testing autolockrenew and sessions, let's make sure we don't call the lock-lost callback when a session exits.
            renewer._renew_period = 1
            session = None

            async with sb_client.get_queue_session_receiver(servicebus_queue.name, session_id=session_id, max_wait_time=5, receive_mode=ReceiveMode.PeekLock, prefetch_count=10) as receiver:
                session = receiver.session
                renewer.register(session, timeout=5, on_lock_renew_failure=lock_lost_callback)
            await asyncio.sleep(max(0,(session.locked_until_utc - utc_now()).total_seconds()+1)) # If this pattern repeats make sleep_until_expired_async
            assert not results

            await renewer.close()
            assert len(messages) == 2
    def test_sb_client_bad_entity(self, servicebus_namespace_connection_string, **kwargs):

        client = ServiceBusClient.from_connection_string(servicebus_namespace_connection_string)

        with client:
            with pytest.raises(ServiceBusConnectionError):
                with client.get_queue_sender("invalid") as sender:
                    sender.send_messages(Message("test"))
    async def test_async_session_message_connection_closed(self, servicebus_namespace_connection_string, servicebus_queue, **kwargs):
        async with ServiceBusClient.from_connection_string(
            servicebus_namespace_connection_string, logging_enable=False) as sb_client:

            session_id = str(uuid.uuid4())

            async with sb_client.get_queue_sender(servicebus_queue.name) as sender:
                message = Message("test")
                message.session_id = session_id
                await sender.send_messages(message)

            async with sb_client.get_queue_session_receiver(servicebus_queue.name, session_id=session_id) as receiver:
                messages = await receiver.receive_messages(max_wait_time=10)
                assert len(messages) == 1

            with pytest.raises(MessageSettleFailed):
                await messages[0].complete()