def test_stats(self):
    self.time_mock.return_value = 1000
    pb_headers = {'protocolbuffertype': 'Request', 'appdata': 'test-app', 'Version': 'test-version', 'Module': 'test-module'}
    pb_body = 'does not matter'
    self.pb_remote_request_mock.return_value = ('BulkAdd', 'OK')
    self.fetch('/queues', method='POST', body=pb_body, headers=pb_headers)
    self.pb_remote_request_mock.return_value = ('BulkAdd', 'OK')
    self.fetch('/queues', method='POST', body=pb_body, headers=pb_headers)
    self.pb_remote_request_mock.return_value = ('FetchTask', 'OK')
    self.fetch('/queues', method='POST', body=pb_body, headers=pb_headers)
    self.pb_remote_request_mock.return_value = ('PauseQueue', 'UNKNOWN_QUEUE')
    self.fetch('/queues', method='POST', body=pb_body, headers=pb_headers)
    self.pb_remote_request_mock.return_value = ('PauseQueue', 'OK')
    self.fetch('/queues', method='POST', body=pb_body, headers=pb_headers)
    self.pb_remote_request_mock.return_value = ('FetchTask', 'UNKNOWN_TASK')
    self.fetch('/queues', method='POST', body=pb_body, headers=pb_headers)
    self.get_http_status_mock.return_value = 200
    path = '/taskqueue/v1beta2/projects/app1/taskqueues/queue1'
    self.fetch(path, method='GET')
    self.get_http_status_mock.return_value = 200
    path = '/taskqueue/v1beta2/projects/app1/taskqueues/queue1'
    self.fetch(path, method='GET')
    self.get_http_status_mock.return_value = 200
    path = '/taskqueue/v1beta2/projects/app1/taskqueues/queue1/tasks'
    self.fetch(path, method='GET')
    self.get_http_status_mock.return_value = 200
    path = '/taskqueue/v1beta2/projects/app1/taskqueues/qeueu1/tasks'
    self.fetch(path, method='POST', allow_nonstandard_methods=True)
    self.get_http_status_mock.return_value = 200
    path = '/taskqueue/v1beta2/projects/app1/taskqueues/qeueu1/tasks'
    self.fetch(path, method='POST', allow_nonstandard_methods=True)
    self.get_http_status_mock.return_value = 200
    path = '/taskqueue/v1beta2/projects/app1/taskqueues/qeueu1/tasks/task1'
    self.fetch(path, method='GET')
    self.get_http_status_mock.return_value = 500
    path = '/taskqueue/v1beta2/projects/app1/taskqueues/qeueu1/tasks/task1'
    self.fetch(path, method='DELETE', allow_nonstandard_methods=True)
    self.get_http_status_mock.return_value = 404
    path = '/taskqueue/v1beta2/projects/app1/taskqueues/unknown/tasks/task1'
    self.fetch(path, method='PATCH', allow_nonstandard_methods=True)
    self.get_http_status_mock.return_value = 404
    path = '/taskqueue/v1beta2/projects/unknown/taskqueues/qeueu1/tasks/task1'
    self.fetch(path, method='GET')
    raw_stats = self.fetch('/service-stats').body
    stats = json.loads(raw_stats)
    self.assertGreater(stats['cumulative_counters'].pop('from'), 0)
    self.assertGreater(stats['cumulative_counters'].pop('to'), 0)
    self.assertGreater(stats['recent_stats'].pop('from'), 0)
    self.assertGreater(stats['recent_stats'].pop('to'), 0)
    self.assertGreaterEqual(stats['recent_stats'].pop('avg_latency'), 0)
    self.assertEqual(stats, {'current_requests': 0, 'cumulative_counters': {'all': 15, 'failed': 5, 'pb_reqs': 6, 'rest_reqs': 9}, 'recent_stats': {'all': 15, 'failed': 5, 'pb_reqs': 6, 'rest_reqs': 9, 'by_rest_method': {'get_task': 2, 'get_tasks': 1, 'delete_task': 1, 'get_queue': 2, 'patch_task': 1, 'post_tasks': 2}, 'by_rest_status': {'200': 6, '404': 2, '500': 1}, 'by_pb_method': {'BulkAdd': 2, 'PauseQueue': 2, 'FetchTask': 2}, 'by_pb_status': {'OK': 4, 'UNKNOWN_TASK': 1, 'UNKNOWN_QUEUE': 1}}})
def test_stats(self):
    self.time_mock.return_value = 1000
    pb_headers = {'protocolbuffertype': 'Request', 'appdata': 'test-app', 'Version': 'test-version', 'Module': 'test-module'}
    pb_body = 'does not matter'
    self.pb_remote_request_mock.return_value = ('BulkAdd', 'OK')
    self.fetch('/queues', method='POST', body=pb_body, headers=pb_headers)
    self.pb_remote_request_mock.return_value = ('BulkAdd', 'OK')
    self.fetch('/queues', method='POST', body=pb_body, headers=pb_headers)
    self.pb_remote_request_mock.return_value = ('FetchTask', 'OK')
    self.fetch('/queues', method='POST', body=pb_body, headers=pb_headers)
    self.pb_remote_request_mock.return_value = ('PauseQueue', 'UNKNOWN_QUEUE')
    self.fetch('/queues', method='POST', body=pb_body, headers=pb_headers)
    self.pb_remote_request_mock.return_value = ('PauseQueue', 'OK')
    self.fetch('/queues', method='POST', body=pb_body, headers=pb_headers)
    self.pb_remote_request_mock.return_value = ('FetchTask', 'UNKNOWN_TASK')
    self.fetch('/queues', method='POST', body=pb_body, headers=pb_headers)
    self.get_http_status_mock.return_value = 200
    path = '/taskqueue/v1beta2/projects/app1/taskqueues/queue1'
    self.fetch(path, method='GET')
    self.get_http_status_mock.return_value = 200
    path = '/taskqueue/v1beta2/projects/app1/taskqueues/queue1'
    self.fetch(path, method='GET')
    self.get_http_status_mock.return_value = 200
    path = '/taskqueue/v1beta2/projects/app1/taskqueues/queue1/tasks'
    self.fetch(path, method='GET')
    self.get_http_status_mock.return_value = 200
    path = '/taskqueue/v1beta2/projects/app1/taskqueues/qeueu1/tasks'
    self.fetch(path, method='POST', allow_nonstandard_methods=True)
    self.get_http_status_mock.return_value = 200
    path = '/taskqueue/v1beta2/projects/app1/taskqueues/qeueu1/tasks'
    self.fetch(path, method='POST', allow_nonstandard_methods=True)
    self.get_http_status_mock.return_value = 200
    path = '/taskqueue/v1beta2/projects/app1/taskqueues/qeueu1/tasks/task1'
    self.fetch(path, method='GET')
    self.get_http_status_mock.return_value = 500
    path = '/taskqueue/v1beta2/projects/app1/taskqueues/qeueu1/tasks/task1'
    self.fetch(path, method='DELETE', allow_nonstandard_methods=True)
    self.get_http_status_mock.return_value = 404
    path = '/taskqueue/v1beta2/projects/app1/taskqueues/unknown/tasks/task1'
    self.fetch(path, method='PATCH', allow_nonstandard_methods=True)
    self.get_http_status_mock.return_value = 404
    path = '/taskqueue/v1beta2/projects/unknown/taskqueues/qeueu1/tasks/task1'
    self.fetch(path, method='GET')
    raw_stats = self.fetch('/service-stats').body
    stats = json.loads(raw_stats)
    self.assertGreater(stats['cumulative_counters'].pop('from'), 0)
    self.assertGreater(stats['cumulative_counters'].pop('to'), 0)
    self.assertGreater(stats['recent_stats'].pop('from'), 0)
    self.assertGreater(stats['recent_stats'].pop('to'), 0)
    self.assertGreaterEqual(stats['recent_stats'].pop('avg_latency'), 0)
    self.assertEqual(stats, {'current_requests': 0, 'cumulative_counters': {'all': 15, 'failed': 5, 'pb_reqs': 6, 'rest_reqs': 9}, 'recent_stats': {'all': 15, 'failed': 5, 'pb_reqs': 6, 'rest_reqs': 9, 'by_rest_method': {'get_task': 2, 'get_tasks': 1, 'delete_task': 1, 'get_queue': 2, 'patch_task': 1, 'post_tasks': 2}, 'by_rest_status': {'200': 6, '404': 2, '500': 1}, 'by_pb_method': {'BulkAdd': 2, 'PauseQueue': 2, 'FetchTask': 2}, 'by_pb_status': {'OK': 4, 'UNKNOWN_TASK': 1, 'UNKNOWN_QUEUE': 1}}})