Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

help: use the schedulers with simulated time for tests #293

Closed
liordon opened this issue Mar 12, 2018 · 5 comments
Closed

help: use the schedulers with simulated time for tests #293

liordon opened this issue Mar 12, 2018 · 5 comments

Comments

@liordon
Copy link

liordon commented Mar 12, 2018

I want to use the apscheduler in a module I am developing but I don't understand how to make it run with a simulated clock.
If I could run it in simulated time I could test periodic events and long waits without actually putthing the thread into long sleeps.
Is there any possibility to create such a simulated scheduler? if not, how were the different scheduler modules tested?

Thanks in advance!

@kaos
Copy link

kaos commented Jun 25, 2019

This is not a Q&A forum, use stackoverflow or the mailinglist for that. Just a hint as to a possibility why there's been no response here..

@kaos
Copy link

kaos commented Aug 13, 2019

For testing purposes, you can run the jobs with a custom trigger, as it is the trigger that decides when it is time to run a particular job. If you base it on another trigger that you actually want to use, you can rely on its implementation, and simply tweak the output times to shrink the times as needed.

The different scheduler modules are tested in isolation, so triggers are tested with, "given these inputs, what are your outputs", no need to actually wait for those conditions to actually occur (they're date time in, date time out) while the executors are tested for how they execute a job, no need to "wait" for a condition to execute the job to occur. This is basic unit testing strategy.

Integration tests on the other hand, take the system as a whole and tests it. I haven't looked too close at the test suite to find if there are also integration tests.

@funkyfuture
Copy link

i have a related issue. i currently would want to write unit tests for scheduler's event listeners. to do so, i would need to invent a whole set of mock classes, b/c everything is so intertwined. and this is too much effort for my case where i'm fixing a c&p error. as an example this how a test could look like:

from apscheduler.events import EVENT_JOB_ERROR
from apscheduler.mocks import Job, JobExecutionEvent

from myapp import on_error


def _pass():
    pass


def test_scheduler_listener():
    job = Job(func=_pass)
    event = JobExecutionEvent(code=EVENT_JOB_ERROR, job=job)
    on_error(event)

my point is that i think a set of utilities to facilitate better testing would be a great addition to a great lib.

funkyfuture added a commit to funkyfuture/deck-chores that referenced this issue Apr 4, 2020
The code in question is still uncovered by tests, because of the complexity
it would introduce, see this comment:
agronholm/apscheduler#293 (comment)

Closes #52
@bendikro
Copy link

You can use freezegun to advance the time

# Setup scheduler and add job with trigger in 5 seconds
ts_advance = datetime.now() + timedelta(seconds=5)
with freezegun.freeze_time(ts_advance, tick=True):
    await asyncio.sleep(0.2)
    # The job should now have been executed

@agronholm
Copy link
Owner

Closing as stale/QA.

@agronholm agronholm closed this as not planned Won't fix, can't repro, duplicate, stale Aug 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants