예제 #1
0
    def test_call_rate_delay_throttle_disabled(self):
        """Test that the limiter is a noop if no delay set."""
        runs = []

        limit = pilight.CallRateDelayThrottle(self.hass, 0.0)
        action = limit.limited(lambda x: runs.append(x))

        for i in range(3):
            action(i)

        self.assertEqual(runs, [0, 1, 2])
예제 #2
0
    def test_call_rate_delay_throttle_enabled(self):
        """Test that throttling actually work."""
        runs = []
        delay = 5.0

        limit = pilight.CallRateDelayThrottle(self.hass, delay)
        action = limit.limited(lambda x: runs.append(x))

        for i in range(3):
            action(i)

        assert runs == []

        exp = []
        now = dt_util.utcnow()
        for i in range(3):
            exp.append(i)
            shifted_time = now + (timedelta(seconds=delay + 0.1) * i)
            async_fire_time_changed(self.hass, shifted_time)
            self.hass.block_till_done()
            assert runs == exp
예제 #3
0
    def test_call_rate_delay_throttle_enabled(self):
        """Test that throttling actually work."""
        runs = []
        delay = 5.0

        limit = pilight.CallRateDelayThrottle(self.hass, delay)
        action = limit.limited(lambda x: runs.append(x))

        for i in range(3):
            action(i)

        self.assertEqual(runs, [])

        exp = []
        now = dt_util.utcnow()
        for i in range(3):
            exp.append(i)
            shifted_time = now + (timedelta(seconds=delay + 0.1) * i)
            self.hass.bus.fire(ha.EVENT_TIME_CHANGED,
                               {ha.ATTR_NOW: shifted_time})
            self.hass.block_till_done()
            self.assertEqual(runs, exp)