|
| 1 | +import logging |
| 2 | +import asyncio |
| 3 | +import pytest |
| 4 | +import pubnub as pn |
| 5 | + |
| 6 | +from unittest.mock import patch |
| 7 | +from pubnub.callbacks import SubscribeCallback |
| 8 | +from pubnub.models.consumer.common import PNStatus |
| 9 | +from pubnub.pubnub_asyncio import PubNubAsyncio |
| 10 | +from tests.helper import pnconf_env_copy |
| 11 | + |
| 12 | +from pubnub.enums import PNReconnectionPolicy, PNStatusCategory |
| 13 | +from pubnub.managers import LinearDelay, ExponentialDelay |
| 14 | +# from tests.integrational.vcr_helper import pn_vcr |
| 15 | + |
| 16 | +pn.set_stream_logger('pubnub', logging.DEBUG) |
| 17 | + |
| 18 | + |
| 19 | +class TestCallback(SubscribeCallback): |
| 20 | + message_result = None |
| 21 | + status_result = None |
| 22 | + presence_result = None |
| 23 | + |
| 24 | + def status(self, pubnub, status): |
| 25 | + super().status(pubnub, status) |
| 26 | + self.status_result = status |
| 27 | + |
| 28 | + def message(self, pubnub, message): |
| 29 | + self.message_result = message |
| 30 | + |
| 31 | + def presence(self, pubnub, presence): |
| 32 | + self.presence_result = presence |
| 33 | + |
| 34 | + |
| 35 | +@pytest.mark.asyncio |
| 36 | +async def test_subscribe_failing_reconnect_policy_none(): |
| 37 | + config = pnconf_env_copy(enable_subscribe=True, |
| 38 | + uuid="test-subscribe-failing-reconnect-policy-none", |
| 39 | + reconnect_policy=PNReconnectionPolicy.NONE, |
| 40 | + origin='127.0.0.1') |
| 41 | + pubnub = PubNubAsyncio(config) |
| 42 | + |
| 43 | + listener = TestCallback() |
| 44 | + pubnub.add_listener(listener) |
| 45 | + pubnub.subscribe().channels("my_channel").execute() |
| 46 | + while True: |
| 47 | + if isinstance(listener.status_result, PNStatus) \ |
| 48 | + and listener.status_result.category == PNStatusCategory.PNConnectionErrorCategory: |
| 49 | + break |
| 50 | + await asyncio.sleep(1) |
| 51 | + |
| 52 | + |
| 53 | +@pytest.mark.asyncio |
| 54 | +async def test_subscribe_failing_reconnect_policy_none(): |
| 55 | + config = pnconf_env_copy(enable_subscribe=True, |
| 56 | + uuid="test-subscribe-failing-reconnect-policy-none", |
| 57 | + reconnect_policy=PNReconnectionPolicy.NONE, |
| 58 | + origin='127.0.0.1') |
| 59 | + pubnub = PubNubAsyncio(config) |
| 60 | + |
| 61 | + listener = TestCallback() |
| 62 | + pubnub.add_listener(listener) |
| 63 | + pubnub.subscribe().channels("my_channel_none").execute() |
| 64 | + while True: |
| 65 | + if isinstance(listener.status_result, PNStatus) \ |
| 66 | + and listener.status_result.category == PNStatusCategory.PNConnectionErrorCategory: |
| 67 | + break |
| 68 | + await asyncio.sleep(0.5) |
| 69 | + await pubnub.stop() |
| 70 | + await asyncio.sleep(1) |
| 71 | + |
| 72 | + |
| 73 | +@pytest.mark.asyncio |
| 74 | +async def test_subscribe_failing_reconnect_policy_linear(): |
| 75 | + # we don't test the actual delay calculation here, just everything around it |
| 76 | + def mock_calculate(*args, **kwargs): |
| 77 | + return 0.2 |
| 78 | + |
| 79 | + with patch('pubnub.managers.LinearDelay.calculate', wraps=mock_calculate) as calculate_mock: |
| 80 | + config = pnconf_env_copy(enable_subscribe=True, |
| 81 | + uuid="test-subscribe-failing-reconnect-policy-linear", |
| 82 | + reconnect_policy=PNReconnectionPolicy.LINEAR, |
| 83 | + origin='127.0.0.1') |
| 84 | + pubnub = PubNubAsyncio(config) |
| 85 | + |
| 86 | + listener = TestCallback() |
| 87 | + pubnub.add_listener(listener) |
| 88 | + pubnub.subscribe().channels("my_channel_linear").execute() |
| 89 | + while True: |
| 90 | + if isinstance(listener.status_result, PNStatus) \ |
| 91 | + and listener.status_result.category == PNStatusCategory.PNConnectionErrorCategory: |
| 92 | + break |
| 93 | + await asyncio.sleep(0.5) |
| 94 | + assert calculate_mock.call_count == LinearDelay.MAX_RETRIES |
| 95 | + await pubnub.stop() |
| 96 | + await asyncio.sleep(1) |
| 97 | + |
| 98 | + |
| 99 | +@pytest.mark.asyncio |
| 100 | +async def test_subscribe_failing_reconnect_policy_exponential(): |
| 101 | + # we don't test the actual delay calculation here, just everything around it |
| 102 | + def mock_calculate(*args, **kwargs): |
| 103 | + return 0.2 |
| 104 | + |
| 105 | + with patch('pubnub.managers.ExponentialDelay.calculate', wraps=mock_calculate) as calculate_mock: |
| 106 | + config = pnconf_env_copy(enable_subscribe=True, |
| 107 | + uuid="test-subscribe-failing-reconnect-policy-exponential", |
| 108 | + reconnect_policy=PNReconnectionPolicy.EXPONENTIAL, |
| 109 | + origin='127.0.0.1') |
| 110 | + pubnub = PubNubAsyncio(config) |
| 111 | + |
| 112 | + listener = TestCallback() |
| 113 | + pubnub.add_listener(listener) |
| 114 | + pubnub.subscribe().channels("my_channel_exponential").execute() |
| 115 | + while True: |
| 116 | + if isinstance(listener.status_result, PNStatus) \ |
| 117 | + and listener.status_result.category == PNStatusCategory.PNConnectionErrorCategory: |
| 118 | + break |
| 119 | + await asyncio.sleep(0.5) |
| 120 | + assert calculate_mock.call_count == ExponentialDelay.MAX_RETRIES |
| 121 | + await pubnub.stop() |
| 122 | + await asyncio.sleep(1) |
| 123 | + |
| 124 | + |
| 125 | +@pytest.mark.asyncio |
| 126 | +async def test_subscribe_failing_reconnect_policy_linear_with_max_retries(): |
| 127 | + # we don't test the actual delay calculation here, just everything around it |
| 128 | + def mock_calculate(*args, **kwargs): |
| 129 | + return 0.2 |
| 130 | + |
| 131 | + with patch('pubnub.managers.LinearDelay.calculate', wraps=mock_calculate) as calculate_mock: |
| 132 | + config = pnconf_env_copy(enable_subscribe=True, maximum_reconnection_retries=3, |
| 133 | + uuid="test-subscribe-failing-reconnect-policy-linear-with-max-retries", |
| 134 | + reconnect_policy=PNReconnectionPolicy.LINEAR, |
| 135 | + origin='127.0.0.1') |
| 136 | + pubnub = PubNubAsyncio(config) |
| 137 | + |
| 138 | + listener = TestCallback() |
| 139 | + pubnub.add_listener(listener) |
| 140 | + pubnub.subscribe().channels("my_channel_linear").execute() |
| 141 | + while True: |
| 142 | + if isinstance(listener.status_result, PNStatus) \ |
| 143 | + and listener.status_result.category == PNStatusCategory.PNConnectionErrorCategory: |
| 144 | + break |
| 145 | + await asyncio.sleep(0.5) |
| 146 | + assert calculate_mock.call_count == 3 |
| 147 | + await pubnub.stop() |
| 148 | + await asyncio.sleep(1) |
| 149 | + |
| 150 | + |
| 151 | +@pytest.mark.asyncio |
| 152 | +async def test_subscribe_failing_reconnect_policy_exponential_with_max_retries(): |
| 153 | + # we don't test the actual delay calculation here, just everything around it |
| 154 | + def mock_calculate(*args, **kwargs): |
| 155 | + return 0.2 |
| 156 | + |
| 157 | + with patch('pubnub.managers.ExponentialDelay.calculate', wraps=mock_calculate) as calculate_mock: |
| 158 | + config = pnconf_env_copy(enable_subscribe=True, maximum_reconnection_retries=3, |
| 159 | + uuid="test-subscribe-failing-reconnect-policy-exponential-with-max-retries", |
| 160 | + reconnect_policy=PNReconnectionPolicy.EXPONENTIAL, |
| 161 | + origin='127.0.0.1') |
| 162 | + pubnub = PubNubAsyncio(config) |
| 163 | + |
| 164 | + listener = TestCallback() |
| 165 | + pubnub.add_listener(listener) |
| 166 | + pubnub.subscribe().channels("my_channel_exponential").execute() |
| 167 | + while True: |
| 168 | + if isinstance(listener.status_result, PNStatus) \ |
| 169 | + and listener.status_result.category == PNStatusCategory.PNConnectionErrorCategory: |
| 170 | + break |
| 171 | + await asyncio.sleep(0.5) |
| 172 | + assert calculate_mock.call_count == 3 |
| 173 | + await pubnub.stop() |
| 174 | + await asyncio.sleep(1) |
| 175 | + |
| 176 | + |
| 177 | +@pytest.mark.asyncio |
| 178 | +async def test_subscribe_failing_reconnect_policy_linear_with_custom_interval(): |
| 179 | + # we don't test the actual delay calculation here, just everything around it |
| 180 | + def mock_calculate(*args, **kwargs): |
| 181 | + return 0.2 |
| 182 | + |
| 183 | + with patch('pubnub.managers.LinearDelay.calculate', wraps=mock_calculate) as calculate_mock: |
| 184 | + config = pnconf_env_copy(enable_subscribe=True, maximum_reconnection_retries=3, reconnection_interval=1, |
| 185 | + uuid="test-subscribe-failing-reconnect-policy-linear-with-max-retries", |
| 186 | + reconnect_policy=PNReconnectionPolicy.LINEAR, |
| 187 | + origin='127.0.0.1') |
| 188 | + pubnub = PubNubAsyncio(config) |
| 189 | + |
| 190 | + listener = TestCallback() |
| 191 | + pubnub.add_listener(listener) |
| 192 | + pubnub.subscribe().channels("my_channel_linear").execute() |
| 193 | + while True: |
| 194 | + if isinstance(listener.status_result, PNStatus) \ |
| 195 | + and listener.status_result.category == PNStatusCategory.PNConnectionErrorCategory: |
| 196 | + break |
| 197 | + await asyncio.sleep(0.5) |
| 198 | + assert calculate_mock.call_count == 0 |
| 199 | + await pubnub.stop() |
| 200 | + await asyncio.sleep(1) |
0 commit comments