2016-09-16 19:24:14 +08:00
|
|
|
import requests
|
|
|
|
from unittest import TestCase
|
|
|
|
from nose.tools import assert_raises
|
|
|
|
from datetime import datetime, date
|
2018-01-04 22:00:52 +08:00
|
|
|
from cartodb_services.tools.qps import qps_retry
|
|
|
|
from cartodb_services.tools.exceptions import (ServiceException,
|
|
|
|
TimeoutException)
|
2016-09-16 19:24:14 +08:00
|
|
|
import requests_mock
|
|
|
|
import mock
|
|
|
|
|
|
|
|
requests_mock.Mocker.TEST_PREFIX = 'test_'
|
|
|
|
|
2018-01-04 22:00:52 +08:00
|
|
|
|
2016-09-16 19:24:14 +08:00
|
|
|
@requests_mock.Mocker()
|
|
|
|
class TestQPS(TestCase):
|
|
|
|
QPS_ERROR_MESSAGE = "Queries per second exceeded: Queries exceeded (10 allowed)"
|
|
|
|
|
|
|
|
def test_qps_timeout(self, req_mock):
|
|
|
|
class TestClass:
|
2016-09-28 19:43:50 +08:00
|
|
|
@qps_retry(timeout=0.001, qps=100)
|
2016-09-16 19:24:14 +08:00
|
|
|
def test(self):
|
|
|
|
response = requests.get('http://localhost/test_qps')
|
|
|
|
if response.status_code == 429:
|
|
|
|
raise ServiceException('Error 429', response)
|
|
|
|
|
|
|
|
def _text_cb(request, context):
|
|
|
|
context.status_code = 429
|
|
|
|
return self.QPS_ERROR_MESSAGE
|
|
|
|
|
|
|
|
req_mock.register_uri('GET', 'http://localhost/test_qps',
|
2018-01-04 22:00:52 +08:00
|
|
|
text=_text_cb)
|
2016-09-16 19:24:14 +08:00
|
|
|
with self.assertRaises(TimeoutException):
|
|
|
|
c = TestClass()
|
|
|
|
c.test()
|