|
| 1 | +import pytest |
| 2 | + |
| 3 | +from mongoose.core.processing import ProcessingQueue, ProcessingTopic |
| 4 | +from mongoose.utils.exceptions import TopicNotFoundException |
| 5 | + |
| 6 | + |
| 7 | +def test_unsubscribe(): |
| 8 | + pq = ProcessingQueue() |
| 9 | + subscriber_id = "test_subscriber" |
| 10 | + topic = ProcessingTopic.NETWORK_DPI |
| 11 | + |
| 12 | + # Reset singleton-like behavior if any (though it looks like a normal class with class attributes) |
| 13 | + # Actually, subscribers and queues are class attributes, which is a bit strange if multiple instances are intended. |
| 14 | + # Let's check if they are indeed class attributes. |
| 15 | + ProcessingQueue.subscribers.clear() |
| 16 | + ProcessingQueue.queues.clear() |
| 17 | + |
| 18 | + # Subscribe |
| 19 | + pq.subscribe(topic, subscriber_id) |
| 20 | + assert subscriber_id in pq.subscribers |
| 21 | + assert topic in pq.queues |
| 22 | + assert len(pq.queues[topic]) == 1 |
| 23 | + |
| 24 | + # Unsubscribe (method not yet implemented) |
| 25 | + if hasattr(pq, 'unsubscribe'): |
| 26 | + pq.unsubscribe(subscriber_id) |
| 27 | + assert subscriber_id not in pq.subscribers |
| 28 | + assert topic not in pq.queues or len(pq.queues[topic]) == 0 |
| 29 | + |
| 30 | + # Verify that publishing to the topic now raises TopicNotFoundException if no other subscribers |
| 31 | + with pytest.raises(TopicNotFoundException): |
| 32 | + pq.publish(topic, "test data") |
| 33 | + else: |
| 34 | + pytest.fail("ProcessingQueue has no unsubscribe method") |
| 35 | + |
| 36 | + |
| 37 | +def test_unsubscribe_not_found(): |
| 38 | + pq = ProcessingQueue() |
| 39 | + ProcessingQueue.subscribers.clear() |
| 40 | + ProcessingQueue.queues.clear() |
| 41 | + |
| 42 | + # Should not raise any exception |
| 43 | + pq.unsubscribe("non_existent_subscriber") |
| 44 | + |
| 45 | + |
| 46 | +def test_unsubscribe_partial_others_remain(): |
| 47 | + pq = ProcessingQueue() |
| 48 | + sub1 = "sub1" |
| 49 | + sub2 = "sub2" |
| 50 | + topic = ProcessingTopic.NETWORK_DPI |
| 51 | + |
| 52 | + ProcessingQueue.subscribers.clear() |
| 53 | + ProcessingQueue.queues.clear() |
| 54 | + |
| 55 | + pq.subscribe(topic, sub1) |
| 56 | + pq.subscribe(topic, sub2) |
| 57 | + |
| 58 | + assert len(pq.queues[topic]) == 2 |
| 59 | + |
| 60 | + pq.unsubscribe(sub1) |
| 61 | + |
| 62 | + assert sub1 not in pq.subscribers |
| 63 | + assert sub2 in pq.subscribers |
| 64 | + assert topic in pq.queues |
| 65 | + assert len(pq.queues[topic]) == 1 |
| 66 | + |
| 67 | + # Can still publish to sub2 |
| 68 | + pq.publish(topic, "data") |
| 69 | + q2 = pq.subscribers[sub2][topic] |
| 70 | + assert q2.get_nowait() == "data" |
0 commit comments