|
| 1 | +from time import sleep |
| 2 | + |
1 | 3 | from ld_eventsource import * |
2 | 4 | from ld_eventsource.actions import * |
3 | 5 | from ld_eventsource.config import * |
@@ -101,6 +103,57 @@ def test_all_iterator_continues_after_retry(): |
101 | 103 | assert client.next_retry_delay == initial_delay * 2 |
102 | 104 |
|
103 | 105 |
|
| 106 | +def test_retry_delay_gets_reset_after_threshold(): |
| 107 | + initial_delay = 0.005 |
| 108 | + retry_delay_reset_threshold = 0.1 |
| 109 | + mock = MockConnectStrategy( |
| 110 | + RespondWithData("data: data1\n\n"), |
| 111 | + RejectConnection(HTTPStatusError(503)), |
| 112 | + ) |
| 113 | + with SSEClient( |
| 114 | + connect=mock, |
| 115 | + error_strategy=ErrorStrategy.always_continue(), |
| 116 | + initial_retry_delay=initial_delay, |
| 117 | + retry_delay_reset_threshold=retry_delay_reset_threshold, |
| 118 | + retry_delay_strategy=RetryDelayStrategy.default(jitter_multiplier=None), |
| 119 | + ) as client: |
| 120 | + assert client._retry_reset_baseline == 0 |
| 121 | + all = client.all |
| 122 | + |
| 123 | + # Establish a successful connection |
| 124 | + item1 = next(all) |
| 125 | + assert isinstance(item1, Start) |
| 126 | + assert client._retry_reset_baseline != 0 |
| 127 | + |
| 128 | + item2 = next(all) |
| 129 | + assert isinstance(item2, Event) |
| 130 | + assert item2.data == 'data1' |
| 131 | + |
| 132 | + # Stream is dropped and then fails to re-connect, resulting in backoff. |
| 133 | + item3 = next(all) |
| 134 | + assert isinstance(item3, Fault) |
| 135 | + assert client.next_retry_delay == initial_delay |
| 136 | + |
| 137 | + item4 = next(all) |
| 138 | + assert isinstance(item4, Fault) |
| 139 | + assert client.next_retry_delay == initial_delay * 2 |
| 140 | + |
| 141 | + # Sleeping the threshold should reset the retry thresholds |
| 142 | + sleep(retry_delay_reset_threshold) |
| 143 | + |
| 144 | + # Which we see it does here |
| 145 | + item5 = next(all) |
| 146 | + assert isinstance(item5, Fault) |
| 147 | + assert client.next_retry_delay == initial_delay |
| 148 | + |
| 149 | + # And if we don't sleep long enough, it doesn't get reset. |
| 150 | + sleep(retry_delay_reset_threshold / 2) |
| 151 | + |
| 152 | + item6 = next(all) |
| 153 | + assert isinstance(item6, Fault) |
| 154 | + assert client.next_retry_delay == initial_delay * 2 |
| 155 | + |
| 156 | + |
104 | 157 | def test_can_interrupt_and_restart_stream(): |
105 | 158 | initial_delay = 0.005 |
106 | 159 | mock = MockConnectStrategy( |
|
0 commit comments