Skip to content

Commit e11ff45

Browse files
committed
test: replace sleep with assert_eventually for rate reset verification
1 parent 1641f95 commit e11ff45

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

tests/asyncio/test_flow_control.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from qstash import AsyncQStash
77
from qstash.flow_control_api import GlobalParallelismInfo
88
from qstash.message import FlowControl, PublishResponse
9+
from tests import assert_eventually_async
910

1011

1112
@pytest.mark.asyncio
@@ -191,12 +192,13 @@ async def test_flow_control_reset_rate_async(async_client: AsyncQStash) -> None:
191192
# Reset the rate
192193
await async_client.flow_control.reset_rate(flow_control_key)
193194

194-
# sleep 1 second
195-
await asyncio.sleep(1)
195+
# We use assert_eventually because reset_rate doesn't take effect immediately.
196+
# Not ideal but it's what we have.
197+
async def check_rate_reset() -> None:
198+
info = await async_client.flow_control.get(flow_control_key)
199+
assert info.rate_count == 0
196200

197-
# Verify rate was reset by checking the flow control info
198-
info = await async_client.flow_control.get(flow_control_key)
199-
assert info.rate_count == 0
201+
await assert_eventually_async(check_rate_reset)
200202

201203
# Clean up
202204
await async_client.message.cancel(result.message_id)

tests/test_flow_control.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from qstash import QStash
44
from qstash.flow_control_api import GlobalParallelismInfo
55
from qstash.message import FlowControl, PublishResponse
6+
from tests import assert_eventually
67

78

89
def test_flow_control_get(client: QStash) -> None:
@@ -180,12 +181,13 @@ def test_flow_control_reset_rate(client: QStash) -> None:
180181
# Reset the rate
181182
client.flow_control.reset_rate(flow_control_key)
182183

183-
# sleep 1 second
184-
time.sleep(1)
184+
# We use assert_eventually because reset_rate doesn't take effect immediately.
185+
# Not ideal but it's what we have.
186+
def check_rate_reset() -> None:
187+
info = client.flow_control.get(flow_control_key)
188+
assert info.rate_count == 0
185189

186-
# Verify rate was reset by checking the flow control info
187-
info = client.flow_control.get(flow_control_key)
188-
assert info.rate_count == 0
190+
assert_eventually(check_rate_reset)
189191

190192
# Clean up
191193
client.message.cancel(result.message_id)

0 commit comments

Comments
 (0)