forked from getsentry/raven-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support server sending back Retry-After header for rate limiting
- Loading branch information
1 parent
37aaf08
commit 31b87f5
Showing
2 changed files
with
54 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,12 +6,14 @@ | |
import pytest | ||
import raven | ||
import time | ||
from StringIO import StringIO | ||
from socket import socket, AF_INET, SOCK_DGRAM | ||
from raven.base import Client, ClientState | ||
from raven.transport import AsyncTransport | ||
from raven.utils.stacks import iter_stack_frames | ||
from raven.utils import six | ||
from raven.utils.testutils import TestCase | ||
from raven.utils.compat import HTTPError | ||
|
||
|
||
class TempStoreClient(Client): | ||
|
@@ -62,6 +64,22 @@ def test_set_success(self): | |
self.assertEquals(state.last_check, None) | ||
self.assertEquals(state.retry_number, 0) | ||
|
||
def test_should_try_retry_after(self): | ||
state = ClientState() | ||
state.status = state.ERROR | ||
state.last_check = time.time() | ||
state.retry_number = 1 | ||
state.retry_after = 1 | ||
self.assertFalse(state.should_try()) | ||
|
||
def test_should_try_retry_after_passed(self): | ||
state = ClientState() | ||
state.status = state.ERROR | ||
state.last_check = time.time() - 1 | ||
state.retry_number = 1 | ||
state.retry_after = 1 | ||
self.assertTrue(state.should_try()) | ||
|
||
|
||
class ClientTest(TestCase): | ||
def setUp(self): | ||
|
@@ -96,6 +114,31 @@ def test_send_remote_failover(self, should_try, send): | |
client.send_remote('sync+http://example.com/api/store', 'foo') | ||
self.assertEquals(client.state.status, client.state.ONLINE) | ||
|
||
@mock.patch('raven.transport.http.HTTPTransport.send') | ||
@mock.patch('raven.base.ClientState.should_try') | ||
def test_send_remote_failover_with_retry_after(self, should_try, send): | ||
should_try.return_value = True | ||
|
||
client = Client( | ||
dsn='sync+http://public:[email protected]/1' | ||
) | ||
|
||
e = HTTPError( | ||
'http://example.com/api/store', 429, 'oops', | ||
{'Retry-After': '5'}, StringIO()) | ||
|
||
# test error | ||
send.side_effect = e | ||
client.send_remote('sync+http://example.com/api/store', 'foo') | ||
self.assertEquals(client.state.status, client.state.ERROR) | ||
self.assertEqual(client.state.retry_after, 5) | ||
|
||
# test recovery | ||
send.side_effect = None | ||
client.send_remote('sync+http://example.com/api/store', 'foo') | ||
self.assertEquals(client.state.status, client.state.ONLINE) | ||
self.assertEqual(client.state.retry_after, 0) | ||
|
||
@mock.patch('raven.base.Client._registry.get_transport') | ||
@mock.patch('raven.base.ClientState.should_try') | ||
def test_async_send_remote_failover(self, should_try, get_transport): | ||
|