From 18664addf4f61079c1c6595eecc6bcb6e020a8f1 Mon Sep 17 00:00:00 2001 From: dragonknightk <37928252+dragonknightk@users.noreply.github.com> Date: Sat, 28 Nov 2026 10:01:39 +0800 Subject: [PATCH] fix: apply pattern_auto_discovery_period in Client.subscribe Closes issue 171 ### Motivation `Client.subscribe` accepts `pattern_auto_discovery_period` and checks its type, but never passes it to the C++ `ConsumerConfiguration`. A regex consumer therefore always used the C++ default of 60 seconds, whatever value the caller set. ### Modifications Python wrapper (`pulsar/`): `Client.subscribe` now calls `conf.pattern_auto_discovery_period(...)`. The pybind11 binding in `src/config.cc` already exposes this setter, so no C++ change is needed. The value goes straight to the C++ setter, so the C++ rule applies unchanged: a value of 0 or less turns auto discovery off. Tests (`tests/`): `test_topics_pattern_consumer` created all three topics before subscribing, so they were found at subscribe time and the period never mattered. It now creates the third topic after subscribing, so the consumer only gets it through auto discovery. The consumer uses `InitialPosition.Earliest` so messages sent before the new topic is discovered are not lost. ### Verifying this change `test_topics_pattern_consumer` fails on the old code, where the third topic is not found within the receive timeout, and passes with the fix. Signed-off-by: dragonknightk <37928252+dragonknightk@users.noreply.github.com> --- pulsar/__init__.py | 1 + tests/pulsar_test.py | 12 ++++++++---- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/pulsar/__init__.py b/pulsar/__init__.py index 96d80d9..f1f2b87 100644 --- a/pulsar/__init__.py +++ b/pulsar/__init__.py @@ -1398,6 +1398,7 @@ def my_listener(consumer, message): conf = _pulsar.ConsumerConfiguration() conf.consumer_type(consumer_type) conf.regex_subscription_mode(regex_subscription_mode) + conf.pattern_auto_discovery_period(pattern_auto_discovery_period) conf.read_compacted(is_read_compacted) if message_listener: conf.message_listener(_listener_wrapper(message_listener, schema)) diff --git a/tests/pulsar_test.py b/tests/pulsar_test.py index 68eb499..d830076 100755 --- a/tests/pulsar_test.py +++ b/tests/pulsar_test.py @@ -1366,11 +1366,9 @@ def test_topics_pattern_consumer(self): doHttpPut(url1, "2") doHttpPut(url2, "3") - doHttpPut(url3, "4") producer1 = client.create_producer(topic1) producer2 = client.create_producer(topic2) - producer3 = client.create_producer(topic3) consumer = client.subscribe( re.compile(topics_pattern), @@ -1378,10 +1376,16 @@ def test_topics_pattern_consumer(self): consumer_type=ConsumerType.Shared, receiver_queue_size=10, pattern_auto_discovery_period=1, + initial_position=InitialPosition.Earliest, ) - # wait enough time to trigger auto discovery - time.sleep(2) + # topic3 is created after the subscription, so the consumer can only get it through auto discovery. + # Expected: found about 1 second after the subscription since pattern_auto_discovery_period=1. Before + # the fix the period was never applied, so the C++ default of 60 seconds was used (issue 171). + # There is no fixed sleep: the receive() calls below wait up to TM (10 seconds) for each message, + # which is far below 60 seconds, so the test still times out without the fix. + doHttpPut(url3, "4") + producer3 = client.create_producer(topic3) for i in range(100): producer1.send(b"hello-1-%d" % i)