| 1 |
NAME |
| 2 |
|
| 3 |
Test::Mock::Time - Deterministic time & timers for event loop tests |
| 4 |
|
| 5 |
VERSION |
| 6 |
|
| 7 |
This document describes Test::Mock::Time version v0.2.1 |
| 8 |
|
| 9 |
SYNOPSIS |
| 10 |
|
| 11 |
use Test::Mock::Time; |
| 12 |
|
| 13 |
# All these functions will return same constant time |
| 14 |
# until you manually move time forward by some deterministic |
| 15 |
# value by sleep(), ff() or doing one tick of your event loop. |
| 16 |
say time(); |
| 17 |
say localtime(); |
| 18 |
say gmtime(); |
| 19 |
say Time::HiRes::time(); |
| 20 |
say Time::HiRes::gettimeofday(); |
| 21 |
say Time::HiRes::clock_gettime(CLOCK_REALTIME()); |
| 22 |
say Time::HiRes::clock_gettime(CLOCK_MONOTONIC()); |
| 23 |
|
| 24 |
# All these functions will fast-forward time (so time() etc. |
| 25 |
# will return increased value on next call) and return immediately. |
| 26 |
# Pending timers of your event loop (if any) will not be processed. |
| 27 |
sleep(1); |
| 28 |
Time::HiRes::sleep(0.5); |
| 29 |
Time::HiRes::usleep(500_000); |
| 30 |
Time::HiRes::nanosleep(500_000_000); |
| 31 |
Time::HiRes::clock_nanosleep(500_000_000); |
| 32 |
|
| 33 |
# This will fast-forward time and process pending timers (if any). |
| 34 |
ff(0.5); |
| 35 |
|
| 36 |
# These will call ff() in case no other (usually I/O) event happens in |
| 37 |
# $Test::Mock::Time::WAIT_ONE_TICK seconds of real time and there are |
| 38 |
# some active timers. |
| 39 |
Mojo::IOLoop->one_tick; |
| 40 |
EV::run(EV::RUN_ONCE); |
| 41 |
|
| 42 |
DESCRIPTION |
| 43 |
|
| 44 |
This module replaces actual time with simulated time everywhere (core |
| 45 |
time(), Time::HiRes, EV, AnyEvent with EV, Mojolicious, …) and provide |
| 46 |
a way to write deterministic tests for event loop based applications |
| 47 |
with timers. |
| 48 |
|
| 49 |
IMPORTANT! This module must be loaded by your script/app/test before |
| 50 |
other related modules (Time::HiRes, Mojolicious, EV, etc.). |
| 51 |
|
| 52 |
EXPORTS |
| 53 |
|
| 54 |
These functions are exported by default: |
| 55 |
|
| 56 |
ff |
| 57 |
|
| 58 |
INTERFACE |
| 59 |
|
| 60 |
WAIT_ONE_TICK |
| 61 |
|
| 62 |
$Test::Mock::Time::WAIT_ONE_TICK = 0.05; |
| 63 |
|
| 64 |
This value is used to limit amount of real time spend waiting for |
| 65 |
non-timer (usually I/O) event while one tick of event loop if there are |
| 66 |
some active timers. In case no events happens while this time event |
| 67 |
loop will be interrupted and time will be fast-forward to time when |
| 68 |
next timer should expire by calling ff(). |
| 69 |
|
| 70 |
ff |
| 71 |
|
| 72 |
ff( $seconds ); |
| 73 |
ff(); |
| 74 |
|
| 75 |
Fast-forward current time by $seconds (can be fractional). All |
| 76 |
functions like time() will returns previous value increased by $seconds |
| 77 |
after that. |
| 78 |
|
| 79 |
Will run callbacks for pending timers of your event loop if they'll |
| 80 |
expire while $seconds or if they've already expired (because you've |
| 81 |
used functions like sleep() which fast-forward time without processing |
| 82 |
timers). |
| 83 |
|
| 84 |
When called without params will fast-forward time by amount needed to |
| 85 |
run callback for next pending timer (it may be 0 in case there are no |
| 86 |
pending timers or if next pending timer already expired). |
| 87 |
|
| 88 |
Mocked functions/methods from other modules |
| 89 |
|
| 90 |
See "SYNOPSIS" for explanation how they works. |
| 91 |
|
| 92 |
CORE::GLOBAL |
| 93 |
|
| 94 |
time |
| 95 |
|
| 96 |
localtime |
| 97 |
|
| 98 |
gmtime |
| 99 |
|
| 100 |
sleep |
| 101 |
|
| 102 |
Time::HiRes |
| 103 |
|
| 104 |
time |
| 105 |
|
| 106 |
gettimeofday |
| 107 |
|
| 108 |
clock_gettime |
| 109 |
|
| 110 |
clock_getres |
| 111 |
|
| 112 |
sleep |
| 113 |
|
| 114 |
usleep |
| 115 |
|
| 116 |
nanosleep |
| 117 |
|
| 118 |
clock_nanosleep |
| 119 |
|
| 120 |
Mojo::Reactor::Poll |
| 121 |
|
| 122 |
All required methods. |
| 123 |
|
| 124 |
EV |
| 125 |
|
| 126 |
All required methods except: |
| 127 |
|
| 128 |
EV::once |
| 129 |
EV::Watcher::feed_event |
| 130 |
|
| 131 |
SUPPORT |
| 132 |
|
| 133 |
Bugs / Feature Requests |
| 134 |
|
| 135 |
Please report any bugs or feature requests through the issue tracker at |
| 136 |
https://github.com/powerman/perl-Test-Mock-Time/issues. You will be |
| 137 |
notified automatically of any progress on your issue. |
| 138 |
|
| 139 |
Source Code |
| 140 |
|
| 141 |
This is open source software. The code repository is available for |
| 142 |
public review and contribution under the terms of the license. Feel |
| 143 |
free to fork the repository and submit pull requests. |
| 144 |
|
| 145 |
https://github.com/powerman/perl-Test-Mock-Time |
| 146 |
|
| 147 |
git clone https://github.com/powerman/perl-Test-Mock-Time.git |
| 148 |
|
| 149 |
Resources |
| 150 |
|
| 151 |
* MetaCPAN Search |
| 152 |
|
| 153 |
https://metacpan.org/search?q=Test-Mock-Time |
| 154 |
|
| 155 |
* CPAN Ratings |
| 156 |
|
| 157 |
http://cpanratings.perl.org/dist/Test-Mock-Time |
| 158 |
|
| 159 |
* AnnoCPAN: Annotated CPAN documentation |
| 160 |
|
| 161 |
http://annocpan.org/dist/Test-Mock-Time |
| 162 |
|
| 163 |
* CPAN Testers Matrix |
| 164 |
|
| 165 |
http://matrix.cpantesters.org/?dist=Test-Mock-Time |
| 166 |
|
| 167 |
* CPANTS: A CPAN Testing Service (Kwalitee) |
| 168 |
|
| 169 |
http://cpants.cpanauthors.org/dist/Test-Mock-Time |
| 170 |
|
| 171 |
AUTHOR |
| 172 |
|
| 173 |
Alex Efros <[email protected]> |
| 174 |
|
| 175 |
COPYRIGHT AND LICENSE |
| 176 |
|
| 177 |
This software is Copyright (c) 2016- by Alex Efros <[email protected]>. |
| 178 |
|
| 179 |
This is free software, licensed under: |
| 180 |
|
| 181 |
The MIT (X11) License |
| 182 |
|