Beispiel #1
0
def _claim_ownership_exception_test(storage_connection_str, table_name):
    fully_qualified_namespace = "test_namespace"
    eventhub_name = "eventhub"
    consumer_group = "$default"
    ownership_cnt = 8

    checkpoint_store = TableCheckpointStore.from_connection_string(
        storage_connection_str, table_name)
    ownership_list = []
    for i in range(ownership_cnt):
        ownership = _create_ownership(str(i), "owner_id", None, None)
        ownership_list.append(ownership)
    result_ownership_list = checkpoint_store.claim_ownership(ownership_list)
    assert result_ownership_list[0]["owner_id"] == "owner_id"
    single_ownership = [result_ownership_list[0].copy()]
    single_ownership[0]["owner_id"] = "Bill"
    ownership_list = checkpoint_store.claim_ownership(single_ownership)
    assert ownership_list[0]["owner_id"] == "Bill"

    single_ownership = [result_ownership_list[0].copy()]
    single_ownership[0][
        "etag"] = "W/\"datetime'2021-08-02T00%3A46%3A51.7645424Z'\""
    single_ownership[0]["owner_id"] = "Jack"
    single_ownership[0]["partition_id"] = "10"
    result_ownership = checkpoint_store.claim_ownership(single_ownership)
    list_ownership = checkpoint_store.list_ownership(fully_qualified_namespace,
                                                     eventhub_name,
                                                     consumer_group)
    assert result_ownership[0] in list_ownership

    single_ownership = [result_ownership_list[0].copy()]
    single_ownership[0][
        "etag"] = "W/\"datetime'2021-08-02T00%3A46%3A51.7645424Z'\""
    with pytest.raises(OwnershipLostError) as e_info:
        checkpoint_store.claim_ownership(single_ownership)
Beispiel #2
0
def _claim_and_list_ownership(storage_connection_str, table_name):
    fully_qualified_namespace = "test_namespace"
    eventhub_name = "eventhub"
    consumer_group = "$default"
    ownership_cnt = 8

    checkpoint_store = TableCheckpointStore.from_connection_string(
        storage_connection_str, table_name)
    ownership_list = checkpoint_store.list_ownership(fully_qualified_namespace,
                                                     eventhub_name,
                                                     consumer_group)
    assert len(ownership_list) == 0

    ownership_list = []

    for i in range(ownership_cnt):
        ownership = _create_ownership(str(i), "owner_id", None, None)
        ownership_list.append(ownership)
    result_ownership_list = checkpoint_store.claim_ownership(ownership_list)
    assert ownership_list != result_ownership_list
    assert len(result_ownership_list) == len(ownership_list)
    for i in range(len(ownership_list)):
        assert ownership_list[i]["etag"] != result_ownership_list[i]["etag"]
        assert (ownership_list[i]["last_modified_time"] !=
                result_ownership_list[i]["last_modified_time"])

    ownership_list = checkpoint_store.list_ownership(fully_qualified_namespace,
                                                     eventhub_name,
                                                     consumer_group)
    assert len(ownership_list) == ownership_cnt
    assert len(ownership_list) == len(result_ownership_list)
    for i in range(len(result_ownership_list)):
        assert ownership_list[i]["etag"] == result_ownership_list[i]["etag"]
        assert (ownership_list[i]["last_modified_time"] ==
                result_ownership_list[i]["last_modified_time"])
Beispiel #3
0
def _update_and_list_checkpoint(storage_connection_str, table_name):
    fully_qualified_namespace = "test_namespace"
    eventhub_name = "eventhub"
    consumer_group = "$default"
    partition_cnt = 8

    checkpoint_store = TableCheckpointStore.from_connection_string(
        storage_connection_str, table_name)
    checkpoint_list = checkpoint_store.list_checkpoints(
        fully_qualified_namespace, eventhub_name, consumer_group)
    assert len(checkpoint_list) == 0
    for i in range(partition_cnt):
        checkpoint = _create_checkpoint(i, 2, 20)
        checkpoint_store.update_checkpoint(checkpoint)

    checkpoint_list = checkpoint_store.list_checkpoints(
        fully_qualified_namespace, eventhub_name, consumer_group)
    assert len(checkpoint_list) == partition_cnt
    for checkpoint in checkpoint_list:
        assert checkpoint["offset"] == "2"
        assert checkpoint["sequence_number"] == 20

    checkpoint = _create_checkpoint(0, "30", 42)
    checkpoint_store.update_checkpoint(checkpoint)
    checkpoint_list = checkpoint_store.list_checkpoints(
        fully_qualified_namespace, eventhub_name, consumer_group)
    assert len(checkpoint_list) == partition_cnt
    assert checkpoint_list[0]["offset"] == "30"
CONNECTION_STR = os.environ["EVENT_HUB_CONN_STR"]
EVENTHUB_NAME = os.environ["EVENT_HUB_NAME"]
STORAGE_CONNECTION_STR = os.environ["AZURE_STORAGE_CONN_STR"]
TABLE_NAME = "your-table-name"  # Please make sure the table resource exists.


def on_event(partition_context, event):
    # Put your code here.
    # Avoid time-consuming operations.
    print(event)
    partition_context.update_checkpoint(event)


if __name__ == "__main__":
    checkpoint_store = TableCheckpointStore.from_connection_string(
        STORAGE_CONNECTION_STR,
        table_name=TABLE_NAME,
    )
    client = EventHubConsumerClient.from_connection_string(
        CONNECTION_STR,
        consumer_group="$Default",
        eventhub_name=EVENTHUB_NAME,
        checkpoint_store=checkpoint_store,
    )

    try:
        client.receive(on_event)
    except KeyboardInterrupt:
        client.close()