async def sample_queue_send_receive_batch_async(sb_config, queue):
    client = ServiceBusClient(service_namespace=sb_config['hostname'],
                              shared_access_key_name=sb_config['key_name'],
                              shared_access_key_value=sb_config['access_key'],
                              debug=False)

    queue_client = client.get_queue(queue)
    async with queue_client.get_sender() as sender:
        for i in range(100):
            message = Message("Sample message no. {}".format(i))
            await sender.send(message)
        await sender.send(Message("shutdown"))

    async with queue_client.get_receiver(idle_timeout=1,
                                         mode=ReceiveSettleMode.PeekLock,
                                         prefetch=10) as receiver:
        # Receive list of messages as a batch
        batch = await receiver.fetch_next(max_batch_size=10)
        await asyncio.gather(*[m.complete() for m in batch])

        # Receive messages as a continuous generator
        async for message in receiver:
            print("Message: {}".format(message))
            print("Sequence number: {}".format(message.sequence_number))
            await message.complete()
Beispiel #2
0
async def test_async_queue_schedule_message(live_servicebus_config,
                                            standard_queue):
    client = ServiceBusClient(
        service_namespace=live_servicebus_config['hostname'],
        shared_access_key_name=live_servicebus_config['key_name'],
        shared_access_key_value=live_servicebus_config['access_key'],
        debug=True)
    import uuid
    queue_client = client.get_queue(standard_queue)
    enqueue_time = (datetime.utcnow() +
                    timedelta(minutes=2)).replace(microsecond=0)
    async with queue_client.get_receiver() as receiver:
        async with queue_client.get_sender() as sender:
            content = str(uuid.uuid4())
            message_id = uuid.uuid4()
            message = Message(content)
            message.properties.message_id = message_id
            message.schedule(enqueue_time)
            await sender.send(message)

        messages = await receiver.fetch_next(timeout=120)
        if messages:
            try:
                data = str(messages[0])
                assert data == content
                assert messages[0].properties.message_id == message_id
                assert messages[0].scheduled_enqueue_time == enqueue_time
                assert messages[0].scheduled_enqueue_time == messages[
                    0].enqueued_time.replace(microsecond=0)
                assert len(messages) == 1
            finally:
                for m in messages:
                    await m.complete()
        else:
            raise Exception("Failed to receive schdeduled message.")
async def test_async_session_schedule_message(live_servicebus_config, session_queue):
    client = ServiceBusClient(
        service_namespace=live_servicebus_config['hostname'],
        shared_access_key_name=live_servicebus_config['key_name'],
        shared_access_key_value=live_servicebus_config['access_key'],
        debug=True)
    import uuid
    session_id = str(uuid.uuid4())
    queue_client = client.get_queue(session_queue)
    enqueue_time = (datetime.now() + timedelta(minutes=2)).replace(microsecond=0)
    async with queue_client.get_sender(session=session_id) as sender:
        content = str(uuid.uuid4())
        message_id = uuid.uuid4()
        message = Message(content)
        message.properties.message_id = message_id
        message.schedule(enqueue_time)
        await sender.send(message)

    messages = []
    renewer = AutoLockRenew()
    async with queue_client.get_receiver(session=session_id) as receiver:
        renewer.register(receiver, timeout=140)
        messages.extend(await receiver.fetch_next(timeout=120))
        messages.extend(await receiver.fetch_next(timeout=5))
        if messages:
            data = str(messages[0])
            assert data == content
            assert messages[0].properties.message_id == message_id
            assert messages[0].scheduled_enqueue_time == enqueue_time
            assert messages[0].scheduled_enqueue_time == messages[0].enqueued_time.replace(microsecond=0)
            assert len(messages) == 1
        else:
            raise Exception("Failed to receive schdeduled message.")
    await renewer.shutdown()
async def test_async_session_message_expiry(live_servicebus_config, session_queue):
    client = ServiceBusClient(
        service_namespace=live_servicebus_config['hostname'],
        shared_access_key_name=live_servicebus_config['key_name'],
        shared_access_key_value=live_servicebus_config['access_key'],
        debug=True)
    session_id = str(uuid.uuid4())
    queue_client = client.get_queue(session_queue)
    
    async with queue_client.get_sender() as sender:
        message = Message("Testing expired messages")
        message.session_id = session_id
        await sender.send(message)

    async with queue_client.get_receiver(session=session_id) as receiver:
        messages = await receiver.fetch_next(timeout=10)
        assert len(messages) == 1
        print_message(messages[0])
        await asyncio.sleep(30)
        with pytest.raises(TypeError):
            messages[0].expired
        with pytest.raises(TypeError):
            await messages[0].renew_lock()
        assert receiver.expired
        with pytest.raises(SessionLockExpired):
            await messages[0].complete()
        with pytest.raises(SessionLockExpired):
            await receiver.renew_lock()

    async with queue_client.get_receiver(session=session_id) as receiver:
        messages = await receiver.fetch_next(timeout=30)
        assert len(messages) == 1
        print_message(messages[0])
        #assert messages[0].header.delivery_count  # TODO confirm this with service
        await messages[0].complete()
async def test_async_session_schedule_message(live_servicebus_config, session_queue):
    client = ServiceBusClient(
        service_namespace=live_servicebus_config['hostname'],
        shared_access_key_name=live_servicebus_config['key_name'],
        shared_access_key_value=live_servicebus_config['access_key'],
        debug=True)
    import uuid
    session_id = str(uuid.uuid4())
    queue_client = client.get_queue(session_queue)
    enqueue_time = (datetime.now() + timedelta(minutes=2)).replace(microsecond=0)
    async with queue_client.get_sender(session=session_id) as sender:
        content = str(uuid.uuid4())
        message_id = uuid.uuid4()
        message = Message(content)
        message.properties.message_id = message_id
        message.schedule(enqueue_time)
        await sender.send(message)

    messages = []
    renewer = AutoLockRenew()
    async with queue_client.get_receiver(session=session_id) as receiver:
        renewer.register(receiver, timeout=140)
        messages.extend(await receiver.fetch_next(timeout=120))
        messages.extend(await receiver.fetch_next(timeout=5))
        if messages:
            data = str(messages[0])
            assert data == content
            assert messages[0].properties.message_id == message_id
            assert messages[0].scheduled_enqueue_time == enqueue_time
            assert messages[0].scheduled_enqueue_time == messages[0].enqueued_time.replace(microsecond=0)
            assert len(messages) == 1
        else:
            raise Exception("Failed to receive schdeduled message.")
    await renewer.shutdown()
Beispiel #6
0
async def test_async_queue_by_servicebus_client_fail_send_messages(
        live_servicebus_config, standard_queue):
    client = ServiceBusClient(
        service_namespace=live_servicebus_config['hostname'],
        shared_access_key_name=live_servicebus_config['key_name'],
        shared_access_key_value=live_servicebus_config['access_key'],
        debug=True)

    try:
        queue_client = client.get_queue(standard_queue)
    except MessageSendFailed:
        pytest.skip("Open issue for uAMQP on OSX")

    too_large = "A" * 1024 * 512
    results = await queue_client.send(Message(too_large))
    assert len(results) == 1
    assert not results[0][0]
    assert isinstance(results[0][1], MessageSendFailed)

    async with queue_client.get_sender() as sender:
        with pytest.raises(MessageSendFailed):
            await sender.send(Message(too_large))

    async with queue_client.get_sender() as sender:
        sender.queue_message(Message(too_large))
        results = await sender.send_pending_messages()
        assert len(results) == 1
        assert not results[0][0]
        assert isinstance(results[0][1], MessageSendFailed)
Beispiel #7
0
async def test_async_queue_message_time_to_live(live_servicebus_config,
                                                standard_queue):
    client = ServiceBusClient(
        service_namespace=live_servicebus_config['hostname'],
        shared_access_key_name=live_servicebus_config['key_name'],
        shared_access_key_value=live_servicebus_config['access_key'],
        debug=True)
    import uuid
    queue_client = client.get_queue(standard_queue)

    async with queue_client.get_sender() as sender:
        content = str(uuid.uuid4())
        message_id = uuid.uuid4()
        message = Message(content)
        message.time_to_live = timedelta(seconds=30)
        await sender.send(message)

    time.sleep(30)
    async with queue_client.get_receiver() as receiver:
        messages = await receiver.fetch_next(timeout=10)
    assert not messages

    async with queue_client.get_deadletter_receiver(
            idle_timeout=5, mode=ReceiveSettleMode.PeekLock) as receiver:
        count = 0
        async for message in receiver:
            print_message(message)
            await message.complete()
            count += 1
        assert count == 1
Beispiel #8
0
    async def test_async_session_by_servicebus_client_browse_messages_with_receiver(
            self, servicebus_namespace, servicebus_namespace_key_name,
            servicebus_namespace_primary_key, servicebus_queue, **kwargs):

        client = ServiceBusClient(
            service_namespace=servicebus_namespace.name,
            shared_access_key_name=servicebus_namespace_key_name,
            shared_access_key_value=servicebus_namespace_primary_key,
            debug=False)

        queue_client = client.get_queue(servicebus_queue.name)
        session_id = str(uuid.uuid4())
        async with queue_client.get_receiver(idle_timeout=5,
                                             session=session_id) as receiver:
            async with queue_client.get_sender(session=session_id) as sender:
                for i in range(5):
                    message = Message("Test message no. {}".format(i))
                    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(message)
                with pytest.raises(TypeError):
                    message.complete()
async def test_async_session_by_servicebus_client_browse_messages_client(
        live_servicebus_config, session_queue):
    client = ServiceBusClient(
        service_namespace=live_servicebus_config['hostname'],
        shared_access_key_name=live_servicebus_config['key_name'],
        shared_access_key_value=live_servicebus_config['access_key'],
        debug=True)

    queue_client = client.get_queue(session_queue)
    session_id = str(uuid.uuid4())
    async with queue_client.get_sender(session=session_id) as sender:
        for i in range(5):
            message = Message("Test message no. {}".format(i))
            await sender.send(message)

    with pytest.raises(ValueError):
        messages = await queue_client.peek(5)

    messages = await queue_client.peek(5, session=session_id)
    assert len(messages) == 5
    assert all(isinstance(m, PeekMessage) for m in messages)
    for message in messages:
        print_message(message)
        with pytest.raises(TypeError):
            message.complete()
async def test_async_queue_message_time_to_live(live_servicebus_config, standard_queue):
    client = ServiceBusClient(
        service_namespace=live_servicebus_config['hostname'],
        shared_access_key_name=live_servicebus_config['key_name'],
        shared_access_key_value=live_servicebus_config['access_key'],
        debug=True)
    import uuid
    queue_client = client.get_queue(standard_queue)
    
    async with queue_client.get_sender() as sender:
        content = str(uuid.uuid4())
        message_id = uuid.uuid4()
        message = Message(content)
        message.time_to_live = timedelta(seconds=30)
        await sender.send(message)

    time.sleep(30)
    async with queue_client.get_receiver() as receiver:
        messages = await receiver.fetch_next(timeout=10)
    assert not messages

    async with queue_client.get_deadletter_receiver(idle_timeout=5, mode=ReceiveSettleMode.PeekLock) as receiver:
        count = 0
        async for message in receiver:
            print_message(message)
            await message.complete()
            count += 1
        assert count == 1
Beispiel #11
0
async def main(msg: func.ServiceBusMessage):
    try:
        logging.info('Python ServiceBus queue trigger processed message: %s',
                     msg.get_body().decode('utf-8'))

        raise Exception('something went wrong')
    except Exception:
        retry_count = int(msg.user_properties['retry-count']
                          ) if 'retry-count' in msg.user_properties else 0

        # If there are retries remaining
        if retry_count < RETRY_LIMIT:
            # Calculate a delay of seconds for next message
            retry_count += 1
            delay_seconds = retry_count * RETRY_MULTIPLIER_SECONDS
            logging.info('Scheduling message for the %i retry in %i seconds',
                         retry_count, delay_seconds)

            # Schedule and resend message onto queue
            async with queue_client.get_sender() as sender:
                message = Message(msg.get_body())
                message.message_id = msg.message_id
                message.user_properties = {
                    'original-message-id': msg.message_id,
                    'retry-count': retry_count
                }
                enqueue_time = datetime.now() + timedelta(
                    seconds=delay_seconds)
                await sender.schedule(enqueue_time, message)
        else:
            logging.error('Exhausted all retries')
Beispiel #12
0
async def main(req: func.HttpRequest) -> func.HttpResponse:
    body_bytes = req.get_body()

    if not body_bytes:
        return func.HttpResponse(status_code=400)

    body_json = json.loads(body_bytes.decode())

    # Get queue client
    connection_str = os.environ['ServiceBusConnection']
    sb_client = ServiceBusClient.from_connection_string(connection_str)
    queue_client = sb_client.get_queue("scheduled-tweets")

    # Create Service Bus message
    status, time_delay = body_json.get('statusUpdate'), body_json.get(
        'minutesFromNow')
    message = Message(status)
    message.properties.message_id = uuid.uuid4()

    # schedule message
    enqueue_time = (datetime.utcnow() +
                    timedelta(minutes=time_delay)).replace(microsecond=0)
    message.scheduled_enqueue_time
    message.schedule(enqueue_time)

    # Add message to queue
    await queue_client.send(message)

    return func.HttpResponse(status_code=201)
async def test_async_session_message_expiry(live_servicebus_config, session_queue):
    client = ServiceBusClient(
        service_namespace=live_servicebus_config['hostname'],
        shared_access_key_name=live_servicebus_config['key_name'],
        shared_access_key_value=live_servicebus_config['access_key'],
        debug=True)
    session_id = str(uuid.uuid4())
    queue_client = client.get_queue(session_queue)
    
    async with queue_client.get_sender() as sender:
        message = Message("Testing expired messages")
        message.session_id = session_id
        await sender.send(message)

    async with queue_client.get_receiver(session=session_id) as receiver:
        messages = await receiver.fetch_next(timeout=10)
        assert len(messages) == 1
        print_message(messages[0])
        await asyncio.sleep(30)
        with pytest.raises(TypeError):
            messages[0].expired
        with pytest.raises(TypeError):
            await messages[0].renew_lock()
        assert receiver.expired
        with pytest.raises(SessionLockExpired):
            await messages[0].complete()
        with pytest.raises(SessionLockExpired):
            await receiver.renew_lock()

    async with queue_client.get_receiver(session=session_id) as receiver:
        messages = await receiver.fetch_next(timeout=30)
        assert len(messages) == 1
        print_message(messages[0])
        #assert messages[0].header.delivery_count  # TODO confirm this with service
        await messages[0].complete()
async def test_async_queue_by_queue_client_conn_str_receive_handler_receiveanddelete(live_servicebus_config, standard_queue):
    queue_client = QueueClient.from_connection_string(
        live_servicebus_config['conn_str'],
        name=standard_queue,
        debug=True)
    queue_client.get_properties()

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

    messages = []
    receiver = queue_client.get_receiver(mode=ReceiveSettleMode.ReceiveAndDelete, idle_timeout=5)
    async for message in receiver:
        messages.append(message)
        with pytest.raises(MessageAlreadySettled):
            await message.complete()

    assert not receiver.running
    assert len(messages) == 10
    time.sleep(30)

    messages = []
    receiver = queue_client.get_receiver(mode=ReceiveSettleMode.ReceiveAndDelete, idle_timeout=5)
    async for message in receiver:
        messages.append(message)
    assert len(messages) == 0
async def test_async_queue_schedule_message(live_servicebus_config, standard_queue):
    client = ServiceBusClient(
        service_namespace=live_servicebus_config['hostname'],
        shared_access_key_name=live_servicebus_config['key_name'],
        shared_access_key_value=live_servicebus_config['access_key'],
        debug=True)
    import uuid
    queue_client = client.get_queue(standard_queue)
    enqueue_time = (datetime.now() + timedelta(minutes=2)).replace(microsecond=0)
    async with queue_client.get_receiver() as receiver:
        async with queue_client.get_sender() as sender:
            content = str(uuid.uuid4())
            message_id = uuid.uuid4()
            message = Message(content)
            message.properties.message_id = message_id
            message.schedule(enqueue_time)
            await sender.send(message)

        messages = await receiver.fetch_next(timeout=120)
        if messages:
            try:
                data = str(messages[0])
                assert data == content
                assert messages[0].properties.message_id == message_id
                assert messages[0].scheduled_enqueue_time == enqueue_time
                assert messages[0].scheduled_enqueue_time == messages[0].enqueued_time.replace(microsecond=0)
                assert len(messages) == 1
            finally:
                for m in messages:
                    await m.complete()
        else:
            raise Exception("Failed to receive schdeduled message.")
Beispiel #16
0
async def test_async_queue_by_queue_client_conn_str_receive_handler_receiveanddelete(
        live_servicebus_config, standard_queue):
    queue_client = QueueClient.from_connection_string(
        live_servicebus_config['conn_str'], name=standard_queue, debug=True)
    queue_client.get_properties()

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

    messages = []
    receiver = queue_client.get_receiver(
        mode=ReceiveSettleMode.ReceiveAndDelete, idle_timeout=5)
    async for message in receiver:
        messages.append(message)
        with pytest.raises(MessageAlreadySettled):
            await message.complete()

    assert not receiver.running
    assert len(messages) == 10
    time.sleep(30)

    messages = []
    receiver = queue_client.get_receiver(
        mode=ReceiveSettleMode.ReceiveAndDelete, idle_timeout=5)
    async for message in receiver:
        messages.append(message)
    assert len(messages) == 0
async def test_async_session_cancel_scheduled_messages(live_servicebus_config, session_queue):
    client = ServiceBusClient(
        service_namespace=live_servicebus_config['hostname'],
        shared_access_key_name=live_servicebus_config['key_name'],
        shared_access_key_value=live_servicebus_config['access_key'],
        debug=True)

    session_id = str(uuid.uuid4())
    queue_client = client.get_queue(session_queue)
    enqueue_time = (datetime.now() + timedelta(minutes=2)).replace(microsecond=0)
    async with queue_client.get_sender(session=session_id) as sender:
        message_a = Message("Test scheduled message")
        message_b = Message("Test scheduled message")
        tokens = await sender.schedule(enqueue_time, message_a, message_b)
        assert len(tokens) == 2
        await sender.cancel_scheduled_messages(*tokens)

    renewer = AutoLockRenew()
    messages = []
    async with queue_client.get_receiver(session=session_id) as receiver:
        renewer.register(receiver, timeout=140)
        messages.extend(await receiver.fetch_next(timeout=120))
        messages.extend(await receiver.fetch_next(timeout=5))
        try:
            assert len(messages) == 0
        except AssertionError:
            for m in messages:
                print(str(m))
                await m.complete()
            raise
    await renewer.shutdown()
Beispiel #18
0
async def main(count: int):
    queue_client: QueueClient = QueueClient.from_connection_string(
        sender_constr, 'simplequeue')
    msg = Message(body='This is the message content')

    for index in range(count):
        msg = Message(f'New Message {index}')
        msg.properties.content_type = 'text/plain'
        msg.properties.message_id = uuid.uuid4().hex
        await queue_client.send(msg)
async def test_async_session_by_servicebus_client_iter_messages_with_retrieve_deferred_client(live_servicebus_config, session_queue):
    client = ServiceBusClient(
        service_namespace=live_servicebus_config['hostname'],
        shared_access_key_name=live_servicebus_config['key_name'],
        shared_access_key_value=live_servicebus_config['access_key'],
        debug=True)

    queue_client = client.get_queue(session_queue)
    deferred_messages = []
    session_id = str(uuid.uuid4())
    async with queue_client.get_sender(session=session_id) as sender:
        for i in range(10):
            message = Message("Deferred message no. {}".format(i))
            await sender.send(message)

    session = queue_client.get_receiver(session=session_id, idle_timeout=5)
    count = 0
    async for message in session:
        deferred_messages.append(message.sequence_number)
        print_message(message)
        count += 1
        await message.defer()

    assert count == 10

    with pytest.raises(ValueError):
        deferred = await queue_client.receive_deferred_messages(deferred_messages, session=session_id)

    with pytest.raises(ValueError):
        await queue_client.settle_deferred_messages("completed", [message], session=session_id)
async def test_async_session_by_servicebus_client_iter_messages_with_retrieve_deferred_receiver_deletemode(live_servicebus_config, session_queue):
    client = ServiceBusClient(
        service_namespace=live_servicebus_config['hostname'],
        shared_access_key_name=live_servicebus_config['key_name'],
        shared_access_key_value=live_servicebus_config['access_key'],
        debug=True)

    queue_client = client.get_queue(session_queue)
    deferred_messages = []
    session_id = str(uuid.uuid4())
    messages = [Message("Deferred message no. {}".format(i)) for i in range(10)]
    results = await queue_client.send(messages, session=session_id)
    assert all(result[0] for result in results)

    count = 0
    session = queue_client.get_receiver(session=session_id, idle_timeout=5)
    async for message in session:
        deferred_messages.append(message.sequence_number)
        print_message(message)
        count += 1
        await message.defer()

    assert count == 10
    async with queue_client.get_receiver(session=session_id, idle_timeout=5) as session:
        deferred = await session.receive_deferred_messages(deferred_messages, mode=ReceiveSettleMode.ReceiveAndDelete)
        assert len(deferred) == 10
        for message in deferred:
            assert isinstance(message, DeferredMessage)
            with pytest.raises(MessageAlreadySettled):
                await message.complete()
        with pytest.raises(ServiceBusError):
            deferred = await session.receive_deferred_messages(deferred_messages)
class ServiceBusNotificationService(BaseNotificationService):
    """Implement the notification service for the service bus service."""
    def __init__(self, client):
        """Initialize the service."""
        self._client = client

    async def async_send_message(self, message, **kwargs):
        """Send a message."""
        dto = {ATTR_ASB_MESSAGE: message}

        if ATTR_TITLE in kwargs:
            dto[ATTR_ASB_TITLE] = kwargs[ATTR_TITLE]
        if ATTR_TARGET in kwargs:
            dto[ATTR_ASB_TARGET] = kwargs[ATTR_TARGET]

        if data := kwargs.get(ATTR_DATA):
            dto.update(data)

        queue_message = Message(json.dumps(dto))
        queue_message.properties.content_type = CONTENT_TYPE_JSON
        try:
            await self._client.send(queue_message)
        except MessageSendFailed as err:
            _LOGGER.error(
                "Could not send service bus notification to %s. %s",
                self._client.name,
                err,
            )
async def test_async_queue_message_receive_and_delete(live_servicebus_config, standard_queue):
    client = ServiceBusClient(
        service_namespace=live_servicebus_config['hostname'],
        shared_access_key_name=live_servicebus_config['key_name'],
        shared_access_key_value=live_servicebus_config['access_key'],
        debug=True)
    queue_client = client.get_queue(standard_queue)
    
    async with queue_client.get_sender() as sender:
        message = Message("Receive and delete test")
        await sender.send(message)

    async with queue_client.get_receiver(mode=ReceiveSettleMode.ReceiveAndDelete) as receiver:
        messages = await receiver.fetch_next(timeout=10)
        assert len(messages) == 1
        received = messages[0]
        print_message(received)
        with pytest.raises(MessageAlreadySettled):
            await received.complete()
        with pytest.raises(MessageAlreadySettled):
            await received.abandon()
        with pytest.raises(MessageAlreadySettled):
            await received.defer()
        with pytest.raises(MessageAlreadySettled):
            await received.dead_letter()
        with pytest.raises(MessageAlreadySettled):
            await received.renew_lock()

    time.sleep(30)
    async with queue_client.get_receiver() as receiver:
        messages = await receiver.fetch_next(timeout=10)
        for m in messages:
            print_message(m)
        assert len(messages) == 0
async def test_async_session_by_session_client_conn_str_receive_handler_peeklock(live_servicebus_config, session_queue):
    queue_client = QueueClient.from_connection_string(
        live_servicebus_config['conn_str'],
        name=session_queue,
        debug=True)
    queue_client.get_properties()

    session_id = str(uuid.uuid4())
    async with queue_client.get_sender(session=session_id) as sender:
        for i in range(3):
            message = Message("Handler message no. {}".format(i))
            await sender.send(message)

    with pytest.raises(ValueError):
        session = queue_client.get_receiver(idle_timeout=5)

    session = queue_client.get_receiver(session=session_id, idle_timeout=5)
    count = 0
    async for message in session:
        print_message(message)
        assert message.session_id == session_id
        count += 1
        await message.complete()

    assert count == 3
Beispiel #24
0
async def test_async_queue_message_lock_renew(live_servicebus_config,
                                              standard_queue):
    client = ServiceBusClient(
        service_namespace=live_servicebus_config['hostname'],
        shared_access_key_name=live_servicebus_config['key_name'],
        shared_access_key_value=live_servicebus_config['access_key'],
        debug=True)
    import uuid
    queue_client = client.get_queue(standard_queue)

    async with queue_client.get_sender() as sender:
        content = str(uuid.uuid4())
        message = Message(content)
        await sender.send(message)

    async with queue_client.get_receiver() as receiver:
        messages = await receiver.fetch_next(timeout=10)
        assert len(messages) == 1
        time.sleep(15)
        await messages[0].renew_lock()
        time.sleep(15)
        await messages[0].renew_lock()
        time.sleep(15)
        assert not messages[0].expired
        await messages[0].complete()

    async with queue_client.get_receiver() as receiver:
        messages = await receiver.fetch_next(timeout=10)
        assert len(messages) == 0
async def test_async_session_by_servicebus_client_fetch_next_with_retrieve_deadletter(live_servicebus_config, session_queue):
    client = ServiceBusClient(
        service_namespace=live_servicebus_config['hostname'],
        shared_access_key_name=live_servicebus_config['key_name'],
        shared_access_key_value=live_servicebus_config['access_key'],
        debug=True)

    queue_client = client.get_queue(session_queue)
    session_id = str(uuid.uuid4())
    async with queue_client.get_receiver(session=session_id, idle_timeout=5, prefetch=10) as receiver:

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

        count = 0
        messages = await receiver.fetch_next()
        while messages:
            for message in messages:
                print_message(message)
                await message.dead_letter(description="Testing queue deadletter")
                count += 1
            messages = await receiver.fetch_next()
    assert count == 10

    async with queue_client.get_deadletter_receiver(idle_timeout=5) as session:
        count = 0
        async for message in session:
            print_message(message)
            #assert message.user_properties[b'DeadLetterReason'] == b'something'  # TODO
            #assert message.user_properties[b'DeadLetterErrorDescription'] == b'something'  # TODO
            await message.complete()
            count += 1
    assert count == 10
Beispiel #26
0
async def test_async_queue_message_expiry(live_servicebus_config,
                                          standard_queue):
    client = ServiceBusClient(
        service_namespace=live_servicebus_config['hostname'],
        shared_access_key_name=live_servicebus_config['key_name'],
        shared_access_key_value=live_servicebus_config['access_key'],
        debug=True)
    import uuid
    queue_client = client.get_queue(standard_queue)

    async with queue_client.get_sender() as sender:
        content = str(uuid.uuid4())
        message = Message(content)
        await sender.send(message)

    async with queue_client.get_receiver() as receiver:
        messages = await receiver.fetch_next(timeout=10)
        assert len(messages) == 1
        time.sleep(30)
        assert messages[0].expired
        with pytest.raises(MessageLockExpired):
            await messages[0].complete()
        with pytest.raises(MessageLockExpired):
            await messages[0].renew_lock()

    async with queue_client.get_receiver() as receiver:
        messages = await receiver.fetch_next(timeout=30)
        assert len(messages) == 1
        print_message(messages[0])
        assert messages[0].header.delivery_count > 0
        await messages[0].complete()
Beispiel #27
0
async def test_async_queue_by_servicebus_client_iter_messages_with_retrieve_deferred_not_found(
        live_servicebus_config, standard_queue):
    client = ServiceBusClient(
        service_namespace=live_servicebus_config['hostname'],
        shared_access_key_name=live_servicebus_config['key_name'],
        shared_access_key_value=live_servicebus_config['access_key'],
        debug=True)

    queue_client = client.get_queue(standard_queue)
    deferred_messages = []
    async with queue_client.get_receiver(
            idle_timeout=5, mode=ReceiveSettleMode.PeekLock) as receiver:

        async with queue_client.get_sender() as sender:
            for i in range(3):
                message = Message("Deferred message no. {}".format(i))
                await sender.send(message)

        count = 0
        async for message in receiver:
            deferred_messages.append(message.sequence_number)
            print_message(message)
            count += 1
            await message.defer()

    assert count == 3

    with pytest.raises(ServiceBusError):
        deferred = await queue_client.receive_deferred_messages(
            [3, 4], mode=ReceiveSettleMode.PeekLock)

    with pytest.raises(ServiceBusError):
        deferred = await queue_client.receive_deferred_messages(
            [5, 6, 7], mode=ReceiveSettleMode.PeekLock)
Beispiel #28
0
async def test_async_queue_message_duplicate_detection(live_servicebus_config,
                                                       duplicate_queue):
    client = ServiceBusClient(
        service_namespace=live_servicebus_config['hostname'],
        shared_access_key_name=live_servicebus_config['key_name'],
        shared_access_key_value=live_servicebus_config['access_key'],
        debug=True)
    import uuid
    message_id = uuid.uuid4()
    queue_client = client.get_queue(duplicate_queue)

    async with queue_client.get_sender() as sender:
        for i in range(5):
            message = Message(str(i))
            message.properties.message_id = message_id
            await sender.send(message)

    async with queue_client.get_receiver(idle_timeout=5) as receiver:
        count = 0
        async for message in receiver:
            print_message(message)
            assert message.properties.message_id == message_id
            await message.complete()
            count += 1
        assert count == 1
Beispiel #29
0
async def test_async_queue_by_servicebus_conn_str_client_iter_messages_with_abandon(
        live_servicebus_config, standard_queue):
    client = ServiceBusClient.from_connection_string(
        live_servicebus_config['conn_str'], debug=True)

    queue_client = client.get_queue(standard_queue)
    async with queue_client.get_receiver(
            idle_timeout=5, mode=ReceiveSettleMode.PeekLock) as receiver:

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

        count = 0
        async for message in receiver:
            print_message(message)
            if not message.header.delivery_count:
                count += 1
                await message.abandon()
            else:
                assert message.header.delivery_count == 1
                await message.complete()

    assert count == 10

    async with queue_client.get_receiver(
            idle_timeout=5, mode=ReceiveSettleMode.PeekLock) as receiver:
        count = 0
        async for message in receiver:
            print_message(message)
            await message.complete()
            count += 1
    assert count == 0
Beispiel #30
0
async def test_async_queue_by_servicebus_client_iter_messages_with_defer(
        live_servicebus_config, standard_queue):
    client = ServiceBusClient(
        service_namespace=live_servicebus_config['hostname'],
        shared_access_key_name=live_servicebus_config['key_name'],
        shared_access_key_value=live_servicebus_config['access_key'],
        debug=True)

    queue_client = client.get_queue(standard_queue)
    deferred_messages = []
    async with queue_client.get_receiver(
            idle_timeout=5, mode=ReceiveSettleMode.PeekLock) as receiver:

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

        count = 0
        async for message in receiver:
            deferred_messages.append(message.sequence_number)
            print_message(message)
            count += 1
            await message.defer()

    assert count == 10
    async with queue_client.get_receiver(
            idle_timeout=5, mode=ReceiveSettleMode.PeekLock) as receiver:
        count = 0
        async for message in receiver:
            print_message(message)
            await message.complete()
            count += 1
    assert count == 0
Beispiel #31
0
async def test_async_queue_by_queue_client_conn_str_receive_handler_with_stop(
        live_servicebus_config, standard_queue):
    queue_client = QueueClient.from_connection_string(
        live_servicebus_config['conn_str'], name=standard_queue, debug=True)

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

    messages = []
    receiver = queue_client.get_receiver(idle_timeout=5)
    async for message in receiver:
        messages.append(message)
        await message.complete()
        if len(messages) >= 5:
            break

    assert receiver.running
    assert len(messages) == 5

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

    assert not receiver.running
    assert len(messages) == 6
Beispiel #32
0
async def test_async_queue_by_servicebus_client_iter_messages_simple(
        live_servicebus_config, standard_queue):
    client = ServiceBusClient(
        service_namespace=live_servicebus_config['hostname'],
        shared_access_key_name=live_servicebus_config['key_name'],
        shared_access_key_value=live_servicebus_config['access_key'],
        debug=True)

    queue_client = client.get_queue(standard_queue)
    async with queue_client.get_receiver(
            idle_timeout=5, mode=ReceiveSettleMode.PeekLock) as receiver:

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

        count = 0
        async for message in receiver:
            print_message(message)
            await message.complete()
            with pytest.raises(MessageAlreadySettled):
                await message.complete()
            with pytest.raises(MessageAlreadySettled):
                await message.renew_lock()
            count += 1

        with pytest.raises(InvalidHandlerState):
            await receiver.__anext__()

    assert count == 10
async def test_async_session_by_queue_client_conn_str_receive_handler_receiveanddelete(live_servicebus_config, session_queue):
    queue_client = QueueClient.from_connection_string(
        live_servicebus_config['conn_str'],
        name=session_queue,
        debug=True)
    queue_client.get_properties()

    session_id = str(uuid.uuid4())
    async with queue_client.get_sender(session=session_id) as sender:
        for i in range(10):
            message = Message("Handler message no. {}".format(i))
            await sender.send(message)

    messages = []
    session = queue_client.get_receiver(session=session_id, mode=ReceiveSettleMode.ReceiveAndDelete, idle_timeout=5)
    async for message in session:
        messages.append(message)
        assert session_id == session.session_id
        assert session_id == message.session_id
        with pytest.raises(MessageAlreadySettled):
            await message.complete()

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

    messages = []
    session = queue_client.get_receiver(session=session_id, mode=ReceiveSettleMode.ReceiveAndDelete, idle_timeout=5)
    async for message in session:
        messages.append(message)
    assert len(messages) == 0
async def test_async_session_by_servicebus_client_session_pool(live_servicebus_config, session_queue):
    messages = []
    errors = []
    async def message_processing(queue_client):
        while True:
            try:
                async with queue_client.get_receiver(session=NEXT_AVAILABLE, idle_timeout=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)]
    client = ServiceBusClient(
        service_namespace=live_servicebus_config['hostname'],
        shared_access_key_name=live_servicebus_config['key_name'],
        shared_access_key_value=live_servicebus_config['access_key'],
        debug=True)

    queue_client = client.get_queue(session_queue)
    for session_id in sessions:
        async with queue_client.get_sender(session=session_id) as sender:
            await asyncio.gather(*[sender.send(Message("Sample message no. {}".format(i))) for i in range(20)])

    receive_sessions = [message_processing(queue_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_list_sessions_with_client(live_servicebus_config, session_queue):
    client = ServiceBusClient(
        service_namespace=live_servicebus_config['hostname'],
        shared_access_key_name=live_servicebus_config['key_name'],
        shared_access_key_value=live_servicebus_config['access_key'],
        debug=True)

    queue_client = client.get_queue(session_queue)
    sessions = []
    start_time = datetime.now()
    for i in range(5):
        sessions.append(str(uuid.uuid4()))

    for session in sessions:
        async with queue_client.get_sender(session=session) as sender:
            for i in range(5):
                message = Message("Test message no. {}".format(i))
                await sender.send(message)
    for session in sessions:
        async with queue_client.get_receiver(session=session) as receiver:
            await receiver.set_session_state("SESSION {}".format(session))

    current_sessions = await queue_client.list_sessions(updated_since=start_time)
    assert len(current_sessions) == 5
    assert current_sessions == sessions
async def test_async_queue_by_servicebus_client_browse_messages_client(live_servicebus_config, standard_queue):
    client = ServiceBusClient(
        service_namespace=live_servicebus_config['hostname'],
        shared_access_key_name=live_servicebus_config['key_name'],
        shared_access_key_value=live_servicebus_config['access_key'],
        debug=True)

    queue_client = client.get_queue(standard_queue)
    async with queue_client.get_sender() as sender:
        for i in range(5):
            message = Message("Test message no. {}".format(i))
            await sender.send(message)

    messages = await queue_client.peek(5)
    assert len(messages) == 5
    assert all(isinstance(m, PeekMessage) for m in messages)
    for message in messages:
        print_message(message)
        with pytest.raises(TypeError):
            message.complete()
async def test_async_session_message_connection_closed(live_servicebus_config, session_queue):
    client = ServiceBusClient(
        service_namespace=live_servicebus_config['hostname'],
        shared_access_key_name=live_servicebus_config['key_name'],
        shared_access_key_value=live_servicebus_config['access_key'],
        debug=True)

    session_id = str(uuid.uuid4())
    queue_client = client.get_queue(session_queue)
    
    async with queue_client.get_sender() as sender:
        message = Message("test")
        message.session_id = session_id
        await sender.send(message)

    async with queue_client.get_receiver(session=session_id) as receiver:
        messages = await receiver.fetch_next(timeout=10)
        assert len(messages) == 1

    with pytest.raises(MessageSettleFailed):
        await messages[0].complete()
async def test_async_session_by_servicebus_client_browse_messages_with_receiver(live_servicebus_config, session_queue):
    client = ServiceBusClient(
        service_namespace=live_servicebus_config['hostname'],
        shared_access_key_name=live_servicebus_config['key_name'],
        shared_access_key_value=live_servicebus_config['access_key'],
        debug=True)

    queue_client = client.get_queue(session_queue)
    session_id = str(uuid.uuid4())
    async with queue_client.get_receiver(idle_timeout=5, session=session_id) as receiver:
        async with queue_client.get_sender(session=session_id) as sender:
            for i in range(5):
                message = Message("Test message no. {}".format(i))
                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(message)
            with pytest.raises(TypeError):
                message.complete()
async def test_async_queue_by_queue_client_conn_str_receive_handler_peeklock(live_servicebus_config, standard_queue):
    queue_client = QueueClient.from_connection_string(
        live_servicebus_config['conn_str'],
        name=standard_queue,
        debug=True)
    queue_client.get_properties()

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

    with pytest.raises(ValueError):
        queue_client.get_receiver(session="test", idle_timeout=5)

    receiver = queue_client.get_receiver(idle_timeout=5)
    count = 0
    async for message in receiver:
        print_message(message)
        count += 1
        await message.complete()

    assert count == 10
 def _build_message(self, received):
     message = Message(None, message=received)
     message._receiver = self  # pylint: disable=protected-access
     self.last_received = message.sequence_number
     return message