1Test::Mock::Time(3pm) User Contributed Perl DocumentationTest::Mock::Time(3pm)
2
3
4

NAME

6       Test::Mock::Time - Deterministic time & timers for event loop tests
7

VERSION

9       This document describes Test::Mock::Time version v0.1.7
10

SYNOPSIS

12         use Test::Mock::Time;
13
14         # All these functions will return same constant time
15         # until you manually move time forward by some deterministic
16         # value by sleep(), ff() or doing one tick of your event loop.
17         say time();
18         say localtime();
19         say gmtime();
20         say Time::HiRes::time();
21         say Time::HiRes::gettimeofday();
22         say Time::HiRes::clock_gettime(CLOCK_REALTIME());
23         say Time::HiRes::clock_gettime(CLOCK_MONOTONIC());
24
25         # All these functions will fast-forward time (so time() etc.
26         # will return increased value on next call) and return immediately.
27         # Pending timers of your event loop (if any) will not be processed.
28         sleep(1);
29         Time::HiRes::sleep(0.5);
30         Time::HiRes::usleep(500_000);
31         Time::HiRes::nanosleep(500_000_000);
32         Time::HiRes::clock_nanosleep(500_000_000);
33
34         # This will fast-forward time and process pending timers (if any).
35         ff(0.5);
36
37         # These will call ff() in case no other (usually I/O) event happens in
38         # $Test::Mock::Time::WAIT_ONE_TICK seconds of real time and there are
39         # some active timers.
40         Mojo::IOLoop->one_tick;
41         EV::run(EV::RUN_ONCE);
42

DESCRIPTION

44       This module replaces actual time with simulated time everywhere (core
45       time(), Time::HiRes, EV, AnyEvent with EV, Mojolicious, X) 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

EXPORTS

53       These functions are exported by default:
54
55           ff
56

INTERFACE

58   WAIT_ONE_TICK
59           $Test::Mock::Time::WAIT_ONE_TICK = 0.05;
60
61       This value is used to limit amount of real time spend waiting for non-
62       timer (usually I/O) event while one tick of event loop if there are
63       some active timers. In case no events happens while this time event
64       loop will be interrupted and time will be fast-forward to time when
65       next timer should expire by calling ff().
66
67   ff
68           ff( $seconds );
69           ff();
70
71       Fast-forward current time by $seconds (can be fractional). All
72       functions like time() will returns previous value increased by $seconds
73       after that.
74
75       Will run callbacks for pending timers of your event loop if they'll
76       expire while $seconds or if they've already expired (because you've
77       used functions like sleep() which fast-forward time without processing
78       timers).
79
80       When called without params will fast-forward time by amount needed to
81       run callback for next pending timer (it may be 0 in case there are no
82       pending timers or if next pending timer already expired).
83
84   Mocked functions/methods from other modules
85       See "SYNOPSIS" for explanation how they works.
86
87       CORE::GLOBAL
88           time
89           localtime
90           gmtime
91           sleep
92       Time::HiRes
93           time
94           gettimeofday
95           clock_gettime
96           clock_getres
97           sleep
98           usleep
99           nanosleep
100           clock_nanosleep
101       Mojo::Reactor::Poll
102           All required methods.
103
104       EV  All required methods except:
105
106               EV::once
107               EV::Watcher::feed_event
108

SUPPORT

110   Bugs / Feature Requests
111       Please report any bugs or feature requests through the issue tracker at
112       <https://github.com/powerman/perl-Test-Mock-Time/issues>.  You will be
113       notified automatically of any progress on your issue.
114
115   Source Code
116       This is open source software. The code repository is available for
117       public review and contribution under the terms of the license.  Feel
118       free to fork the repository and submit pull requests.
119
120       <https://github.com/powerman/perl-Test-Mock-Time>
121
122           git clone https://github.com/powerman/perl-Test-Mock-Time.git
123
124   Resources
125       ·   MetaCPAN Search
126
127           <https://metacpan.org/search?q=Test-Mock-Time>
128
129       ·   CPAN Ratings
130
131           <http://cpanratings.perl.org/dist/Test-Mock-Time>
132
133       ·   AnnoCPAN: Annotated CPAN documentation
134
135           <http://annocpan.org/dist/Test-Mock-Time>
136
137       ·   CPAN Testers Matrix
138
139           <http://matrix.cpantesters.org/?dist=Test-Mock-Time>
140
141       ·   CPANTS: A CPAN Testing Service (Kwalitee)
142
143           <http://cpants.cpanauthors.org/dist/Test-Mock-Time>
144

AUTHOR

146       Alex Efros <powerman@cpan.org>
147
149       This software is Copyright (c) 2016- by Alex Efros <powerman@cpan.org>.
150
151       This is free software, licensed under:
152
153         The MIT (X11) License
154
155
156
157perl v5.32.0                      2020-07-28             Test::Mock::Time(3pm)
Impressum