class StatisticsTests(TestCase): def setUp(self): self.statistics = Statistics() def test_rejection_count(self): expected_count = 3 for i in range(expected_count): self.statistics.task_rejected(a_request()) self.assertEqual(expected_count, self.statistics.rejection_count) def test_request_count(self): expected_count = 10 for i in range(expected_count): self.statistics.task_created(a_request()) self.assertEqual(expected_count, self.statistics.arrival_count) def test_reset(self): self.statistics.task_created(a_request()) self.statistics.rejection_of(a_request()) self.statistics.task_failed(a_request()) self.statistics.task_successful(a_request(10)) self.statistics.reset() self.assertEqual(0, self.statistics.arrival_count) self.assertEqual(0, self.statistics.rejection_count) self.assertEqual(0, self.statistics.failure_count) self.assertIsNone(self.statistics.response_time) def test_error_response(self): self.statistics.task_failed(a_request()) self.assertEqual(1, self.statistics.failure_count) def test_success_count(self): self.statistics.task_successful(a_request()) self.statistics.task_successful(a_request()) self.statistics.task_successful(a_request()) self.statistics.task_successful(a_request()) self.statistics.rejection_of(a_request()) self.statistics.task_failed(a_request()) self.assertEqual(4, self.statistics.success_count) def test_reliability(self): self.statistics.task_successful(a_request()) self.statistics.task_successful(a_request()) self.statistics.task_successful(a_request()) self.statistics.task_successful(a_request()) self.statistics.task_rejected(a_request()) self.statistics.task_failed(a_request()) expected = 4 / (4+2) self.assertEqual(expected, self.statistics.reliability) def test_reliability_with_only_rejection(self): self.statistics.task_rejected(a_request()) self.statistics.task_rejected(a_request()) self.statistics.task_rejected(a_request()) self.statistics.task_rejected(a_request()) expected = 0. self.assertEqual(expected, self.statistics.reliability) def test_reliability_with_only_errors(self): self.statistics.task_failed(a_request()) self.statistics.task_failed(a_request()) self.statistics.task_failed(a_request()) self.statistics.task_failed(a_request()) expected = 0. self.assertEqual(expected, self.statistics.reliability) def test_response_time(self): response_times = [10, 6, 7, 9, 8] self._success_of_tasks(response_times) expectation = sum(response_times) / len(response_times) self.assertEqual(expectation, self.statistics.response_time) def test_response_time_per_operation(self): tasks = [{"operation": "foo", "response_time": 12}, {"operation": "foo", "response_time": 14}, {"operation": "bar", "response_time": 5}, {"operation": "bar", "response_time": 3}, {"operation": "quz", "response_time": 10}] for each_task in tasks: request = a_task(**each_task) self.statistics.task_successful(request) self.assertEqual(13.0, self.statistics.response_time_for("foo")) self.assertEqual(4.0, self.statistics.response_time_for("bar")) self.assertEqual(10, self.statistics.response_time_for("quz")) def _success_of_tasks(self, response_times=[3, 4, 5, 6]): for each_response_time in response_times: task = a_task(response_time=each_response_time) self.statistics.task_successful(task)
class StatisticsTests(TestCase): def setUp(self): self.statistics = Statistics() def test_rejection_count(self): expected_count = 3 for i in range(expected_count): self.statistics.rejection_of(_a_fake_request()) self.assertEqual(expected_count, self.statistics.rejection_count) def test_request_count(self): expected_count = 10 for i in range(expected_count): self.statistics.arrival_of(_a_fake_request()) self.assertEqual(expected_count, self.statistics.arrival_count) def test_reset(self): self.statistics.arrival_of(_a_fake_request()) self.statistics.rejection_of(_a_fake_request()) self.statistics.error_replied_to(_a_fake_request()) self.statistics.success_replied_to(_a_fake_request(10)) self.statistics.reset() self.assertEqual(0, self.statistics.arrival_count) self.assertEqual(0, self.statistics.rejection_count) self.assertEqual(0, self.statistics.failure_count) self.assertIsNone(self.statistics.response_time) def test_error_response(self): self.statistics.error_replied_to(_a_fake_request()) self.assertEqual(1, self.statistics.failure_count) def test_success_count(self): self.statistics.success_replied_to(_a_fake_request()) self.statistics.success_replied_to(_a_fake_request()) self.statistics.success_replied_to(_a_fake_request()) self.statistics.success_replied_to(_a_fake_request()) self.statistics.rejection_of(_a_fake_request()) self.statistics.error_replied_to(_a_fake_request()) self.assertEqual(4, self.statistics.success_count) def test_reliability(self): self.statistics.success_replied_to(_a_fake_request()) self.statistics.success_replied_to(_a_fake_request()) self.statistics.success_replied_to(_a_fake_request()) self.statistics.success_replied_to(_a_fake_request()) self.statistics.rejection_of(_a_fake_request()) self.statistics.error_replied_to(_a_fake_request()) expected = 4 / (4+2) self.assertEqual(expected, self.statistics.reliability) def test_response_time(self): response_times = [10, 6, 7, 9, 8] self._success_of_requests(response_times) expectation = sum(response_times) / len(response_times) self.assertEqual(expectation, self.statistics.response_time) def test_response_time_per_operation(self): requests = [{"operation": "foo", "response_time": 12}, {"operation": "foo", "response_time": 14}, {"operation": "bar", "response_time": 5}, {"operation": "bar", "response_time": 3}, {"operation": "quz", "response_time": 10}] for each_request in requests: request = _a_fake_request(**each_request) self.statistics.success_replied_to(request) self.assertEqual(13.0, self.statistics.response_time_for("foo")) self.assertEqual(4.0, self.statistics.response_time_for("bar")) self.assertEqual(10, self.statistics.response_time_for("quz")) def _success_of_requests(self, response_times=[3,4,5,6]): for each_response_time in response_times: request = _a_fake_request(response_time=each_response_time) self.statistics.success_replied_to(request)
class StatisticsTests(TestCase): def setUp(self): self.statistics = Statistics() def test_rejection_count(self): expected_count = 3 for i in range(expected_count): self.statistics.task_rejected(a_request()) self.assertEqual(expected_count, self.statistics.rejection_count) def test_request_count(self): expected_count = 10 for i in range(expected_count): self.statistics.task_created(a_request()) self.assertEqual(expected_count, self.statistics.arrival_count) def test_reset(self): self.statistics.task_created(a_request()) self.statistics.rejection_of(a_request()) self.statistics.task_failed(a_request()) self.statistics.task_successful(a_request(10)) self.statistics.reset() self.assertEqual(0, self.statistics.arrival_count) self.assertEqual(0, self.statistics.rejection_count) self.assertEqual(0, self.statistics.failure_count) self.assertIsNone(self.statistics.response_time) def test_error_response(self): self.statistics.task_failed(a_request()) self.assertEqual(1, self.statistics.failure_count) def test_success_count(self): self.statistics.task_successful(a_request()) self.statistics.task_successful(a_request()) self.statistics.task_successful(a_request()) self.statistics.task_successful(a_request()) self.statistics.rejection_of(a_request()) self.statistics.task_failed(a_request()) self.assertEqual(4, self.statistics.success_count) def test_reliability(self): self.statistics.task_successful(a_request()) self.statistics.task_successful(a_request()) self.statistics.task_successful(a_request()) self.statistics.task_successful(a_request()) self.statistics.task_rejected(a_request()) self.statistics.task_failed(a_request()) expected = 4 / (4 + 2) self.assertEqual(expected, self.statistics.reliability) def test_reliability_with_only_rejection(self): self.statistics.task_rejected(a_request()) self.statistics.task_rejected(a_request()) self.statistics.task_rejected(a_request()) self.statistics.task_rejected(a_request()) expected = 0. self.assertEqual(expected, self.statistics.reliability) def test_reliability_with_only_errors(self): self.statistics.task_failed(a_request()) self.statistics.task_failed(a_request()) self.statistics.task_failed(a_request()) self.statistics.task_failed(a_request()) expected = 0. self.assertEqual(expected, self.statistics.reliability) def test_response_time(self): response_times = [10, 6, 7, 9, 8] self._success_of_tasks(response_times) expectation = sum(response_times) / len(response_times) self.assertEqual(expectation, self.statistics.response_time) def test_response_time_per_operation(self): tasks = [{ "operation": "foo", "response_time": 12 }, { "operation": "foo", "response_time": 14 }, { "operation": "bar", "response_time": 5 }, { "operation": "bar", "response_time": 3 }, { "operation": "quz", "response_time": 10 }] for each_task in tasks: request = a_task(**each_task) self.statistics.task_successful(request) self.assertEqual(13.0, self.statistics.response_time_for("foo")) self.assertEqual(4.0, self.statistics.response_time_for("bar")) self.assertEqual(10, self.statistics.response_time_for("quz")) def _success_of_tasks(self, response_times=[3, 4, 5, 6]): for each_response_time in response_times: task = a_task(response_time=each_response_time) self.statistics.task_successful(task)