예제 #1
0
def test_wait_timeout():
    with TimeTravel() as t:
        socket = mock.MagicMock()

        t.add_future_event(50 * 60 + 1, socket, t.event_types.select.READ)

        with pytest.raises(ValueError):
            wait_and_respond(socket)
예제 #2
0
def test_sleep_patch_sleep():
    with TimeTravel(modules_to_patch=(__name__, )) as t:

        assert time.time() == _t(0)
        time.sleep(200)
        assert time.time() == _t(200)

        t.clock.time = _t(6000)
        assert time.time() == _t(6000)
예제 #3
0
def test_select_timeout_occurring():
    with TimeTravel() as t:
        event = mock.MagicMock()

        t.events_pool.add_future_event(10, event, t.events_types.select.READ)

        assert select.select([event], [], [], 6) == ([], [], [])
        assert time.time() == 6
        assert datetime.today() == datetime.fromtimestamp(6)
예제 #4
0
def test_select_no_timeout():
    with TimeTravel() as t:
        event = mock.MagicMock()

        t.events_pool.add_future_event(2, event, t.events_types.select.WRITE)

        assert select.select([], [event], []) == ([], [event], [])
        assert time.time() == 2
        assert datetime.today() == datetime.fromtimestamp(2)
예제 #5
0
def test_patch_stop_afer_scope_end():
    with TimeTravel(modules_to_patch=__name__) as t:

        assert datetime_cls.now() == datetime_cls.fromtimestamp(_t(0))
        t.clock.time = _t(1000)
        assert datetime_cls.today() == datetime_cls.fromtimestamp(_t(1000))

    assert time.time() != _t(1000)
    assert datetime_cls.today() != datetime_cls.fromtimestamp(_t(1000))
예제 #6
0
def test_sleep_patch_sleep():
    with TimeTravel() as t:

        assert time.time() == 0
        time.sleep(3600)
        assert time.time() == 3600

        t.clock.time = 7200
        assert time.time() == 7200
예제 #7
0
def test_select_with_timeout():
    with TimeTravel(modules_to_patch=__name__) as t:
        sock = socket.socket()

        t.add_future_event(2, sock, t.event_types.select.EXCEPTIONAL)

        now = t.clock.time
        assert select.select([], [], [sock], 6) == ([], [], [sock])
        assert time.time() == now + 2
        assert datetime_cls.today() == datetime_cls.fromtimestamp(now + 2)
예제 #8
0
def test_select_timeout_occurring():
    with TimeTravel(modules_to_patch=__name__) as t:
        sock = socket.socket()

        t.add_future_event(10, sock, t.event_types.select.READ)

        now = t.clock.time
        assert select.select([sock], [], [], 6) == ([], [], [])
        assert time.time() == now + 6
        assert datetime_cls.today() == datetime_cls.fromtimestamp(now + 6)
예제 #9
0
def test_epoll():
    with TimeTravel(modules_to_patch=__name__) as t:
        sock = socket.socket()
        t.add_future_event(2, sock, select.POLLIN)

        poll = select.epoll()
        poll.register(sock, select.POLLIN | select.POLLOUT)

        now = t.clock.time
        assert poll.poll() == [(sock, select.POLLIN)]
        assert time.time() == now + 2
예제 #10
0
def test_really_long_wait():
    with TimeTravel() as t:
        socket = mock.MagicMock()

        t.add_future_event(49 * 60, socket, t.event_types.select.READ)
        t.add_future_event(49 * 60 + 2, socket, t.event_types.select.WRITE)

        wait_and_respond(socket)

        from datetime import datetime
        socket.send.assert_called_once_with(
            str(datetime.fromtimestamp(t.clock.time)))
예제 #11
0
def test_time_patch_set_time():
    with TimeTravel(modules_to_patch=__name__) as t:

        assert time.time() == _t(0)
        t.clock.time += 360
        assert time.time() == _t(360)
예제 #12
0
def test_sleep_changing_datetime_now():
    with TimeTravel(modules_to_patch=__name__):
        assert datetime_cls.today() == datetime_cls.fromtimestamp(_t(0))
        time.sleep(150)
        assert datetime_cls.now() == datetime_cls.fromtimestamp(_t(150))
예제 #13
0
def test_no_renaming_patching():
    with TimeTravel(modules_to_patch=__name__) as t:

        assert datetime.today() == datetime_cls.fromtimestamp(_t(0))
        t.clock.time = _t(1000)
        assert datetime.today() == datetime_cls.fromtimestamp(_t(1000))
예제 #14
0
def test_inner_importing_of_datetime():
    with TimeTravel(modules_to_patch=__name__):
        import datetime
        assert datetime.date.today() == datetime.date.fromtimestamp(_t(0))
예제 #15
0
def test_sleep_changing_today():
    with TimeTravel():
        assert datetime.today() == datetime.fromtimestamp(0)

        time.sleep(3600)
        assert datetime.today() == datetime.fromtimestamp(3600)
예제 #16
0
def test_patch_without_module_name():
    with TimeTravel() as t:

        assert datetime_cls.utcnow() == datetime_cls.fromtimestamp(_t(0))
        t.clock.time = _t(1000)
        assert datetime_cls.now() == datetime_cls.fromtimestamp(_t(1000))
예제 #17
0
def test_datetime_patch_set_time():
    with TimeTravel(modules_to_patch=[__name__]) as t:

        assert datetime_cls.today() == datetime_cls.fromtimestamp(_t(0))
        t.clock.time = _t(1000)
        assert datetime_cls.today() == datetime_cls.fromtimestamp(_t(1000))
예제 #18
0
def test_time_patch_set_time():
    with TimeTravel() as t:

        assert time.time() == 0
        t.clock.time = 3600
        assert time.time() == 3600
예제 #19
0
def test_datetime_patch_set_time():
    with TimeTravel() as t:

        assert datetime.today() == datetime.fromtimestamp(0)
        t.clock.time = 3600
        assert datetime.today() == datetime.fromtimestamp(3600)