Skip to content

Commit b44f8e0

Browse files
author
Nikita Grover
committed
Fix PubSub tests: enable message ordering in PublisherClient to exercise full ordering flow
1 parent 7651201 commit b44f8e0

2 files changed

Lines changed: 16 additions & 4 deletions

File tree

sdks/python/apache_beam/io/gcp/pubsub.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,8 @@ def __init__(
389389
topic: str,
390390
with_attributes: bool = False,
391391
id_label: Optional[str] = None,
392-
timestamp_attribute: Optional[str] = None) -> None:
392+
timestamp_attribute: Optional[str] = None,
393+
enable_message_ordering: bool = False) -> None:
393394
"""Initializes ``WriteToPubSub``.
394395
395396
Args:
@@ -405,9 +406,13 @@ def __init__(
405406
in a ReadFromPubSub PTransform to deduplicate messages.
406407
timestamp_attribute: If set, will set an attribute for each Cloud Pub/Sub
407408
message with the given name and the message's publish time as the value.
409+
enable_message_ordering: If True, enables message ordering on the
410+
PublisherClient. Messages with an ordering_key will be delivered
411+
in order. Requires messages to have ordering_key set.
408412
"""
409413
super().__init__()
410414
self.with_attributes = with_attributes
415+
self.enable_message_ordering = enable_message_ordering
411416
self.id_label = id_label
412417
self.timestamp_attribute = timestamp_attribute
413418
self.project, self.topic_name = parse_topic(topic)
@@ -467,6 +472,9 @@ def display_data(self):
467472
True, label='With Attributes').drop_if_none(),
468473
'timestamp_attribute': DisplayDataItem(
469474
self.timestamp_attribute, label='Timestamp Attribute'),
475+
'enable_message_ordering': DisplayDataItem(
476+
self.enable_message_ordering,
477+
label='Enable Message Ordering').drop_if_none(),
470478
}
471479

472480

@@ -573,7 +581,7 @@ def __init__(self, transform):
573581
self.id_label = transform.id_label
574582
self.timestamp_attribute = transform.timestamp_attribute
575583
self.with_attributes = transform.with_attributes
576-
self.with_ordering = transform.with_attributes
584+
self.with_ordering = transform.enable_message_ordering
577585

578586
# TODO(https://github.com/apache/beam/issues/18939): Add support for
579587
# id_label and timestamp_attribute.

sdks/python/apache_beam/io/gcp/pubsub_test.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,7 +1112,9 @@ def test_write_messages_with_ordering_key(self, mock_pubsub):
11121112
p
11131113
| Create(payloads)
11141114
| WriteToPubSub(
1115-
'projects/fakeprj/topics/a_topic', with_attributes=True))
1115+
'projects/fakeprj/topics/a_topic',
1116+
with_attributes=True,
1117+
enable_message_ordering=True))
11161118

11171119
# Verify that publish was called with ordering_key
11181120
mock_pubsub.return_value.publish.assert_called()
@@ -1135,7 +1137,9 @@ def test_write_messages_with_ordering_key_no_attributes(self, mock_pubsub):
11351137
p
11361138
| Create(payloads)
11371139
| WriteToPubSub(
1138-
'projects/fakeprj/topics/a_topic', with_attributes=True))
1140+
'projects/fakeprj/topics/a_topic',
1141+
with_attributes=True,
1142+
enable_message_ordering=True))
11391143

11401144
# Verify that publish was called with ordering_key
11411145
mock_pubsub.return_value.publish.assert_called()

0 commit comments

Comments
 (0)