Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 # Copyright 2015 The Chromium Authors. All rights reserved. | 1 # Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 import copy | 5 import copy |
| 6 import datetime | 6 import datetime |
| 7 import os | 7 import os |
| 8 import time | 8 import time |
| 9 import unittest | 9 import unittest |
| 10 | 10 |
| 11 import endpoints | |
| 11 import gae_ts_mon | 12 import gae_ts_mon |
| 12 import mock | 13 import mock |
| 13 import webapp2 | 14 import webapp2 |
| 14 | 15 |
| 15 from infra_libs.ts_mon import config | 16 from infra_libs.ts_mon import config |
| 16 from infra_libs.ts_mon import shared | 17 from infra_libs.ts_mon import shared |
| 17 from infra_libs.ts_mon.common import http_metrics | 18 from infra_libs.ts_mon.common import http_metrics |
| 18 from infra_libs.ts_mon.common import interface | 19 from infra_libs.ts_mon.common import interface |
| 19 from infra_libs.ts_mon.common import monitors | 20 from infra_libs.ts_mon.common import monitors |
| 20 from infra_libs.ts_mon.common import targets | 21 from infra_libs.ts_mon.common import targets |
| 21 from infra_libs.ts_mon.common.test import stubs | 22 from infra_libs.ts_mon.common.test import stubs |
| 23 from protorpc import message_types | |
| 24 from protorpc import remote | |
| 22 from testing_utils import testing | 25 from testing_utils import testing |
| 23 | 26 |
| 24 | 27 |
| 25 class InitializeTest(testing.AppengineTestCase): | 28 class InitializeTest(testing.AppengineTestCase): |
| 26 def setUp(self): | 29 def setUp(self): |
| 27 super(InitializeTest, self).setUp() | 30 super(InitializeTest, self).setUp() |
| 28 | 31 |
| 29 config.reset_for_unittest() | 32 config.reset_for_unittest() |
| 30 target = targets.TaskTarget('test_service', 'test_job', | 33 target = targets.TaskTarget('test_service', 'test_job', |
| 31 'test_region', 'test_host') | 34 'test_region', 'test_host') |
| (...skipping 309 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 341 | 344 |
| 342 app = webapp2.WSGIApplication([('/', Handler)]) | 345 app = webapp2.WSGIApplication([('/', Handler)]) |
| 343 config.instrument_wsgi_application(app) | 346 config.instrument_wsgi_application(app) |
| 344 | 347 |
| 345 app.get_response('/', POST='foo') | 348 app.get_response('/', POST='foo') |
| 346 | 349 |
| 347 fields = {'name': '^/$', 'status': 200, 'is_robot': False} | 350 fields = {'name': '^/$', 'status': 200, 'is_robot': False} |
| 348 self.assertEqual(1, http_metrics.server_response_status.get(fields)) | 351 self.assertEqual(1, http_metrics.server_response_status.get(fields)) |
| 349 self.assertEqual( | 352 self.assertEqual( |
| 350 len('foo'), http_metrics.server_request_bytes.get(fields).sum) | 353 len('foo'), http_metrics.server_request_bytes.get(fields).sum) |
| 354 | |
|
dsansome
2016/03/15 02:01:20
nit: delete 1 \n
Sergey Berezin
2016/03/17 22:14:04
Done.
| |
| 355 | |
| 356 | |
| 357 class FakeTime(object): | |
| 358 def __init__(self): | |
| 359 self.timestamp_now = 1000.0 | |
| 360 | |
| 361 def time_fn(self): | |
| 362 self.timestamp_now += 0.2 | |
| 363 return self.timestamp_now | |
| 364 | |
| 365 | |
| 366 @endpoints.api(name='testapi', version='v1') | |
| 367 class TestEndpoint(remote.Service): | |
| 368 | |
| 369 @gae_ts_mon.instrument_endpoint(time_fn=FakeTime().time_fn) | |
| 370 @endpoints.method(message_types.VoidMessage, message_types.VoidMessage, | |
| 371 name='method_good') | |
| 372 def do_good(self, request): | |
| 373 return request | |
| 374 | |
| 375 @gae_ts_mon.instrument_endpoint(time_fn=FakeTime().time_fn) | |
| 376 @endpoints.method(message_types.VoidMessage, message_types.VoidMessage, | |
| 377 name='method_bad') | |
| 378 def do_bad(self, request): | |
| 379 raise Exception | |
| 380 | |
| 381 @gae_ts_mon.instrument_endpoint(time_fn=FakeTime().time_fn) | |
| 382 @endpoints.method(message_types.VoidMessage, message_types.VoidMessage, | |
| 383 name='method_400') | |
| 384 def do_400(self, request): | |
| 385 raise endpoints.BadRequestException('Bad request') | |
| 386 | |
| 387 | |
| 388 class InstrumentEndpointTest(testing.EndpointsTestCase): | |
| 389 api_service_cls = TestEndpoint | |
| 390 | |
| 391 def setUp(self): | |
| 392 super(InstrumentEndpointTest, self).setUp() | |
| 393 | |
| 394 config.reset_for_unittest() | |
| 395 target = targets.TaskTarget('test_service', 'test_job', | |
| 396 'test_region', 'test_host') | |
| 397 self.mock_state = interface.State(target=target) | |
| 398 self.mock_state.metrics = copy.copy(interface.state.metrics) | |
| 399 mock.patch('infra_libs.ts_mon.common.interface.state', | |
| 400 new=self.mock_state).start() | |
| 401 | |
| 402 mock.patch('infra_libs.ts_mon.common.monitors.PubSubMonitor', | |
| 403 autospec=True).start() | |
| 404 | |
| 405 def tearDown(self): | |
| 406 config.reset_for_unittest() | |
| 407 self.assertEqual([], list(shared.global_metrics_callbacks)) | |
| 408 mock.patch.stopall() | |
| 409 super(InstrumentEndpointTest, self).tearDown() | |
| 410 | |
| 411 def test_good(self): | |
| 412 self.call_api('do_good') | |
| 413 fields = {'name': 'method_good', 'status': 200, 'is_robot': False} | |
| 414 self.assertEqual(1, http_metrics.server_response_status.get(fields)) | |
| 415 self.assertEqual(200, http_metrics.server_durations.get(fields).sum) | |
| 416 | |
| 417 def test_bad(self): | |
| 418 with self.call_should_fail('500 Internal Server Error'): | |
| 419 self.call_api('do_bad') | |
| 420 fields = {'name': 'method_bad', 'status': 500, 'is_robot': False} | |
| 421 self.assertEqual(1, http_metrics.server_response_status.get(fields)) | |
| 422 self.assertEqual(200, http_metrics.server_durations.get(fields).sum) | |
| 423 | |
| 424 def test_400(self): | |
| 425 with self.call_should_fail('400 Bad Request'): | |
| 426 self.call_api('do_400') | |
| 427 fields = {'name': 'method_400', 'status': 400, 'is_robot': False} | |
| 428 self.assertEqual(1, http_metrics.server_response_status.get(fields)) | |
| 429 self.assertEqual(200, http_metrics.server_durations.get(fields).sum) | |
| OLD | NEW |