<s> import datetime <EOL> import unittest <EOL> from datetime import timedelta <EOL> import mock <EOL> from olympia . amo . celery import task <EOL> from olympia . amo . utils import utc_millesecs_from_epoch <EOL> @ task <EOL> def fake_task ( ** kw ) : <EOL> pass <EOL> class TestTaskTiming ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> patch = mock . patch ( '<STR_LIT>' ) <EOL> self . cache = patch . start ( ) <EOL> self . addCleanup ( patch . stop ) <EOL> patch = mock . patch ( '<STR_LIT>' ) <EOL> self . statsd = patch . start ( ) <EOL> self . addCleanup ( patch . stop ) <EOL> def test_cache_start_time ( self ) : <EOL> fake_task . delay ( ) <EOL> assert self . cache . set . call_args [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] . startswith ( '<STR_LIT>' ) <EOL> def test_track_run_time ( self ) : <EOL> minute_ago = datetime . datetime . now ( ) - timedelta ( minutes = <NUM_LIT:1> ) <EOL> task_start = utc_millesecs_from_epoch ( minute_ago ) <EOL> self . cache . get . return_value = task_start <EOL> fake_task . delay ( ) <EOL> approx_run_time = utc_millesecs_from_epoch ( ) - task_start <EOL> assert ( self . statsd . timing . call_args [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] == <EOL> '<STR_LIT>' ) <EOL> actual_run_time = self . statsd . timing . call_args [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] <EOL> fuzz = <NUM_LIT> <EOL> assert ( actual_run_time >= ( approx_run_time - fuzz ) and <EOL> actual_run_time <= ( approx_run_time + fuzz ) ) <EOL> assert self . cache . get . call_args [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] . startswith ( '<STR_LIT>' ) <EOL> assert self . cache . delete . call_args [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] . startswith ( '<STR_LIT>' ) <EOL> def test_handle_cache_miss_for_stats ( self ) : <EOL> self . cache . get . return_value = None <EOL> fake_task . delay ( ) <EOL> assert not self . statsd . timing . called </s>
