@@ -2408,6 +2408,20 @@ func (s *SQLQueueIntegrationSuite) TestWatermarkAdvancesContiguously() {
24082408 return - 1
24092409 }
24102410
2411+ // Watermark advancement is incremental and runs in the subscriber poll loop.
2412+ // A delivery-check signal proves one poll completed, but the watermark may
2413+ // need another poll to converge after several acknowledgements.
2414+ waitForLag := func (expected int64 ) int64 {
2415+ for {
2416+ lag := getLag ()
2417+ require .GreaterOrEqual (t , lag , expected , "watermark advanced past expected lag" )
2418+ if lag == expected {
2419+ return lag
2420+ }
2421+ waitForSignal (t , signalCh , queueMySQL .SignalDeliveryCheck )
2422+ }
2423+ }
2424+
24112425 // Ack message 3 first (out of order)
24122426 require .NoError (t , deliveries ["wm-msg-3" ].Ack (s .ctx ))
24132427 t .Logf ("Acked msg-3" )
@@ -2417,31 +2431,24 @@ func (s *SQLQueueIntegrationSuite) TestWatermarkAdvancesContiguously() {
24172431 require .NoError (t , deliveries ["wm-msg-2" ].Ack (s .ctx ))
24182432 t .Logf ("Acked msg-1 and msg-2" )
24192433
2420- // Wait for poll loop to advance watermark
2421- waitForSignal (t , signalCh , queueMySQL .SignalDeliveryCheck )
2422-
24232434 // After acking 1,2,3: watermark should advance to 3, lag should be 2 (msg-4, msg-5)
2424- lag := getLag ( )
2435+ lag := waitForLag ( 2 )
24252436 assert .Equal (t , int64 (2 ), lag , "lag should be 2 after acking 1,2,3 (4 and 5 remain)" )
24262437 t .Logf ("After acking 1,2,3: lag=%d" , lag )
24272438
24282439 // Ack message 5 (skip 4) — watermark should NOT advance past 3
24292440 require .NoError (t , deliveries ["wm-msg-5" ].Ack (s .ctx ))
24302441 t .Logf ("Acked msg-5 (skipping msg-4)" )
24312442
2432- waitForSignal (t , signalCh , queueMySQL .SignalDeliveryCheck )
2433-
2434- lag = getLag ()
2443+ lag = waitForLag (2 )
24352444 assert .Equal (t , int64 (2 ), lag , "lag should still be 2 after acking 5 but not 4" )
24362445 t .Logf ("After acking 5 (not 4): lag=%d" , lag )
24372446
24382447 // Ack message 4 — now all 5 are contiguous, watermark should advance to 5
24392448 require .NoError (t , deliveries ["wm-msg-4" ].Ack (s .ctx ))
24402449 t .Logf ("Acked msg-4" )
24412450
2442- waitForSignal (t , signalCh , queueMySQL .SignalDeliveryCheck )
2443-
2444- lag = getLag ()
2451+ lag = waitForLag (0 )
24452452 assert .Equal (t , int64 (0 ), lag , "lag should be 0 after acking all 5 messages" )
24462453 t .Logf ("After acking all 5: lag=%d" , lag )
24472454
0 commit comments