1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
|
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
import pytest
import uuid
import warnings
import os
from azure.eventhub.extensions.checkpointstoretable._vendor.data.tables import TableServiceClient
from azure.eventhub.extensions.checkpointstoretable import TableCheckpointStore
from azure.eventhub.exceptions import OwnershipLostError
STORAGE_ENV_KEYS = [
"AZURE_TABLES_CONN_STR",
"AZURE_COSMOS_CONN_STR"
]
def get_live_storage_table_client(conn_str_env_key):
try:
storage_connection_str = os.environ[conn_str_env_key]
table_name = "table{}".format(uuid.uuid4().hex)
table_service_client = TableServiceClient.from_connection_string(
storage_connection_str
)
table_service_client.create_table_if_not_exists(table_name)
return storage_connection_str, table_name
except:
pytest.skip("Storage table client can't be created")
def remove_live_storage_table_client(storage_connection_str, table_name):
try:
table_service_client = TableServiceClient.from_connection_string(
storage_connection_str
)
table_service_client.delete_table(table_name)
except:
warnings.warn(UserWarning("storage table teardown failed"))
def _create_checkpoint(partition_id, offset, sequence_number):
return {
"fully_qualified_namespace": "test_namespace",
"eventhub_name": "eventhub",
"consumer_group": "$default",
"partition_id": str(partition_id),
"offset": offset,
"sequence_number": sequence_number,
}
def _create_ownership(partition_id, owner_id, etag, last_modified_time):
return {
"fully_qualified_namespace": "test_namespace",
"eventhub_name": "eventhub",
"consumer_group": "$default",
"partition_id": str(partition_id),
"owner_id": owner_id,
"etag": etag,
"last_modified_time": last_modified_time,
}
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)
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"]
)
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"
@pytest.mark.parametrize("conn_str_env_key", STORAGE_ENV_KEYS)
@pytest.mark.liveTest
def test_claim_ownership_exception(conn_str_env_key):
storage_connection_str, table_name = get_live_storage_table_client(
conn_str_env_key
)
try:
_claim_ownership_exception_test(storage_connection_str, table_name)
finally:
remove_live_storage_table_client(storage_connection_str, table_name)
@pytest.mark.parametrize("conn_str_env_key", STORAGE_ENV_KEYS)
@pytest.mark.liveTest
def test_claim_and_list_ownership(conn_str_env_key):
storage_connection_str, table_name = get_live_storage_table_client(
conn_str_env_key
)
try:
_claim_and_list_ownership(storage_connection_str, table_name)
finally:
remove_live_storage_table_client(storage_connection_str, table_name)
@pytest.mark.parametrize("conn_str_env_key", STORAGE_ENV_KEYS)
@pytest.mark.liveTest
def test_update_checkpoint(conn_str_env_key):
storage_connection_str, table_name = get_live_storage_table_client(
conn_str_env_key
)
try:
_update_and_list_checkpoint(storage_connection_str, table_name)
finally:
remove_live_storage_table_client(storage_connection_str, table_name)
|