@@ -114,64 +114,52 @@ unsigned long long extract_count_from_influxdb_response(std::string const& respo
114114 return 0 ;
115115}
116116
117- bool connected_test::wait_for_async_inserts (unsigned long long expected_count, std::string const & table_name)
117+ bool connected_test::wait_for_async_inserts (unsigned long long expected_count, std::string const & table_name, unsigned long long tolerance )
118118{
119119 // Async API batches inserts, so we need to poll until count matches
120120 auto query = std::string (" select count(*) from " ) + db_name + " .." + table_name;
121121
122+ // Default tolerance: 0.1% or 50 entries, whichever is larger
123+ if (tolerance == 0 ) {
124+ tolerance = std::max (expected_count / 1000 , 50ULL );
125+ }
126+
122127 unsigned long long current_count = 0 ;
128+ std::cout << " Waiting for " << expected_count << " async inserts (tolerance: " << tolerance << " )..." << std::endl;
123129 unsigned retries = 0 ;
124- const unsigned max_retries = 1000 ; // Much longer timeout for Windows async batching (up to ~100 seconds with exponential backoff)
125- unsigned wait_ms = 100 ;
126- const unsigned max_wait_ms = 1000 ; // Cap at 1 second between checks
127-
128- std::cout << " Waiting for all " << expected_count << " async inserts to arrive..." << std::endl;
130+ const unsigned max_retries = 200 ; // Reasonable timeout (~20 seconds with 100ms waits)
129131
130132 while (current_count < expected_count && retries < max_retries) {
131- std::this_thread::sleep_for (std::chrono::milliseconds (wait_ms ));
133+ std::this_thread::sleep_for (std::chrono::milliseconds (100 ));
132134 retries++;
133135
134136 try {
135137 auto response = raw_db.get (query);
136138 current_count = extract_count_from_influxdb_response (response);
137139
138- // Log more frequently when close to target (within 1% or 1000 entries)
139- bool is_close = current_count > 0 && (expected_count - current_count) <= std::max (expected_count / 100 , 1000ULL );
140- bool should_log = (retries % 50 == 0 ) || (current_count > 0 && retries < 10 ) || (is_close && retries % 5 == 0 );
141-
142- if (should_log) {
143- std::cout << " Poll attempt " << retries << " /" << max_retries
144- << " : " << current_count << " /" << expected_count << " entries arrived" ;
145- if (is_close && current_count < expected_count) {
146- std::cout << " (" << (expected_count - current_count) << " remaining, waiting for final batch...)" ;
147- }
148- std::cout << std::endl;
149- }
150-
151- // Accept if we're very close (within 0.1% or 10 entries) - async batching can leave tiny remainder
152- unsigned long long tolerance = std::max (expected_count / 1000 , 10ULL );
153140 if (current_count >= expected_count) {
154- std::cout << " ✓ All " << expected_count << " entries arrived after " << retries << " polling attempts!" << std::endl;
155141 return true ;
156- } else if (current_count + tolerance >= expected_count && retries >= 50 ) {
157- // After 50 retries, accept if we're within tolerance
158- std::cout << " ✓ " << current_count << " / " << expected_count << " entries arrived (within tolerance of " << tolerance << " ) after " << retries << " polling attempts! " << std::endl;
142+ }
143+ // Accept if within tolerance after reasonable wait ( 50 retries = 5 seconds)
144+ if (retries >= 50 && current_count + tolerance >= expected_count) {
159145 return true ;
160146 }
161147 } catch (const std::exception& e) {
162- // Query might fail early if no data yet, keep trying
163- if (retries % 50 == 0 ) {
164- std::cout << " Poll attempt " << retries << " : Query failed (no data yet), continuing..." << std::endl;
165- }
166- }
167-
168- // Exponential backoff: increase wait time gradually, cap at max_wait_ms
169- wait_ms += 10 ;
170- if (wait_ms > max_wait_ms) {
171- wait_ms = max_wait_ms;
148+ // Query might fail early, keep trying
172149 }
173150 }
174151
175- std::cout << " ✗ Timeout: Only " << current_count << " /" << expected_count << " entries arrived after " << retries << " attempts" << std::endl;
176- return false ;
152+ // Final check: get latest count and check tolerance
153+ try {
154+ auto response = raw_db.get (query);
155+ current_count = extract_count_from_influxdb_response (response);
156+ } catch (const std::exception& e) {
157+ // If query fails, use last known count
158+ }
159+
160+ bool within_tolerance = (current_count + tolerance >= expected_count);
161+ std::cout << " Final check: " << current_count << " /" << expected_count
162+ << " entries (tolerance: " << tolerance << " , within: " << (within_tolerance ? " yes" : " no" ) << " )" << std::endl;
163+
164+ return within_tolerance;
177165}
0 commit comments