Skip to content

Commit 1bb67dc

Browse files
RyanCccchhvm-bot
authored andcommitted
Mock date/time functions in Isolation.php
Summary: Mock date/time functions (e.g. time() ) so they never return the same value when calling multiple times. By doing so, more flakey test cases can be found. Closes #4384 Reviewed By: @JoelMarcey Differential Revision: D1719095 Signature: t1:1719095:1418338273:e3867d429e3162f1110e96b1c16677da5872d8c0
1 parent 7cdb752 commit 1bb67dc

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

hphp/test/frameworks/Isolation.php

+85
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,91 @@ function tempnam_ISOLATION_WRAPPER() {
134134
fb_rename_function('tempnam', 'ORIG_tempnam');
135135
fb_rename_function('tempnam_ISOLATION_WRAPPER', 'tempnam');
136136

137+
function time_ISOLATION_WRAPPER() {
138+
static $time;
139+
$org_time = ORIG_time();
140+
if ($time === null) {
141+
$time = $org_time;
142+
return $time;
143+
}
144+
if ($time >= $org_time) {
145+
++$time;
146+
} else {
147+
$time = $org_time;
148+
}
149+
return $time;
150+
}
151+
fb_rename_function('time', 'ORIG_time');
152+
fb_rename_function('time_ISOLATION_WRAPPER', 'time');
153+
154+
function microtime_ISOLATION_WRAPPER(bool $get_as_float = false) {
155+
static $time;
156+
list($msec, $sec) = explode(" ", ORIG_microtime());
157+
if ($time === null) {
158+
$time = $sec;
159+
} else if ($time >= $sec){
160+
++$time;
161+
} else {
162+
$time = $sec;
163+
}
164+
if ($get_as_float)
165+
return ((float)$msec + (float)$time);
166+
else
167+
return "{$msec} {$time}";
168+
}
169+
fb_rename_function('microtime', 'ORIG_microtime');
170+
fb_rename_function('microtime_ISOLATION_WRAPPER', 'microtime');
171+
172+
function date_ISOLATION_WRAPPER(?string $format, ?int $timestamp = null) {
173+
$timestamp = ($timestamp === null ? time() : $timestamp);
174+
return ORIG_date($format, $timestamp);
175+
}
176+
fb_rename_function('date', 'ORIG_date');
177+
fb_rename_function('date_ISOLATION_WRAPPER', 'date');
178+
179+
function gmdate_ISOLATION_WRAPPER(?string $format, ?int $timestamp = null) {
180+
$timestamp = ($timestamp === null ? time() : $timestamp);
181+
return ORIG_gmdate($format, $timestamp);
182+
}
183+
fb_rename_function('gmdate', 'ORIG_gmdate');
184+
fb_rename_function('gmdate_ISOLATION_WRAPPER', 'gmdate');
185+
186+
function strftime_ISOLATION_WRAPPER(?string $format, ?int $timestamp = null) {
187+
$timestamp = ($timestamp === null ? time() : $timestamp);
188+
return ORIG_strftime($format, $timestamp);
189+
}
190+
fb_rename_function('strftime', 'ORIG_strftime');
191+
fb_rename_function('strftime_ISOLATION_WRAPPER', 'strftime');
192+
193+
function strtotime_ISOLATION_WRAPPER(?string $time, ?int $now = null) {
194+
$now = ($now === null ? time() : $now);
195+
return ORIG_strtotime($time, $now);
196+
}
197+
fb_rename_function('strtotime', 'ORIG_strtotime');
198+
fb_rename_function('strtotime_ISOLATION_WRAPPER', 'strtotime');
199+
200+
function idate_ISOLATION_WRAPPER(?string $format, ?int $timestamp = null) {
201+
$timestamp = ($timestamp === null ? time() : $timestamp);
202+
return ORIG_idate($format, $timestamp);
203+
}
204+
fb_rename_function('idate', 'ORIG_idate');
205+
fb_rename_function('idate_ISOLATION_WRAPPER', 'idate');
206+
207+
function getdate_ISOLATION_WRAPPER(?int $timestamp = null) {
208+
$timestamp = ($timestamp === null ? time() : $timestamp);
209+
return ORIG_getdate($timestamp);
210+
}
211+
fb_rename_function('getdate', 'ORIG_getdate');
212+
fb_rename_function('getdate_ISOLATION_WRAPPER', 'getdate');
213+
214+
function localtime_ISOLATION_WRAPPER(?int $timestamp = null, bool $is_associative = false) {
215+
$timestamp = ($timestamp === null ? time() : $timestamp);
216+
return ORIG_localtime($timestamp, $is_associative);
217+
}
218+
fb_rename_function('localtime', 'ORIG_localtime');
219+
fb_rename_function('localtime_ISOLATION_WRAPPER', 'localtime');
220+
221+
137222
//////////////////////
138223
// Files - Checking //
139224
//////////////////////

hphp/test/frameworks/frameworks.yaml

+7
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@
7171
# Assumes that calculating the current time in seconds twice will give
7272
# the same result. Not true if you're unlucky.
7373
- codeigniter/tests/codeigniter/helpers/date_helper_test.php
74+
# Flakey test case. Already made a PR
75+
# at https://github.com/bcit-ci/CodeIgniter/pull/3399.
76+
- codeigniter/tests/codeigniter/libraries/Session_test.php
7477
composer:
7578
url: https://github.com/composer/composer.git
7679
# master is the only available branch
@@ -523,6 +526,10 @@
523526
- phpbb3/tests/lock/flock_test.php
524527
# race condition - https://github.com/phpbb/phpbb/pull/3006
525528
- phpbb3/tests/wrapper/gmgetdate_test.php
529+
# This flakey test is caused by assuming time functions always return the
530+
# same value. Made PR https://github.com/phpbb/phpbb/pull/3222 to fix.
531+
- phpbb3/tests/passwords/manager_test.php
532+
- phpbb3/tests/random/gen_rand_string_test.php
526533
phpmyadmin:
527534
url: https://github.com/phpmyadmin/phpmyadmin.git
528535
# Stable is RELEASE_4_1_9 but we have many pull requests in master

0 commit comments

Comments
 (0)