Example #1
0
 def test_flush_metrics_too_early(self):
     # Too early to send metrics.
     time_now = 10000
     datetime_now = datetime.datetime.utcfromtimestamp(time_now)
     less_than_min_ago = datetime_now - datetime.timedelta(seconds=59)
     interface.state.last_flushed = less_than_min_ago
     entity = shared.get_instance_entity()
     entity.task_num = 2
     self.assertFalse(config.flush_metrics_if_needed(time_now))
Example #2
0
 def test_flush_metrics_too_early(self):
   # Too early to send metrics.
   time_now = 10000
   datetime_now = datetime.datetime.utcfromtimestamp(time_now)
   less_than_min_ago = datetime_now - datetime.timedelta(seconds=59)
   interface.state.last_flushed = less_than_min_ago
   entity = shared.get_instance_entity()
   entity.task_num = 2
   self.assertFalse(config.flush_metrics_if_needed(time_now))
Example #3
0
 def test_flush_metrics_purged(self):
     # We lost our task_num; cannot send metrics.
     time_now = 10000
     datetime_now = datetime.datetime.utcfromtimestamp(time_now)
     more_than_min_ago = datetime_now - datetime.timedelta(seconds=61)
     interface.state.last_flushed = more_than_min_ago
     entity = shared.get_instance_entity()
     entity.task_num = -1
     interface.state.target.task_num = 2
     self.assertFalse(config.flush_metrics_if_needed(time_now))
Example #4
0
 def test_flush_metrics_purged(self):
   # We lost our task_num; cannot send metrics.
   time_now = 10000
   datetime_now = datetime.datetime.utcfromtimestamp(time_now)
   more_than_min_ago = datetime_now - datetime.timedelta(seconds=61)
   interface.state.last_flushed = more_than_min_ago
   entity = shared.get_instance_entity()
   entity.task_num = -1
   interface.state.target.task_num = 2
   self.assertFalse(config.flush_metrics_if_needed(time_now))
Example #5
0
 def test_flush_metrics_disabled(self, mock_flush):
     # We have task_num and due for sending metrics, but ts_mon is disabled.
     time_now = 10000
     datetime_now = datetime.datetime.utcfromtimestamp(time_now)
     more_than_min_ago = datetime_now - datetime.timedelta(seconds=61)
     interface.state.last_flushed = more_than_min_ago
     interface.state.flush_enabled_fn = lambda: False
     entity = shared.get_instance_entity()
     entity.task_num = 2
     self.assertFalse(config.flush_metrics_if_needed(time_now))
     self.assertEqual(0, mock_flush.call_count)
Example #6
0
 def test_flush_metrics_disabled(self, mock_flush):
   # We have task_num and due for sending metrics, but ts_mon is disabled.
   time_now = 10000
   datetime_now = datetime.datetime.utcfromtimestamp(time_now)
   more_than_min_ago = datetime_now - datetime.timedelta(seconds=61)
   interface.state.last_flushed = more_than_min_ago
   interface.state.flush_enabled_fn = lambda: False
   entity = shared.get_instance_entity()
   entity.task_num = 2
   self.assertFalse(config.flush_metrics_if_needed(time_now))
   self.assertEqual(0, mock_flush.call_count)
Example #7
0
 def test_flush_metrics_no_task_num_too_long(self):
     # We are not assigned task_num for too long; cannot send metrics.
     time_now = 10000
     datetime_now = datetime.datetime.utcfromtimestamp(time_now)
     too_long_ago = datetime_now - datetime.timedelta(
         seconds=shared.INSTANCE_EXPECTED_TO_HAVE_TASK_NUM_SEC + 1)
     interface.state.last_flushed = too_long_ago
     entity = shared.get_instance_entity()
     entity.task_num = -1
     entity.last_updated = too_long_ago
     interface.state.target.task_num = -1
     self.assertFalse(config.flush_metrics_if_needed(time_now))
Example #8
0
 def test_flush_metrics_no_task_num_too_long(self):
   # We are not assigned task_num for too long; cannot send metrics.
   time_now = 10000
   datetime_now = datetime.datetime.utcfromtimestamp(time_now)
   too_long_ago = datetime_now - datetime.timedelta(
       seconds=shared.INSTANCE_EXPECTED_TO_HAVE_TASK_NUM_SEC+1)
   interface.state.last_flushed = too_long_ago
   entity = shared.get_instance_entity()
   entity.task_num = -1
   entity.last_updated = too_long_ago
   interface.state.target.task_num = -1
   self.assertFalse(config.flush_metrics_if_needed(time_now))
Example #9
0
 def test_flush_metrics_successfully(self, mock_flush):
     # We have task_num and due for sending metrics.
     time_now = 10000
     datetime_now = datetime.datetime.utcfromtimestamp(time_now)
     more_than_min_ago = datetime_now - datetime.timedelta(seconds=61)
     interface.state.last_flushed = more_than_min_ago
     entity = shared.get_instance_entity()
     entity.task_num = 2
     # Global metrics must be erased after flush.
     test_global_metric = gae_ts_mon.GaugeMetric('test', 'foo', None)
     test_global_metric.set(42)
     interface.register_global_metrics([test_global_metric])
     self.assertEqual(42, test_global_metric.get())
     self.assertTrue(config.flush_metrics_if_needed(time_now))
     self.assertEqual(None, test_global_metric.get())
     mock_flush.assert_called_once_with()
Example #10
0
 def test_flush_metrics_successfully(self, mock_flush):
   # We have task_num and due for sending metrics.
   time_now = 10000
   datetime_now = datetime.datetime.utcfromtimestamp(time_now)
   more_than_min_ago = datetime_now - datetime.timedelta(seconds=61)
   interface.state.last_flushed = more_than_min_ago
   entity = shared.get_instance_entity()
   entity.task_num = 2
   # Global metrics must be erased after flush.
   test_global_metric = gae_ts_mon.GaugeMetric('test')
   test_global_metric.set(42)
   shared.register_global_metrics([test_global_metric])
   self.assertEqual(42, test_global_metric.get())
   self.assertTrue(config.flush_metrics_if_needed(time_now))
   self.assertEqual(None, test_global_metric.get())
   mock_flush.assert_called_once_with()