def test_consecutive_failures(self):
    '''Verify that a task is unhealthy only after max_consecutive_failures is exceeded'''
    initial_interval_secs = 2
    interval_secs = 1
    self.expect_health_check(False, num_calls=2)
    self.expect_health_check(True)
    self.expect_health_check(False, num_calls=3)
    self.replay()
    hct = HealthCheckerThread(
        self._checker,
        interval_secs=interval_secs,
        initial_interval_secs=initial_interval_secs,
        max_consecutive_failures=2,
        clock=self._clock)
    hct.start()

    # 2 consecutive health check failures followed by a successful health check.
    self._clock.tick(initial_interval_secs)
    assert hct.status is None
    self._clock.tick(interval_secs)
    assert hct.status is None
    self._clock.tick(interval_secs)
    assert hct.status is None

    # 3 consecutive health check failures.
    self._clock.tick(interval_secs)
    assert hct.status is None
    self._clock.tick(interval_secs)
    assert hct.status is None
    self._clock.tick(interval_secs)
    thread_yield()
    assert hct.status is not None
    hct.stop()
    self.verify()
 def test_initial_interval_whatev(self):
   self.expect_health_check(False)
   self.replay()
   hct = HealthCheckerThread(
     self._checker,
     interval_secs=5,
     initial_interval_secs=0,
     clock=self._clock)
   hct.start()
   assert hct.status is not None
   hct.stop()
   self.verify()
 def test_initial_interval_whatev(self):
   self.expect_health_check(False)
   self.replay()
   hct = HealthCheckerThread(
     self._checker,
     interval_secs=5,
     initial_interval_secs=0,
     clock=self._clock)
   hct.start()
   assert hct.status.status == TaskState.Value('TASK_FAILED')
   hct.stop()
   self.verify()
 def test_initial_interval_2x(self):
   self.expect_health_check(False)
   self.replay()
   hct = HealthCheckerThread(self._checker, interval_secs=5, clock=self._clock)
   hct.start()
   thread_yield()
   assert hct.status is None
   self._clock.tick(6)
   assert hct.status is None
   self._clock.tick(3)
   assert hct.status is None
   self._clock.tick(5)
   thread_yield()
   assert hct.status is not None
   hct.stop()
   self.verify()