@@ -363,16 +363,25 @@ def test_batch_write_with_ordering_key(self):
363363 # Retry pulling to handle PubSub delivery delays
364364 received_messages = []
365365 received_message_ids = set ()
366- ack_ids = []
367366 deadline = time .time () + 60 # wait up to 60 seconds
368367 while time .time () < deadline :
369368 response = self .sub_client .pull (
370369 request = {
371370 'subscription' : ordering_sub .name ,
372371 'max_messages' : 10 ,
373372 })
373+ # Acknowledge received messages immediately. This is critical when message
374+ # ordering is enabled because outstanding (unacknowledged) messages block
375+ # the delivery of subsequent messages with the same ordering key.
376+ ack_ids = [msg .ack_id for msg in response .received_messages ]
377+ if ack_ids :
378+ self .sub_client .acknowledge (
379+ request = {
380+ 'subscription' : ordering_sub .name ,
381+ 'ack_ids' : ack_ids ,
382+ })
383+
374384 for msg in response .received_messages :
375- ack_ids .append (msg .ack_id )
376385 # Pub/Sub guarantees at-least-once delivery, so we must deduplicate
377386 # messages by message_id to handle potential duplicate deliveries.
378387 if msg .message .message_id not in received_message_ids :
@@ -391,13 +400,6 @@ def test_batch_write_with_ordering_key(self):
391400 self .assertEqual (received_map [b'order_data001' ].ordering_key , 'key1' )
392401 self .assertEqual (received_map [b'order_data002' ].ordering_key , 'key1' )
393402 self .assertEqual (received_map [b'order_data003' ].ordering_key , 'key2' )
394-
395- if ack_ids :
396- self .sub_client .acknowledge (
397- request = {
398- 'subscription' : ordering_sub .name ,
399- 'ack_ids' : ack_ids ,
400- })
401403 finally :
402404 self .sub_client .delete_subscription (
403405 request = {'subscription' : ordering_sub .name })
0 commit comments