|
1 | 1 | from django.db import connection
|
2 | 2 | from pulpcore.tasking import pubsub
|
3 | 3 | from types import SimpleNamespace
|
| 4 | +from datetime import datetime |
4 | 5 | import select
|
5 | 6 | import pytest
|
6 | 7 |
|
7 | 8 |
|
8 | 9 | def test_postgres_pubsub():
|
| 10 | + """Testing postgres low-level implementation.""" |
9 | 11 | state = SimpleNamespace()
|
10 |
| - state.got_first_message = False |
11 |
| - state.got_second_message = False |
| 12 | + state.got_message = False |
12 | 13 | with connection.cursor() as cursor:
|
13 | 14 | assert connection.connection is cursor.connection
|
14 | 15 | conn = cursor.connection
|
| 16 | + # Listen and Notify |
15 | 17 | conn.execute("LISTEN abc")
|
16 | 18 | conn.add_notify_handler(lambda notification: setattr(state, "got_message", True))
|
17 | 19 | cursor.execute("NOTIFY abc, 'foo'")
|
| 20 | + assert state.got_message is True |
18 | 21 | conn.execute("SELECT 1")
|
| 22 | + assert state.got_message is True |
| 23 | + |
| 24 | + # Reset and retry |
| 25 | + state.got_message = False |
19 | 26 | conn.execute("UNLISTEN abc")
|
20 |
| - assert state.got_message is True |
| 27 | + cursor.execute("NOTIFY abc, 'foo'") |
| 28 | + assert state.got_message is False |
21 | 29 |
|
22 | 30 |
|
23 | 31 | M = pubsub.PubsubMessage
|
24 | 32 |
|
| 33 | +PUBSUB_BACKENDS = [ |
| 34 | + pytest.param(pubsub.PostgresPubSub, id="and-using-postgres-backend"), |
| 35 | +] |
| 36 | + |
| 37 | + |
| 38 | +@pytest.mark.parametrize("pubsub_backend", PUBSUB_BACKENDS) |
| 39 | +class TestPublish: |
| 40 | + |
| 41 | + @pytest.mark.parametrize( |
| 42 | + "payload", |
| 43 | + ( |
| 44 | + pytest.param(None, id="none"), |
| 45 | + pytest.param("", id="empty-string"), |
| 46 | + pytest.param("payload", id="non-empty-string"), |
| 47 | + pytest.param(123, id="int"), |
| 48 | + pytest.param(datetime.now(), id="datetime"), |
| 49 | + pytest.param(True, id="bool"), |
| 50 | + ), |
| 51 | + ) |
| 52 | + def test_with_payload_as(self, pubsub_backend, payload): |
| 53 | + pubsub_backend.publish("channel", payload=payload) |
| 54 | + |
25 | 55 |
|
| 56 | +@pytest.mark.parametrize("pubsub_backend", PUBSUB_BACKENDS) |
26 | 57 | @pytest.mark.parametrize(
|
27 | 58 | "messages",
|
28 | 59 | (
|
29 |
| - [M("channel_a", "A1")], |
30 |
| - [M("channel_a", "A1"), M("channel_a", "A2")], |
31 |
| - [M("channel_a", "A1"), M("channel_a", "A2"), M("channel_b", "B1"), M("channel_c", "C1")], |
| 60 | + pytest.param([M("a", "A1")], id="single-message"), |
| 61 | + pytest.param([M("a", "A1"), M("a", "A2")], id="two-messages-in-same-channel"), |
| 62 | + pytest.param( |
| 63 | + [M("a", "A1"), M("a", "A2"), M("b", "B1"), M("c", "C1")], |
| 64 | + id="tree-msgs-in-different-channels", |
| 65 | + ), |
32 | 66 | ),
|
33 | 67 | )
|
34 |
| -@pytest.mark.parametrize("same_client", (True, False), ids=("same-clients", "different-clients")) |
35 |
| -class TestPubSub: |
36 |
| - |
37 |
| - def test_subscribe_publish_fetch(self, same_client, messages): |
38 |
| - """ |
39 |
| - GIVEN a publisher and a subscriber (which may be the same) |
40 |
| - AND a queue of messages Q with mixed channels and payloads |
41 |
| - WHEN the subscriber subscribes to all the channels in Q |
42 |
| - AND the publisher publishes all the messages in Q |
43 |
| - THEN the subscriber fetch() call returns a queue equivalent to Q |
44 |
| - AND calling fetch() a second time returns an empty queue |
45 |
| - """ |
46 |
| - # Given |
47 |
| - publisher = pubsub.PostgresPubSub(connection) |
48 |
| - subscriber = publisher if same_client else pubsub.PostgresPubSub(connection) |
49 |
| - |
50 |
| - # When |
51 |
| - for message in messages: |
52 |
| - subscriber.subscribe(message.channel) |
53 |
| - for message in messages: |
54 |
| - publisher.publish(message.channel, message=message.payload) |
55 |
| - |
56 |
| - # Then |
57 |
| - assert subscriber.fetch() == messages |
58 |
| - assert subscriber.fetch() == [] |
59 |
| - |
60 |
| - def test_unsubscribe(self, same_client, messages): |
61 |
| - """ |
62 |
| - GIVEN a publisher and a subscriber (which may be the same) |
63 |
| - AND a queue of messages Q with mixed channels and payloads |
64 |
| - WHEN the subscriber subscribes and unsubscribes to all the channels in Q |
65 |
| - AND the publisher publishes all the messages in Q |
66 |
| - THEN the subscriber fetch() call returns an empty queue |
67 |
| - """ |
68 |
| - # Given |
69 |
| - publisher = pubsub.PostgresPubSub(connection) |
70 |
| - subscriber = publisher if same_client else pubsub.PostgresPubSub(connection) |
71 |
| - |
72 |
| - # When |
73 |
| - for message in messages: |
74 |
| - subscriber.subscribe(message.channel) |
75 |
| - for message in messages: |
76 |
| - subscriber.unsubscribe(message.channel) |
77 |
| - for message in messages: |
78 |
| - publisher.publish(message.channel, message=message.payload) |
79 |
| - |
80 |
| - # Then |
81 |
| - assert subscriber.fetch() == [] |
82 |
| - |
83 |
| - def test_select_loop(self, same_client, messages): |
84 |
| - """ |
85 |
| - GIVEN a publisher and a subscriber (which may be the same) |
86 |
| - AND a queue of messages Q with mixed channels and payloads |
87 |
| - AND the subscriber is subscribed to all the channels in Q |
88 |
| - WHEN the publisher has NOT published anything yet |
89 |
| - THEN the select loop won't detect the subscriber readiness |
90 |
| - AND the subscriber fetch() call returns an empty queue |
91 |
| - BUT WHEN the publisher does publish all messages in Q |
92 |
| - THEN the select loop detects the subscriber readiness |
93 |
| - AND the subscriber fetch() call returns a queue equivalent to Q |
94 |
| - """ |
| 68 | +class TestSubscribeFetch: |
| 69 | + def unsubscribe_all(self, channels, subscriber): |
| 70 | + for channel in channels: |
| 71 | + subscriber.unsubscribe(channel) |
| 72 | + |
| 73 | + def subscribe_all(self, channels, subscriber): |
| 74 | + for channel in channels: |
| 75 | + subscriber.subscribe(channel) |
| 76 | + |
| 77 | + def publish_all(self, messages, publisher): |
| 78 | + for channel, payload in messages: |
| 79 | + publisher.publish(channel, payload=payload) |
| 80 | + |
| 81 | + def test_with( |
| 82 | + self, pubsub_backend: pubsub.BasePubSubBackend, messages: list[pubsub.PubsubMessage] |
| 83 | + ): |
| 84 | + channels = {m.channel for m in messages} |
| 85 | + publisher = pubsub_backend |
| 86 | + with pubsub_backend() as subscriber: |
| 87 | + self.subscribe_all(channels, subscriber) |
| 88 | + self.publish_all(messages, publisher) |
| 89 | + assert subscriber.fetch() == messages |
| 90 | + |
| 91 | + self.unsubscribe_all(channels, subscriber) |
| 92 | + assert subscriber.fetch() == [] |
| 93 | + |
| 94 | + def test_select_readiness_with( |
| 95 | + self, pubsub_backend: pubsub.BasePubSubBackend, messages: list[pubsub.PubsubMessage] |
| 96 | + ): |
95 | 97 | TIMEOUT = 0.1
|
96 |
| - |
97 |
| - # Given |
98 |
| - publisher = pubsub.PostgresPubSub(connection) |
99 |
| - subscriber = publisher if same_client else pubsub.PostgresPubSub(connection) |
100 |
| - |
101 |
| - # When |
102 |
| - for message in messages: |
103 |
| - subscriber.subscribe(message.channel) |
104 |
| - r, w, x = select.select([subscriber], [], [], TIMEOUT) |
105 |
| - |
106 |
| - # Then |
107 |
| - assert subscriber not in r |
108 |
| - assert subscriber.fetch() == [] |
109 |
| - |
110 |
| - # But When |
111 |
| - for message in messages: |
112 |
| - publisher.publish(message.channel, message=message.payload) |
113 |
| - r, w, x = select.select([subscriber], [], [], TIMEOUT) |
114 |
| - |
115 |
| - # Then |
116 |
| - assert subscriber in r |
117 |
| - assert subscriber.fetch() == messages |
118 |
| - assert subscriber.fetch() == [] |
| 98 | + channels = {m.channel for m in messages} |
| 99 | + publisher = pubsub_backend |
| 100 | + with pubsub_backend() as subscriber: |
| 101 | + self.subscribe_all(channels, subscriber) |
| 102 | + r, w, x = select.select([subscriber], [], [], TIMEOUT) |
| 103 | + assert subscriber not in r |
| 104 | + assert subscriber.fetch() == [] |
| 105 | + |
| 106 | + self.publish_all(messages, publisher) |
| 107 | + r, w, x = select.select([subscriber], [], [], TIMEOUT) |
| 108 | + assert subscriber in r |
| 109 | + assert subscriber.fetch() == messages |
| 110 | + assert subscriber.fetch() == [] |
| 111 | + |
| 112 | + self.unsubscribe_all(channels, subscriber) |
| 113 | + self.publish_all(messages, publisher) |
| 114 | + r, w, x = select.select([subscriber], [], [], TIMEOUT) |
| 115 | + assert subscriber not in r |
| 116 | + assert subscriber.fetch() == [] |
0 commit comments