def test_percent_no_expected_data(self):
     """
     If time < interval has passed and the monitor has received some pings,
     it should still return 100%.
     """
     monitor = CoverageMonitor(10,
                               1.0,
                               "test",
                               create_time=self.reactor.time)
     monitor.reset()
     self.reactor.advance(1)
     monitor.ping()
     self.assertEqual(monitor.percent, 1.0)
class CoverageMonitorTest(ReactorHavingTest):
    def setUp(self):
        super(CoverageMonitorTest, self).setUp()
        self.monitor = CoverageMonitor(1,
                                       1.0,
                                       "test",
                                       create_time=self.reactor.time)

    def test_warn(self):
        self.monitor.ping()
        self.reactor.advance(1)
        self.assertFalse(self.monitor.warn())

        self.reactor.advance(1)
        self.assertTrue(self.monitor.warn())

        self.monitor.reset()
        self.assertFalse(self.monitor.warn())

    def test_percent_no_data(self):
        """
        If no time has passed and the monitor hasn't received any
        pings it should return 100%.
        """
        self.assertEqual(self.monitor.percent, 1.0)

    def test_percent_no_expected_data(self):
        """
        If time < interval has passed and the monitor has received some pings,
        it should still return 100%.
        """
        monitor = CoverageMonitor(10,
                                  1.0,
                                  "test",
                                  create_time=self.reactor.time)
        monitor.reset()
        self.reactor.advance(1)
        monitor.ping()
        self.assertEqual(monitor.percent, 1.0)

    def test_percent(self):
        self.reactor.advance(1)
        self.assertEqual(self.monitor.percent, 0.0)

        self.monitor.ping()
        self.reactor.advance(1)
        self.assertEqual(self.monitor.percent, 0.5)

    def test_percent_reset(self):
        self.reactor.advance(1)
        self.assertEqual(self.monitor.percent, 0.0)

        self.monitor.reset()
        self.monitor.ping()
        self.reactor.advance(1)
        self.assertEqual(self.monitor.percent, 1.0)

    def test_expected_count(self):
        self.reactor.advance(1)
        self.assertEqual(self.monitor.expected_count, 1.0)

        self.reactor.advance(1)
        self.assertEqual(self.monitor.expected_count, 2.0)

    def test_expected_count_reset(self):
        self.reactor.advance(1)
        self.assertEqual(self.monitor.expected_count, 1.0)

        self.monitor.reset()
        self.reactor.advance(1)
        self.assertEqual(self.monitor.expected_count, 1.0)

    def test_log(self):
        for i in range(100):
            self.monitor.ping()
            self.reactor.advance(1)
        self.monitor.log()
        self.assertTrue(
            "INFO: 100 of 100 expected test events (100.00%) "
            "occurred in the last 100.00s." in self.logfile.getvalue())

    def test_log_warning(self):
        for i in range(100):
            self.reactor.advance(1)
        self.monitor.log()
        self.assertTrue(
            "WARNING: 0 of 100 expected test events (0.00%) "
            "occurred in the last 100.00s." in self.logfile.getvalue())