Coder Social home page Coder Social logo

time-travel's Introduction

https://travis-ci.org/snudler6/time-travel.svg?branch=master https://ci.appveyor.com/api/projects/status/y13ewnvmj0muoapf/branch/master?svg=true Documentation Status

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.

Links

Full documentation

PyPI project page

time-travel's People

Contributors

snudler6 avatar shacharoo avatar yoseft avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.