|
1 | 1 | import responses |
2 | 2 | import unittest |
| 3 | +import time |
3 | 4 |
|
4 | 5 | from tests.support import with_resource, with_fixture, characters |
5 | 6 |
|
|
10 | 11 | from twitter_ads.http import Request |
11 | 12 | from twitter_ads.resource import Resource |
12 | 13 | from twitter_ads import API_VERSION |
| 14 | +from twitter_ads.error import RateLimit |
| 15 | + |
| 16 | + |
| 17 | +@responses.activate |
| 18 | +def test_rate_limit_handle_with_retry_success_1(monkeypatch): |
| 19 | + # scenario: |
| 20 | + # - 500 (retry) -> 429 (handle rate limit) -> 200 (end) |
| 21 | + monkeypatch.setattr(time, 'sleep', lambda s: None) |
| 22 | + |
| 23 | + responses.add(responses.GET, |
| 24 | + with_resource('/' + API_VERSION + '/accounts/2iqph'), |
| 25 | + body=with_fixture('accounts_load'), |
| 26 | + content_type='application/json') |
| 27 | + |
| 28 | + responses.add(responses.GET, |
| 29 | + with_resource('/' + API_VERSION + '/accounts/2iqph/campaigns'), |
| 30 | + status=500, |
| 31 | + body=with_fixture('campaigns_all'), |
| 32 | + content_type='application/json', |
| 33 | + headers={ |
| 34 | + 'x-account-rate-limit-limit': '10000', |
| 35 | + 'x-account-rate-limit-remaining': '0', |
| 36 | + 'x-account-rate-limit-reset': '1546300800' |
| 37 | + }) |
| 38 | + |
| 39 | + responses.add(responses.GET, |
| 40 | + with_resource('/' + API_VERSION + '/accounts/2iqph/campaigns'), |
| 41 | + status=429, |
| 42 | + body=with_fixture('campaigns_all'), |
| 43 | + content_type='application/json', |
| 44 | + headers={ |
| 45 | + 'x-account-rate-limit-limit': '10000', |
| 46 | + 'x-account-rate-limit-remaining': '0', |
| 47 | + 'x-account-rate-limit-reset': str(int(time.time()) + 5) |
| 48 | + }) |
| 49 | + |
| 50 | + responses.add(responses.GET, |
| 51 | + with_resource('/' + API_VERSION + '/accounts/2iqph/campaigns'), |
| 52 | + status=200, |
| 53 | + body=with_fixture('campaigns_all'), |
| 54 | + content_type='application/json', |
| 55 | + headers={ |
| 56 | + 'x-account-rate-limit-limit': '10000', |
| 57 | + 'x-account-rate-limit-remaining': '9999', |
| 58 | + 'x-account-rate-limit-reset': '1546300800' |
| 59 | + }) |
| 60 | + |
| 61 | + client = Client( |
| 62 | + characters(40), |
| 63 | + characters(40), |
| 64 | + characters(40), |
| 65 | + characters(40), |
| 66 | + options={ |
| 67 | + 'handle_rate_limit': True, |
| 68 | + 'retry_max': 1, |
| 69 | + 'retry_delay': 3000, |
| 70 | + 'retry_on_status': [500] |
| 71 | + } |
| 72 | + ) |
| 73 | + |
| 74 | + account = Account.load(client, '2iqph') |
| 75 | + |
| 76 | + cursor = Campaign.all(account) |
| 77 | + assert len(responses.calls) == 4 |
| 78 | + assert cursor is not None |
| 79 | + assert isinstance(cursor, Cursor) |
| 80 | + assert cursor.rate_limit is None |
| 81 | + assert cursor.account_rate_limit == '10000' |
| 82 | + assert cursor.account_rate_limit_remaining == '9999' |
| 83 | + assert cursor.account_rate_limit_reset == '1546300800' |
| 84 | + |
| 85 | + |
| 86 | +@responses.activate |
| 87 | +def test_rate_limit_handle_with_retry_success_2(monkeypatch): |
| 88 | + # scenario: |
| 89 | + # - 429 (handle rate limit) -> 500 (retry) -> 200 (end) |
| 90 | + monkeypatch.setattr(time, 'sleep', lambda s: None) |
| 91 | + |
| 92 | + responses.add(responses.GET, |
| 93 | + with_resource('/' + API_VERSION + '/accounts/2iqph'), |
| 94 | + body=with_fixture('accounts_load'), |
| 95 | + content_type='application/json') |
| 96 | + |
| 97 | + responses.add(responses.GET, |
| 98 | + with_resource('/' + API_VERSION + '/accounts/2iqph/campaigns'), |
| 99 | + status=429, |
| 100 | + body=with_fixture('campaigns_all'), |
| 101 | + content_type='application/json', |
| 102 | + headers={ |
| 103 | + 'x-account-rate-limit-limit': '10000', |
| 104 | + 'x-account-rate-limit-remaining': '0', |
| 105 | + 'x-account-rate-limit-reset': '1546300800' |
| 106 | + }) |
| 107 | + |
| 108 | + responses.add(responses.GET, |
| 109 | + with_resource('/' + API_VERSION + '/accounts/2iqph/campaigns'), |
| 110 | + status=500, |
| 111 | + body=with_fixture('campaigns_all'), |
| 112 | + content_type='application/json', |
| 113 | + headers={ |
| 114 | + 'x-account-rate-limit-limit': '10000', |
| 115 | + 'x-account-rate-limit-remaining': '0', |
| 116 | + 'x-account-rate-limit-reset': str(int(time.time()) + 5) |
| 117 | + }) |
| 118 | + |
| 119 | + responses.add(responses.GET, |
| 120 | + with_resource('/' + API_VERSION + '/accounts/2iqph/campaigns'), |
| 121 | + status=200, |
| 122 | + body=with_fixture('campaigns_all'), |
| 123 | + content_type='application/json', |
| 124 | + headers={ |
| 125 | + 'x-account-rate-limit-limit': '10000', |
| 126 | + 'x-account-rate-limit-remaining': '9999', |
| 127 | + 'x-account-rate-limit-reset': '1546300800' |
| 128 | + }) |
| 129 | + |
| 130 | + client = Client( |
| 131 | + characters(40), |
| 132 | + characters(40), |
| 133 | + characters(40), |
| 134 | + characters(40), |
| 135 | + options={ |
| 136 | + 'handle_rate_limit': True, |
| 137 | + 'retry_max': 1, |
| 138 | + 'retry_delay': 3000, |
| 139 | + 'retry_on_status': [500] |
| 140 | + } |
| 141 | + ) |
| 142 | + |
| 143 | + account = Account.load(client, '2iqph') |
| 144 | + |
| 145 | + cursor = Campaign.all(account) |
| 146 | + assert len(responses.calls) == 4 |
| 147 | + assert cursor is not None |
| 148 | + assert isinstance(cursor, Cursor) |
| 149 | + assert cursor.rate_limit is None |
| 150 | + assert cursor.account_rate_limit == '10000' |
| 151 | + assert cursor.account_rate_limit_remaining == '9999' |
| 152 | + assert cursor.account_rate_limit_reset == '1546300800' |
| 153 | + |
| 154 | + |
| 155 | +@responses.activate |
| 156 | +def test_rate_limit_handle_success(monkeypatch): |
| 157 | + monkeypatch.setattr(time, 'sleep', lambda s: None) |
| 158 | + |
| 159 | + responses.add(responses.GET, |
| 160 | + with_resource('/' + API_VERSION + '/accounts/2iqph'), |
| 161 | + body=with_fixture('accounts_load'), |
| 162 | + content_type='application/json') |
| 163 | + |
| 164 | + responses.add(responses.GET, |
| 165 | + with_resource('/' + API_VERSION + '/accounts/2iqph/campaigns'), |
| 166 | + status=429, |
| 167 | + body=with_fixture('campaigns_all'), |
| 168 | + content_type='application/json', |
| 169 | + headers={ |
| 170 | + 'x-account-rate-limit-limit': '10000', |
| 171 | + 'x-account-rate-limit-remaining': '0', |
| 172 | + 'x-account-rate-limit-reset': str(int(time.time()) + 5) |
| 173 | + }) |
| 174 | + |
| 175 | + responses.add(responses.GET, |
| 176 | + with_resource('/' + API_VERSION + '/accounts/2iqph/campaigns'), |
| 177 | + status=200, |
| 178 | + body=with_fixture('campaigns_all'), |
| 179 | + content_type='application/json', |
| 180 | + headers={ |
| 181 | + 'x-account-rate-limit-limit': '10000', |
| 182 | + 'x-account-rate-limit-remaining': '9999', |
| 183 | + 'x-account-rate-limit-reset': '1546300800' |
| 184 | + }) |
| 185 | + |
| 186 | + client = Client( |
| 187 | + characters(40), |
| 188 | + characters(40), |
| 189 | + characters(40), |
| 190 | + characters(40), |
| 191 | + options={ |
| 192 | + 'handle_rate_limit': True |
| 193 | + } |
| 194 | + ) |
| 195 | + |
| 196 | + account = Account.load(client, '2iqph') |
| 197 | + |
| 198 | + cursor = Campaign.all(account) |
| 199 | + assert len(responses.calls) == 3 |
| 200 | + assert cursor is not None |
| 201 | + assert isinstance(cursor, Cursor) |
| 202 | + assert cursor.rate_limit is None |
| 203 | + assert cursor.account_rate_limit == '10000' |
| 204 | + assert cursor.account_rate_limit_remaining == '9999' |
| 205 | + assert cursor.account_rate_limit_reset == '1546300800' |
| 206 | + |
| 207 | + |
| 208 | +@responses.activate |
| 209 | +def test_rate_limit_handle_error(monkeypatch): |
| 210 | + monkeypatch.setattr(time, 'sleep', lambda s: None) |
| 211 | + |
| 212 | + responses.add(responses.GET, |
| 213 | + with_resource('/' + API_VERSION + '/accounts/2iqph'), |
| 214 | + body=with_fixture('accounts_load'), |
| 215 | + content_type='application/json') |
| 216 | + |
| 217 | + responses.add(responses.GET, |
| 218 | + with_resource('/' + API_VERSION + '/accounts/2iqph/campaigns'), |
| 219 | + status=429, |
| 220 | + body=with_fixture('campaigns_all'), |
| 221 | + content_type='application/json', |
| 222 | + headers={ |
| 223 | + 'x-account-rate-limit-limit': '10000', |
| 224 | + 'x-account-rate-limit-remaining': '0', |
| 225 | + 'x-account-rate-limit-reset': str(int(time.time()) + 5) |
| 226 | + }) |
| 227 | + |
| 228 | + responses.add(responses.GET, |
| 229 | + with_resource('/' + API_VERSION + '/accounts/2iqph/campaigns'), |
| 230 | + status=429, |
| 231 | + body=with_fixture('campaigns_all'), |
| 232 | + content_type='application/json', |
| 233 | + headers={ |
| 234 | + 'x-account-rate-limit-limit': '10000', |
| 235 | + 'x-account-rate-limit-remaining': '0', |
| 236 | + 'x-account-rate-limit-reset': '1546300800' |
| 237 | + }) |
| 238 | + |
| 239 | + client = Client( |
| 240 | + characters(40), |
| 241 | + characters(40), |
| 242 | + characters(40), |
| 243 | + characters(40), |
| 244 | + options={ |
| 245 | + 'handle_rate_limit': True |
| 246 | + } |
| 247 | + ) |
| 248 | + |
| 249 | + account = Account.load(client, '2iqph') |
| 250 | + |
| 251 | + try: |
| 252 | + cursor = Campaign.all(account) |
| 253 | + except Exception as e: |
| 254 | + error = e |
| 255 | + print(error) |
| 256 | + assert len(responses.calls) == 3 |
| 257 | + assert isinstance(error, RateLimit) |
| 258 | + assert error.reset_at == '1546300800' |
13 | 259 |
|
14 | 260 |
|
15 | 261 | @responses.activate |
|
0 commit comments