11import asyncio
22import time
33import uuid
4- from typing import Optional
54from collections import OrderedDict
65from datetime import datetime , timedelta , timezone
76from unittest import mock
1211from .http .httpclienttestbase import HttpClientTestBase , MockResponse
1312from ..anyorderlist import AnyOrderList
1413
15- def _wait_for_call_count (mock_obj , expected : int , * , timeout : float = 3.0 , min_elapsed : Optional [float ] = None ):
1614
15+ def _wait_for_call_count (mock_obj , expected : int , * , timeout : float = 3.0 ):
1716 """Wait until mock_obj.call_count >= expected or timeout.
1817
1918 Args:
2019 mock_obj: Mock with call_count attribute.
2120 expected: Target call count (>=).
2221 timeout: Max seconds to wait.
23- min_elapsed: If provided, fail if the condition is met before this many seconds (guards against premature flushes).
2422
2523 Returns:
2624 Elapsed seconds when condition satisfied.
@@ -30,17 +28,11 @@ def _wait_for_call_count(mock_obj, expected: int, *, timeout: float = 3.0, min_e
3028 """
3129 start = time .monotonic ()
3230 while True :
33- current = mock_obj .call_count
34- if current >= expected :
35- elapsed = time .monotonic () - start
36- if min_elapsed is not None and elapsed < min_elapsed :
37- raise AssertionError (
38- f"Condition reached too early: call_count={ current } elapsed={ elapsed :.4f} s < min_elapsed={ min_elapsed :.4f} s"
39- )
40- return elapsed
31+ if mock_obj .call_count >= expected :
32+ return time .monotonic () - start
4133 if time .monotonic () - start >= timeout :
4234 raise AssertionError (
43- f"Timed out waiting for call_count >= { expected } . Final={ current } timeout={ timeout } s"
35+ f"Timed out waiting for call_count >= { expected } . Final={ mock_obj . call_count } timeout={ timeout } s"
4436 )
4537 time .sleep (0.01 )
4638
@@ -2413,22 +2405,20 @@ def test__create_writer_with_buffer_time__sends_when_timer_elapsed(self):
24132405 path = "tag"
24142406 value = 1
24152407 buffer_ms = 50
2416- writer = self ._uut .create_writer (max_buffer_time = timedelta (milliseconds = buffer_ms ))
2408+ writer = self ._uut .create_writer (
2409+ max_buffer_time = timedelta (milliseconds = buffer_ms )
2410+ )
24172411 timestamp = datetime .now ()
2418- self ._client .all_requests .configure_mock (side_effect = self ._get_mock_request ([None ]))
2412+ self ._client .all_requests .configure_mock (
2413+ side_effect = self ._get_mock_request ([None ])
2414+ )
24192415
24202416 writer .write (path , tbase .DataType .INT32 , value , timestamp = timestamp )
24212417 self ._client .all_requests .assert_not_called ()
24222418
2423- warm = buffer_ms / 1000 * 2.5 # generous warm period for slow CI start
2424- time .sleep (warm )
2419+ time .sleep (buffer_ms / 1000 * 2.5 )
24252420 if self ._client .all_requests .call_count == 0 :
2426- _wait_for_call_count (
2427- self ._client .all_requests ,
2428- 1 ,
2429- timeout = 5.0 ,
2430- min_elapsed = buffer_ms / 1000 * 0.4 , # at least 40% of interval to avoid racey early flush
2431- )
2421+ _wait_for_call_count (self ._client .all_requests , 1 , timeout = 5.0 )
24322422
24332423 utctime = (
24342424 datetime .fromtimestamp (timestamp .timestamp (), timezone .utc )
@@ -2498,16 +2488,12 @@ def test__create_writer_with_buffer_size_and_timer__obeys_both_settings(self):
24982488 )
24992489
25002490 writer2 .write (path , tbase .DataType .INT32 , value3 , timestamp = timestamp )
2501- assert 1 == self ._client .all_requests .call_count
2502- # Use longer warm period in CI to allow thread scheduling
2491+ assert 1 == self ._client .all_requests .call_count # same as before
2492+
25032493 time .sleep (buffer_ms / 1000 * 2.5 )
25042494 if self ._client .all_requests .call_count == 1 :
2505- _wait_for_call_count (
2506- self ._client .all_requests ,
2507- 2 ,
2508- timeout = 5.0 ,
2509- min_elapsed = buffer_ms / 1000 * 0.4 ,
2510- )
2495+ _wait_for_call_count (self ._client .all_requests , 2 , timeout = 5.0 )
2496+
25112497 assert 2 == self ._client .all_requests .call_count
25122498 assert self ._client .all_requests .call_args_list [1 ] == mock .call (
25132499 "POST" ,
0 commit comments