コード例 #1
0
    def test_sleep_until_next_operation_should_wait_for_the_closest_operation(
            self):
        operations = [
            PeriodicOperation("one",
                              lambda: None,
                              period=datetime.timedelta(seconds=60)),
            PeriodicOperation("one",
                              lambda: None,
                              period=datetime.timedelta(hours=1)),
            PeriodicOperation(
                "one", lambda: None,
                period=datetime.timedelta(seconds=10)),  # closest operation
            PeriodicOperation("one",
                              lambda: None,
                              period=datetime.timedelta(minutes=11)),
            PeriodicOperation("one",
                              lambda: None,
                              period=datetime.timedelta(days=1))
        ]
        for op in operations:  # pylint: disable=invalid-name
            op.run()

        def mock_sleep(seconds):
            mock_sleep.seconds = seconds

        mock_sleep.seconds = 0

        with patch("azurelinuxagent.ga.periodic_operation.time.sleep",
                   side_effect=mock_sleep):
            PeriodicOperation.sleep_until_next_operation(operations)
            self.assertAlmostEqual(mock_sleep.seconds, 10, 0,
                                   "did not sleep for the expected time")
コード例 #2
0
    def test_sleep_until_next_operation_should_wait_for_the_closest_operation(
            self):
        operations = [
            TestPeriodicOperation.NoOp(datetime.timedelta(seconds=60)),
            TestPeriodicOperation.NoOp(datetime.timedelta(hours=1)),
            TestPeriodicOperation.NoOp(
                datetime.timedelta(seconds=10)),  # closest operation
            TestPeriodicOperation.NoOp(datetime.timedelta(minutes=11)),
            TestPeriodicOperation.NoOp(datetime.timedelta(days=1))
        ]
        for op in operations:
            op.run()

        def mock_sleep(seconds):
            mock_sleep.seconds = seconds

        mock_sleep.seconds = 0

        with patch("azurelinuxagent.ga.periodic_operation.time.sleep",
                   side_effect=mock_sleep):
            PeriodicOperation.sleep_until_next_operation(operations)
            self.assertAlmostEqual(mock_sleep.seconds, 10, 0,
                                   "did not sleep for the expected time")