@@ -308,7 +308,9 @@ def test_batch_write_with_attributes(self):
308308 @pytest .mark .it_postcommit
309309 def test_batch_write_with_ordering_key (self ):
310310 """Test WriteToPubSub in batch mode with ordering keys.
311- Dataflow Native PubSub Sink does not support ordering_key, therefore this
311+
312+ Dataflow Native PubSub Sink does not support ordering_key
313+ (https://github.com/apache/beam/issues/36201), therefore this
312314 test only applies to runners that use Beam's Python WriteToPubSub Sink.
313315 To use ordering_key, Dataflow users should use the XLang WriteToPubSub
314316 path instead.
@@ -325,44 +327,62 @@ def test_batch_write_with_ordering_key(self):
325327 from apache_beam .options .pipeline_options import StandardOptions
326328 from apache_beam .transforms import Create
327329
328- # Create test messages with ordering keys
329- test_messages = [
330- PubsubMessage (
331- b'order_data001' , {'attr' : 'value1' }, ordering_key = 'key1' ),
332- PubsubMessage (
333- b'order_data002' , {'attr' : 'value2' }, ordering_key = 'key1' ),
334- PubsubMessage (
335- b'order_data003' , {'attr' : 'value3' }, ordering_key = 'key2' )
336- ]
337-
338- pipeline_options = PipelineOptions ()
339- pipeline_options .view_as (StandardOptions ).streaming = False
330+ ordering_topic = self .pub_client .create_topic (
331+ name = self .pub_client .topic_path (
332+ self .project , 'psit_topic_ordering' + self .uuid ))
333+ ordering_sub = self .sub_client .create_subscription (
334+ name = self .sub_client .subscription_path (
335+ self .project , 'psit_sub_ordering' + self .uuid ),
336+ topic = ordering_topic .name ,
337+ enable_message_ordering = True )
338+ time .sleep (10 )
340339
341- with TestPipeline (options = pipeline_options ) as p :
342- messages = p | 'CreateMessages' >> Create (test_messages )
343- _ = messages | 'WriteToPubSub' >> WriteToPubSub (
344- self .output_topic .name , with_attributes = True )
340+ try :
341+ test_messages = [
342+ PubsubMessage (
343+ b'order_data001' , {'attr' : 'value1' }, ordering_key = 'key1' ),
344+ PubsubMessage (
345+ b'order_data002' , {'attr' : 'value2' }, ordering_key = 'key1' ),
346+ PubsubMessage (
347+ b'order_data003' , {'attr' : 'value3' }, ordering_key = 'key2' )
348+ ]
345349
346- # Verify messages were published
347- time . sleep ( 10 )
350+ pipeline_options = PipelineOptions ()
351+ pipeline_options . view_as ( StandardOptions ). streaming = False
348352
349- response = self .sub_client .pull (
350- request = {
351- "subscription" : self .output_sub .name ,
352- "max_messages" : 10 ,
353- })
353+ with TestPipeline (options = pipeline_options ) as p :
354+ messages = p | 'CreateMessages' >> Create (test_messages )
355+ _ = messages | 'WriteToPubSub' >> WriteToPubSub (
356+ ordering_topic .name , with_attributes = True )
354357
355- self . assertEqual ( len ( response . received_messages ), len ( test_messages ) )
358+ time . sleep ( 10 )
356359
357- # Verify ordering keys were preserved
358- for received_message in response .received_messages :
359- self .assertIn ('ordering_key' , dir (received_message .message ))
360- self .sub_client .acknowledge (
360+ response = self .sub_client .pull (
361361 request = {
362- "subscription" : self . output_sub .name ,
363- "ack_ids " : [ received_message . ack_id ] ,
362+ "subscription" : ordering_sub .name ,
363+ "max_messages " : 10 ,
364364 })
365365
366+ self .assertEqual (len (response .received_messages ), len (test_messages ))
367+
368+ received_map = {
369+ msg .message .data : msg .message
370+ for msg in response .received_messages
371+ }
372+ self .assertEqual (received_map [b'order_data001' ].ordering_key , 'key1' )
373+ self .assertEqual (received_map [b'order_data002' ].ordering_key , 'key1' )
374+ self .assertEqual (received_map [b'order_data003' ].ordering_key , 'key2' )
375+
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+ finally :
384+ test_utils .cleanup_subscriptions (self .sub_client , [ordering_sub ])
385+ test_utils .cleanup_topics (self .pub_client , [ordering_topic ])
366386
367387if __name__ == '__main__' :
368388 logging .getLogger ().setLevel (logging .DEBUG )
0 commit comments