Skip to main content
https://travis-ci.org/snudler6/time-travel.svg?branch=master https://ci.appveyor.com/api/projects/status/y13ewnvmj0muoapf/branch/master?svg=true Documentation Status https://img.shields.io/pypi/pyversions/time-travel.svg

time-travel - time and I/O mocking library

time-travel is a python library that helps users write deterministic tests for time sensitive and I/O intensive code.

time-travel supports python 2.7, 3.4, 3.5, 3.6 and pypy2 on both Linux and Windows.

Install

$ pip install time_travel

Testing Time Sensitive Code

Imagine testing a state machine that times out after some time passes. One way to test it would be:

def test_state_timeout():
    sm.handle_event(event=...)
    time.sleep(5)
    sm.handle_event(event=...)
    assert sm.state == TIMEOUT

This is bad for several reasons:

  • Your test takes 5 seconds to run. That’s a no-no.

  • time.sleep() promises that the process will sleep x seconds at most. This test might fail randomly, depending on how sensitive your state machine is.

There’s nothing worse than a heisenbuild (well, perhaps a SLOW heisenbuild). Here’s a better way to do this using time-travel:

def test_state_timeout():
    with TimeTravel() as tt:
        sm.handle_event(event=...)
        tt.clock.time += 5
        sm.handle_event(event=...)
        assert sm.state == TIMEOUT

When the handle_event method is called it will probably check the time using one of time or datetime modules. These modules are patched by time-travel and return the value stored in TimeTravel.clock.time.

From now on, your time sensitive tests will run faster, accurately, and your build will be consistent.

Testing I/O Code

time-travel also mocks I/O event interfaces such as select and poll.

Testing code that uses select is easy - you just inject a real socket object and send data to it from your test code. But what about timeouts? Testing behaviour that occurs on timeout forces you to actually wait! That’s bananas!

Here’s how you’d do it with time-travel:

def test_select_timeout():
    with TimeTravel() as tt:
        sock = socket.socket()
        tt.add_future_event(2, sock, tt.event_types.select.WRITE)
        start = time.time()
        assert select.select([sock], [sock], []) == ([], [sock], [])  # This will be satisfied after "2 seconds"
        assert time.time() == start + 2  # You see? 2 seconds!
        assert select.select([sock], [sock], [], 100) == ([], [], [])  # This is the "timeout"
        assert time.time() == start + 2 + 100

Once again, this code will run instantly.

Oh yes, sock doesn’t even have to be a socket object :)

For detailed information and usage examples, see the full documentation. You know you want to.

Metadata

Release files for time_travel 1.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for time_travel 1.1.2
File Size Uploaded Provenance
time_travel-1.1.2.tar.gz 9.5 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for time_travel 1.1.2
File Interpreter ABI Platform
time_travel-1.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 22.2 kB

Release files / time_travel-1.1.2.tar.gz

Download URL time_travel-1.1.2.tar.gz
Size 9.5 kB
Tags Source
SHA-256 checksum
How to use checksums
55c99d00b169c012e4e7fbebcd208d2e5f132abfbc7bbceb30533ef06f35a44d
BLAKE2b-256 checksum
How to use checksums
0764922fe9d3bd079b4fbea9726997ed216328d9163490c5f0df8afa58baebab
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.6.3

Release files / time_travel-1.1.2-py3-none-any.whl

Download URL time_travel-1.1.2-py3-none-any.whl
Size 12.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
82829b820f9529f714581dfcdb3f96917bc8dc9b28075dbdc144a5e983202038
BLAKE2b-256 checksum
How to use checksums
a14ae9583fa64094ce05f3450c35db3d183a0a54964e70a913fb572885bbb0ef
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.6.3

Security for time_travel 1.1.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page