예제 #1
0
def test_once_single_call():
    mock = Mock()
    mock.side_effect = lambda: time.sleep(0.2)

    schedule.once().do(mock)

    for _ in range(10):
        schedule.run_pending()
        time.sleep(0.05)

    mock.assert_called_once()
예제 #2
0
def test_once_single_call():
    mock = Mock()
    mock.side_effect = lambda: time.sleep(0.2)

    schedule.once().do(mock)

    assert repr(schedule.jobs[0]).startswith("Once at")

    for _ in range(10):
        schedule.run_pending()
        time.sleep(0.05)

    mock.assert_called_once()
예제 #3
0
def test_once():
    def job(path: str):
        print("job executed!")
        path = Path(path).write_text(str(time.time()))

    with tempfile.NamedTemporaryFile("r") as file:
        # Schedule the above to run once, two seconds from now
        start = time.time()
        schedule.once(datetime.fromtimestamp(start + 2)).do(job, file.name)
        assert file.readline() == ""

        # We trigger this every now and then, but the job should only execute at the
        # specified time.
        while time.time() < start + 3:
            schedule.run_pending()
            time.sleep(0.05)

        file.seek(0)
        # Verify that the written time was within 0.1 seconds of the expected time
        assert float(file.readline()) - 2 == pytest.approx(start, abs=0.1)