Skip to content

Commit

Permalink
Avoid 'timeout_ms' aliass in tests, switching to correct params every…
Browse files Browse the repository at this point in the history
…where (#336)

* Avoid any 'timeout_ms' alias in tests and switch to the correct timeout params

* fix selection of timeout in (async_)fetch_raw_database_info_from_id_token
  • Loading branch information
hemidactylus authored Feb 5, 2025
1 parent ea8fccd commit c5e627b
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 46 deletions.
11 changes: 7 additions & 4 deletions astrapy/admin/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,8 @@ def fetch_raw_database_info_from_id_token(
gd_response = dev_ops_commander.request(
http_method=HttpMethod.GET,
timeout_context=_TimeoutContext(
request_ms=timeout_ms, label=_timeout_context_label
request_ms=_api_options.timeout_options.request_timeout_ms,
label=_timeout_context_label,
),
)
return gd_response
Expand Down Expand Up @@ -299,7 +300,8 @@ async def async_fetch_raw_database_info_from_id_token(
gd_response = await dev_ops_commander.async_request(
http_method=HttpMethod.GET,
timeout_context=_TimeoutContext(
request_ms=timeout_ms, label=_timeout_context_label
request_ms=_api_options.timeout_options.request_timeout_ms,
label=_timeout_context_label,
),
)
return gd_response
Expand Down Expand Up @@ -1747,7 +1749,7 @@ def get_database(
(
self.database_info(
parsed_api_endpoint.database_id,
timeout_ms=_database_admin_timeout_ms,
database_admin_timeout_ms=_database_admin_timeout_ms,
).raw
or {}
).get("info")
Expand Down Expand Up @@ -1775,7 +1777,8 @@ def get_database(
_keyspace = (
(
self.database_info(
_id_p, timeout_ms=_database_admin_timeout_ms
_id_p,
database_admin_timeout_ms=_database_admin_timeout_ms,
).raw
or {}
).get("info")
Expand Down
2 changes: 1 addition & 1 deletion astrapy/exceptions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ def remaining_timeout(
Returns:
A _TimeoutContext appropriately detailing the residual time an overall
operation is allowed to last. Alternatively, the method may not return
and raise a DataAPITimeoutException directly.
and raise a DataAPITimeoutException/DevOpsAPITimeoutException directly.
"""

# a zero 'cap' must be treated as None:
Expand Down
44 changes: 28 additions & 16 deletions tests/base/integration/collections/test_collection_timeout_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ async def test_collection_count_documents_timeout_async(

with pytest.raises(DataAPITimeoutException) as exc:
await async_empty_collection.count_documents(
{}, upper_bound=800, timeout_ms=1
{}, upper_bound=800, general_method_timeout_ms=1
)
assert await async_empty_collection.count_documents({}, upper_bound=800) >= 500
assert exc.value.timeout_type in {"connect", "read"}
Expand All @@ -65,7 +65,7 @@ async def test_database_info_timeout_async(
async_database.api_endpoint,
token=async_database.api_options.token,
keyspace=async_database.keyspace,
timeout_ms=1,
request_timeout_ms=1,
)
assert info is not None
assert exc.value.timeout_type in {"connect", "read"}
Expand All @@ -80,37 +80,41 @@ async def test_cursor_overalltimeout_exceptions_async(
acol = async_empty_collection
await acol.insert_many([{"a": 1}] * 1000)

await acol.distinct("a", timeout_ms=60000)
await acol.distinct("a", general_method_timeout_ms=60000)
with pytest.raises(DataAPITimeoutException):
await acol.distinct("a", timeout_ms=1)
await acol.distinct("a", general_method_timeout_ms=1)

await acol.distinct("a", timeout_ms=60000)
await acol.distinct("a", general_method_timeout_ms=60000)
with pytest.raises(DataAPITimeoutException):
await acol.distinct("a", timeout_ms=1)
await acol.distinct("a", general_method_timeout_ms=1)

@pytest.mark.describe("test of insert_many timeouts, async")
async def test_insert_many_timeout_exceptions_async(
self,
async_collection: DefaultAsyncCollection,
) -> None:
fifty_docs = [{"seq": i} for i in range(50)]
await async_collection.insert_many(fifty_docs, ordered=True, timeout_ms=20000)
await async_collection.insert_many(
fifty_docs, ordered=False, concurrency=1, timeout_ms=20000
fifty_docs, ordered=True, general_method_timeout_ms=20000
)
await async_collection.insert_many(
fifty_docs, ordered=False, concurrency=2, timeout_ms=20000
fifty_docs, ordered=False, concurrency=1, general_method_timeout_ms=20000
)
await async_collection.insert_many(
fifty_docs, ordered=False, concurrency=2, general_method_timeout_ms=20000
)

with pytest.raises(DataAPITimeoutException):
await async_collection.insert_many(fifty_docs, ordered=True, timeout_ms=2)
await async_collection.insert_many(
fifty_docs, ordered=True, general_method_timeout_ms=2
)
with pytest.raises(DataAPITimeoutException):
await async_collection.insert_many(
fifty_docs, ordered=False, concurrency=1, timeout_ms=2
fifty_docs, ordered=False, concurrency=1, general_method_timeout_ms=2
)
with pytest.raises(DataAPITimeoutException):
await async_collection.insert_many(
fifty_docs, ordered=False, concurrency=2, timeout_ms=2
fifty_docs, ordered=False, concurrency=2, general_method_timeout_ms=2
)

@pytest.mark.describe("test of update_many timeouts, async")
Expand All @@ -123,12 +127,16 @@ async def test_update_many_timeout_exceptions_async(

await async_collection.update_many({"f": "update_many"}, {"$inc": {"seq": 100}})
await async_collection.update_many(
{"f": "update_many"}, {"$inc": {"seq": 100}}, timeout_ms=20000
{"f": "update_many"},
{"$inc": {"seq": 100}},
general_method_timeout_ms=20000,
)

with pytest.raises(DataAPITimeoutException):
await async_collection.update_many(
{"f": "update_many"}, {"$inc": {"seq": 100}}, timeout_ms=2
{"f": "update_many"},
{"$inc": {"seq": 100}},
general_method_timeout_ms=2,
)

@pytest.mark.describe("test of delete_many timeouts, async")
Expand All @@ -146,9 +154,13 @@ async def test_delete_many_timeout_exceptions_async(
)

await async_collection.delete_many({"f": "delete_many1"})
await async_collection.delete_many({"f": "delete_many2"}, timeout_ms=20000)
await async_collection.delete_many(
{"f": "delete_many2"}, general_method_timeout_ms=20000
)
with pytest.raises(DataAPITimeoutException):
await async_collection.delete_many({"f": "delete_many3"}, timeout_ms=2)
await async_collection.delete_many(
{"f": "delete_many3"}, general_method_timeout_ms=2
)

@pytest.mark.describe("test of collection find-with-collective timeout, async")
async def test_collection_find_with_collective_timeout_async(
Expand Down
44 changes: 29 additions & 15 deletions tests/base/integration/collections/test_collection_timeout_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ def test_collection_count_documents_timeout_sync(
time.sleep(2)

with pytest.raises(DataAPITimeoutException) as exc:
sync_empty_collection.count_documents({}, upper_bound=800, timeout_ms=1)
sync_empty_collection.count_documents(
{}, upper_bound=800, general_method_timeout_ms=1
)
assert sync_empty_collection.count_documents({}, upper_bound=800) >= 500
assert exc.value.timeout_type in {"connect", "read"}
assert exc.value.endpoint is not None
Expand All @@ -59,7 +61,7 @@ def test_database_info_timeout_sync(
sync_database.api_endpoint,
token=sync_database.api_options.token,
keyspace=sync_database.keyspace,
timeout_ms=1,
request_timeout_ms=1,
)
assert info is not None
assert exc.value.timeout_type in {"connect", "read"}
Expand All @@ -74,37 +76,41 @@ def test_cursor_overalltimeout_exceptions_sync(
col = sync_empty_collection
col.insert_many([{"a": 1}] * 1000)

col.distinct("a", timeout_ms=20000)
col.distinct("a", general_method_timeout_ms=20000)
with pytest.raises(DataAPITimeoutException):
col.distinct("a", timeout_ms=1)

col.distinct("a", timeout_ms=20000)
col.distinct("a", general_method_timeout_ms=20000)
with pytest.raises(DataAPITimeoutException):
col.distinct("a", timeout_ms=1)
col.distinct("a", general_method_timeout_ms=1)

@pytest.mark.describe("test of insert_many timeouts, sync")
def test_insert_many_timeout_exceptions_sync(
self,
sync_collection: DefaultCollection,
) -> None:
fifty_docs = [{"seq": i} for i in range(50)]
sync_collection.insert_many(fifty_docs, ordered=True, timeout_ms=20000)
sync_collection.insert_many(
fifty_docs, ordered=False, concurrency=1, timeout_ms=20000
fifty_docs, ordered=True, general_method_timeout_ms=20000
)
sync_collection.insert_many(
fifty_docs, ordered=False, concurrency=1, general_method_timeout_ms=20000
)
sync_collection.insert_many(
fifty_docs, ordered=False, concurrency=2, timeout_ms=20000
fifty_docs, ordered=False, concurrency=2, general_method_timeout_ms=20000
)

with pytest.raises(DataAPITimeoutException):
sync_collection.insert_many(fifty_docs, ordered=True, timeout_ms=2)
sync_collection.insert_many(
fifty_docs, ordered=True, general_method_timeout_ms=2
)
with pytest.raises(DataAPITimeoutException):
sync_collection.insert_many(
fifty_docs, ordered=False, concurrency=1, timeout_ms=2
fifty_docs, ordered=False, concurrency=1, general_method_timeout_ms=2
)
with pytest.raises(DataAPITimeoutException):
sync_collection.insert_many(
fifty_docs, ordered=False, concurrency=2, timeout_ms=2
fifty_docs, ordered=False, concurrency=2, general_method_timeout_ms=2
)

@pytest.mark.describe("test of update_many timeouts, sync")
Expand All @@ -117,12 +123,16 @@ def test_update_many_timeout_exceptions_sync(

sync_collection.update_many({"f": "update_many"}, {"$inc": {"seq": 100}})
sync_collection.update_many(
{"f": "update_many"}, {"$inc": {"seq": 100}}, timeout_ms=20000
{"f": "update_many"},
{"$inc": {"seq": 100}},
general_method_timeout_ms=20000,
)

with pytest.raises(DataAPITimeoutException):
sync_collection.update_many(
{"f": "update_many"}, {"$inc": {"seq": 100}}, timeout_ms=2
{"f": "update_many"},
{"$inc": {"seq": 100}},
general_method_timeout_ms=2,
)

@pytest.mark.describe("test of delete_many timeouts, sync")
Expand All @@ -140,9 +150,13 @@ def test_delete_many_timeout_exceptions_sync(
)

sync_collection.delete_many({"f": "delete_many1"})
sync_collection.delete_many({"f": "delete_many2"}, timeout_ms=20000)
sync_collection.delete_many(
{"f": "delete_many2"}, general_method_timeout_ms=20000
)
with pytest.raises(DataAPITimeoutException):
sync_collection.delete_many({"f": "delete_many3"}, timeout_ms=2)
sync_collection.delete_many(
{"f": "delete_many3"}, general_method_timeout_ms=2
)

@pytest.mark.describe("test of collection find-with-collective timeout, sync")
def test_collection_find_with_collective_timeout_sync(
Expand Down
12 changes: 6 additions & 6 deletions tests/base/unit/test_collection_timeouts.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def test_collection_count_documents_timeout_sync(
).respond_with_handler(response_sleeper)
with pytest.raises(DataAPITimeoutException):
mock_collection.count_documents(
{}, upper_bound=800, timeout_ms=TIMEOUT_PARAM_MS
{}, upper_bound=800, general_method_timeout_ms=TIMEOUT_PARAM_MS
)

@pytest.mark.describe("test of collection count_documents timeout, async")
Expand All @@ -91,7 +91,7 @@ async def test_collection_count_documents_timeout_async(
).respond_with_handler(response_sleeper)
with pytest.raises(DataAPITimeoutException):
await mock_acollection.count_documents(
{}, upper_bound=800, timeout_ms=TIMEOUT_PARAM_MS
{}, upper_bound=800, general_method_timeout_ms=TIMEOUT_PARAM_MS
)

@pytest.mark.describe("test of collection cursor-based timeouts, async")
Expand All @@ -107,7 +107,7 @@ async def test_collection_cursor_timeouts_async(
cur0 = mock_acollection.find({})
await cur0.__anext__()

cur1 = mock_acollection.find({}, timeout_ms=1)
cur1 = mock_acollection.find({}, request_timeout_ms=1)
httpserver.expect_oneshot_request(
f"/{BASE_PATH}/{PATH_SUFFIX}",
method=HttpMethod.POST,
Expand All @@ -132,7 +132,7 @@ async def test_collection_find_one_timeouts_async(
method=HttpMethod.POST,
).respond_with_handler(response_sleeper)
with pytest.raises(DataAPITimeoutException):
await mock_acollection.find_one({}, timeout_ms=1)
await mock_acollection.find_one({}, general_method_timeout_ms=1)

@pytest.mark.describe("test of collection cursor-based timeouts, sync")
def test_collection_cursor_timeouts_sync(
Expand All @@ -147,7 +147,7 @@ def test_collection_cursor_timeouts_sync(
cur0 = mock_collection.find({})
cur0.__next__()

cur1 = mock_collection.find({}, timeout_ms=1)
cur1 = mock_collection.find({}, request_timeout_ms=1)
httpserver.expect_oneshot_request(
f"/{BASE_PATH}/{PATH_SUFFIX}",
method=HttpMethod.POST,
Expand All @@ -172,4 +172,4 @@ def test_collection_find_one_timeouts_sync(
method=HttpMethod.POST,
).respond_with_handler(response_sleeper)
with pytest.raises(DataAPITimeoutException):
mock_collection.find_one({}, timeout_ms=1)
mock_collection.find_one({}, general_method_timeout_ms=1)
8 changes: 4 additions & 4 deletions tests/base/unit/test_timeouts.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ def test_collection_timeout_suppression_sync(self, httpserver: HTTPServer) -> No
dmr_LS_zrq = collection_L_S.delete_many({}, general_method_timeout_ms=0)
assert dmr_LS_zrq.deleted_count == 12

# remove the timeout completely with a zero per-method 'timeout_ms' shorthand
# remove the timeout completely: a zero per-method 'general_method_timeout_ms'
httpserver.expect_oneshot_request(
expected_url,
method="POST",
Expand All @@ -313,7 +313,7 @@ def test_collection_timeout_suppression_sync(self, httpserver: HTTPServer) -> No
},
)
)
dmr_LS_zgrq = collection_L_S.delete_many({}, timeout_ms=0)
dmr_LS_zgrq = collection_L_S.delete_many({}, general_method_timeout_ms=0)
assert dmr_LS_zgrq.deleted_count == 12

@pytest.mark.describe(
Expand Down Expand Up @@ -491,7 +491,7 @@ async def test_collection_timeout_suppression_async(
dmr_LS_zrq = await acollection_L_S.delete_many({}, general_method_timeout_ms=0)
assert dmr_LS_zrq.deleted_count == 12

# remove the timeout completely with a zero per-method 'timeout_ms' shorthand
# remove the timeout completely: a zero per-method 'general_method_timeout_ms'
httpserver.expect_oneshot_request(
expected_url,
method="POST",
Expand All @@ -506,5 +506,5 @@ async def test_collection_timeout_suppression_async(
},
)
)
dmr_LS_zgrq = await acollection_L_S.delete_many({}, timeout_ms=0)
dmr_LS_zgrq = await acollection_L_S.delete_many({}, general_method_timeout_ms=0)
assert dmr_LS_zgrq.deleted_count == 12

0 comments on commit c5e627b

Please sign in to comment.