def test_send_request(self): bt = BaseThrottler() throttled_request, _ = bt._prepare_request(self.default_request) bt._send_request(throttled_request) self.assertEqual(1, bt.successes) self.assertEqual(0, bt.failures) self.assertTrue(throttled_request.finished) self.assertIsNotNone(throttled_request._response) self.assertIsNone(throttled_request._exception) bt = BaseThrottler() request = requests.Request(method=None, url='http://None') throttled_request, _ = bt._prepare_request(request) bt._send_request(throttled_request) self.assertEqual(1, bt.failures) self.assertEqual(0, bt.successes) self.assertTrue(throttled_request.finished) self.assertIsNone(throttled_request._response) self.assertIsNotNone(throttled_request._exception) self.assertIsInstance(throttled_request._exception, Exception) with self.assertRaises(Exception): throttled_request.response
def test_send_request(self): bt = BaseThrottler() throttled_request, _ = bt._prepare_request(self.default_request) bt._send_request(throttled_request) self.assertEqual(1, bt.successes) self.assertEqual(0, bt.failures) self.assertTrue(throttled_request.finished) self.assertIsNotNone(throttled_request._response) self.assertIsNone(throttled_request._exception) bt = BaseThrottler() request = requests.Request(method=None, url='http://None') throttled_request, _ = bt._prepare_request(request) bt._send_request(throttled_request) self.assertEqual(1, bt.failures) self.assertEqual(0, bt.successes) self.assertTrue(throttled_request.finished) self.assertIsNone(throttled_request._response) self.assertIsNotNone(throttled_request._exception) self.assertIsInstance(throttled_request._exception, Exception) with self.assertRaises(Exception): throttled_request.response
def test_enqueue_request(self): bt = BaseThrottler(max_pool_size=1) throttled_request, _ = bt._prepare_request(self.default_request) bt._enqueue_request(throttled_request) self.assertEqual(1, len(bt._requests_pool)) throttled_request, _ = bt._prepare_request(self.default_request) with self.assertRaises(FullRequestsPoolError): bt._enqueue_request(throttled_request)
def test_enqueue_request(self): bt = BaseThrottler(max_pool_size=1) throttled_request, _ = bt._prepare_request(self.default_request) bt._enqueue_request(throttled_request) self.assertEqual(1, len(bt._requests_pool)) throttled_request, _ = bt._prepare_request(self.default_request) with self.assertRaises(FullRequestsPoolError): bt._enqueue_request(throttled_request)
def test_send_request(self): bt = BaseThrottler() throttled_request, _ = bt._prepare_request(self.default_request) bt._send_request(throttled_request) self.assertEqual(1, bt.successes) self.assertEqual(0, bt.failures) self.assertTrue(throttled_request.finished) self.assertIsNotNone(throttled_request._response) self.assertIsNone(throttled_request._exception)
def test_send_request(self): bt = BaseThrottler() throttled_request, _ = bt._prepare_request(self.default_request) bt._send_request(throttled_request) self.assertEqual(1, bt.successes) self.assertEqual(0, bt.failures) self.assertTrue(throttled_request.finished) self.assertIsNotNone(throttled_request._response) self.assertIsNone(throttled_request._exception)
def test_dequeue_request(self): bt = BaseThrottler() for wait_enqueued in [True, False]: bt._status = 'stopped' request = bt._dequeue_request() self.assertIsNone(request) bt._status = 'running' throttled_request, _ = bt._prepare_request(self.default_request) bt._enqueue_request(throttled_request) request = bt._dequeue_request() self.assertEqual(throttled_request, request)
def test_dequeue_request(self): bt = BaseThrottler() for wait_enqueued in [True, False]: bt._status = 'stopped' request = bt._dequeue_request() self.assertIsNone(request) bt._status = 'running' throttled_request, _ = bt._prepare_request(self.default_request) bt._enqueue_request(throttled_request) request = bt._dequeue_request() self.assertEqual(throttled_request, request)
def test_prepare_request(self): bt = BaseThrottler() throttled_request, prepared = bt._prepare_request(self.default_request) self.assertTrue(prepared) self.assertFalse(throttled_request.finished) self.assertIsNone(throttled_request._response) self.assertIsNone(throttled_request._exception) request = requests.Request(method=None, url=None) throttled_request, prepared = bt._prepare_request(request) self.assertFalse(prepared) self.assertEqual(1, bt.failures) self.assertTrue(throttled_request.finished) self.assertIsNone(throttled_request._response) self.assertIsNotNone(throttled_request._exception) with self.assertRaises(Exception): throttled_request.response self.assertIsInstance(throttled_request._exception, Exception)
def test_prepare_request(self): bt = BaseThrottler() throttled_request, prepared = bt._prepare_request(self.default_request) self.assertTrue(prepared) self.assertFalse(throttled_request.finished) self.assertIsNone(throttled_request._response) self.assertIsNone(throttled_request._exception) request = requests.Request(method=None, url=None) throttled_request, prepared = bt._prepare_request(request) self.assertFalse(prepared) self.assertEqual(1, bt.failures) self.assertTrue(throttled_request.finished) self.assertIsNone(throttled_request._response) self.assertIsNotNone(throttled_request._exception) with self.assertRaises(Exception): throttled_request.response self.assertIsInstance(throttled_request._exception, Exception)
def test_dequeue_condition(self): bt = BaseThrottler() bt._status = 'running' self.assertEqual((True, False), bt._dequeue_condition()) self.assertEqual('waiting', bt._status) bt._status = 'paused' self.assertEqual((True, False), bt._dequeue_condition()) self.assertEqual('paused', bt._status) for wait_enqueued in [True, False]: bt._status = 'stopped' bt._wait_enqueued = wait_enqueued self.assertEqual((False, False), bt._dequeue_condition()) self.assertEqual('ending', bt._status) throttled_request, _ = bt._prepare_request(self.default_request) bt._enqueue_request(throttled_request) bt._status = 'running' self.assertEqual((False, True), bt._dequeue_condition()) self.assertEqual('running', bt._status) bt._status = 'paused' self.assertEqual((True, False), bt._dequeue_condition()) self.assertEqual('paused', bt._status) for wait_enqueued in [True, False]: bt._status = 'stopped' bt._wait_enqueued = wait_enqueued if wait_enqueued: self.assertEqual((False, True), bt._dequeue_condition()) else: self.assertEqual((False, False), bt._dequeue_condition()) self.assertEqual('ending', bt._status)
def test_dequeue_condition(self): bt = BaseThrottler() bt._status = 'running' self.assertEqual((True, False), bt._dequeue_condition()) self.assertEqual('waiting', bt._status) bt._status = 'paused' self.assertEqual((True, False), bt._dequeue_condition()) self.assertEqual('paused', bt._status) for wait_enqueued in [True, False]: bt._status = 'stopped' bt._wait_enqueued = wait_enqueued self.assertEqual((False, False), bt._dequeue_condition()) self.assertEqual('ending', bt._status) throttled_request, _ = bt._prepare_request(self.default_request) bt._enqueue_request(throttled_request) bt._status = 'running' self.assertEqual((False, True), bt._dequeue_condition()) self.assertEqual('running', bt._status) bt._status = 'paused' self.assertEqual((True, False), bt._dequeue_condition()) self.assertEqual('paused', bt._status) for wait_enqueued in [True, False]: bt._status = 'stopped' bt._wait_enqueued = wait_enqueued if wait_enqueued: self.assertEqual((False, True), bt._dequeue_condition()) else: self.assertEqual((False, False), bt._dequeue_condition()) self.assertEqual('ending', bt._status)