|
24 | 24 | from unittest import TestCase, main
|
25 | 25 | import time
|
26 | 26 | import os
|
| 27 | +import re |
27 | 28 | import pulsar
|
28 | 29 | import uuid
|
29 | 30 | from datetime import timedelta
|
|
46 | 47 | )
|
47 | 48 | from pulsar.schema import JsonSchema, Record, Integer
|
48 | 49 |
|
49 |
| -from _pulsar import ProducerConfiguration, ConsumerConfiguration |
| 50 | +from _pulsar import ProducerConfiguration, ConsumerConfiguration, RegexSubscriptionMode |
50 | 51 |
|
51 | 52 | from schema_test import *
|
52 | 53 |
|
@@ -1100,7 +1101,6 @@ def test_topics_consumer(self):
|
1100 | 1101 | client.close()
|
1101 | 1102 |
|
1102 | 1103 | def test_topics_pattern_consumer(self):
|
1103 |
| - import re |
1104 | 1104 |
|
1105 | 1105 | client = Client(self.serviceUrl)
|
1106 | 1106 |
|
@@ -1717,6 +1717,77 @@ def test_batch_index_ack(self):
|
1717 | 1717 |
|
1718 | 1718 | client.close()
|
1719 | 1719 |
|
| 1720 | + def test_regex_subscription(self): |
| 1721 | + client = Client(self.serviceUrl) |
| 1722 | + topic1 = "persistent://public/default/test-regex-sub-1" |
| 1723 | + topic2 = "persistent://public/default/test-regex-sub-2" |
| 1724 | + topic3 = "non-persistent://public/default/test-regex-sub-3" |
| 1725 | + topic4 = "persistent://public/default/no-match-test-regex-sub-3" # no match pattern rule topic. |
| 1726 | + |
| 1727 | + producer1 = client.create_producer(topic1) |
| 1728 | + producer2 = client.create_producer(topic2) |
| 1729 | + producer3 = client.create_producer(topic3) |
| 1730 | + producer4 = client.create_producer(topic4) |
| 1731 | + |
| 1732 | + consumer_all = client.subscribe( |
| 1733 | + re.compile('public/default/test-regex-sub-.*'), "regex-sub-all", |
| 1734 | + consumer_type=ConsumerType.Shared, regex_subscription_mode=RegexSubscriptionMode.AllTopics |
| 1735 | + ) |
| 1736 | + |
| 1737 | + consumer_persistent = client.subscribe( |
| 1738 | + re.compile('public/default/test-regex-sub-.*'), "regex-sub-persistent", |
| 1739 | + consumer_type=ConsumerType.Shared, regex_subscription_mode=RegexSubscriptionMode.PersistentOnly |
| 1740 | + ) |
| 1741 | + |
| 1742 | + consumer_non_persistent = client.subscribe( |
| 1743 | + re.compile('public/default/test-regex-sub-.*'), "regex-sub-non-persistent", |
| 1744 | + consumer_type=ConsumerType.Shared, regex_subscription_mode=RegexSubscriptionMode.NonPersistentOnly |
| 1745 | + ) |
| 1746 | + |
| 1747 | + num = 10 |
| 1748 | + for i in range(num): |
| 1749 | + producer1.send(b"hello-1-%d" % i) |
| 1750 | + producer2.send(b"hello-2-%d" % i) |
| 1751 | + producer3.send(b"hello-3-%d" % i) |
| 1752 | + producer4.send(b"hello-4-%d" % i) |
| 1753 | + |
| 1754 | + # Assert consumer_all. |
| 1755 | + received_topics = set() |
| 1756 | + for i in range(3 * num): |
| 1757 | + msg = consumer_all.receive(TM) |
| 1758 | + topic_name = msg.topic_name() |
| 1759 | + self.assertIn(topic_name, [topic1, topic2, topic3]) |
| 1760 | + received_topics.add(topic_name) |
| 1761 | + consumer_all.acknowledge(msg) |
| 1762 | + self.assertEqual(received_topics, {topic1, topic2, topic3}) |
| 1763 | + with self.assertRaises(pulsar.Timeout): |
| 1764 | + consumer_all.receive(100) |
| 1765 | + |
| 1766 | + # Assert consumer_persistent. |
| 1767 | + received_topics.clear() |
| 1768 | + for i in range(2 * num): |
| 1769 | + msg = consumer_persistent.receive(TM) |
| 1770 | + topic_name = msg.topic_name() |
| 1771 | + self.assertIn(topic_name, [topic1, topic2]) |
| 1772 | + received_topics.add(topic_name) |
| 1773 | + consumer_persistent.acknowledge(msg) |
| 1774 | + self.assertEqual(received_topics, {topic1, topic2}) |
| 1775 | + with self.assertRaises(pulsar.Timeout): |
| 1776 | + consumer_persistent.receive(100) |
| 1777 | + |
| 1778 | + # Assert consumer_non_persistent. |
| 1779 | + received_topics.clear() |
| 1780 | + for i in range(num): |
| 1781 | + msg = consumer_non_persistent.receive(TM) |
| 1782 | + topic_name = msg.topic_name() |
| 1783 | + self.assertIn(topic_name, [topic3]) |
| 1784 | + received_topics.add(topic_name) |
| 1785 | + consumer_non_persistent.acknowledge(msg) |
| 1786 | + self.assertEqual(received_topics, {topic3}) |
| 1787 | + with self.assertRaises(pulsar.Timeout): |
| 1788 | + consumer_non_persistent.receive(100) |
| 1789 | + |
| 1790 | + client.close() |
1720 | 1791 |
|
1721 | 1792 | if __name__ == "__main__":
|
1722 | 1793 | main()
|
0 commit comments