You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I was wondering why this wasn't speeding up my unit tests, until I looked closer the list of overriden functions.
methodUsingSleep contains code that is using while loop to wait until specific time has passed or one other variable becomes true, I use sleep() to overcome max_execution_time, because in Linux environments (at least) sleep() is not counted as execution time. This allows that while loop to run longer than max_execution_time and wait that one variable to become 'true'.
I was trying to use timecop_scale() to speed up the test :)
The text was updated successfully, but these errors were encountered:
sleep() and usleep() is not affected because this is not really a time function. I would suggest you not to test sleep-aware methods. Move the code responsible for checking the conditions to different class/method or replace the sleep() call with a class and inject an interface to your service. This way you will be able to mock the Waiter to do nothing and speed up your test.
interface Waiter
{
publicfunctionwait(int$seconds);
}
class SleepWaiter implements Waiter
{
publicfunctionwait(int$seconds)
{
sleep($seconds);
}
}
class MyService
{
/** @var Waiter */protected$waiter;
protected$importantVariable = false;
publicfunction__construct(Waiter$waiter)
{
$this->waiter = $waiter;
}
publicfunctiondoSomething()
{
while (!$this->importantVariable) {
$this->waiter->wait(2);
}
echo'The variable has been set!';
}
}
PS. Use better names for these classes. This is just an example 😄
I have a function which uses
sleep(2)
I wrapped PHPUnit call in following way
I was wondering why this wasn't speeding up my unit tests, until I looked closer the list of overriden functions.
methodUsingSleep
contains code that is usingwhile
loop to wait until specific time has passed or one other variable becomes true, I usesleep()
to overcomemax_execution_time
, because in Linux environments (at least)sleep()
is not counted as execution time. This allows that while loop to run longer thanmax_execution_time
and wait that one variable to become 'true'.I was trying to use
timecop_scale()
to speed up the test :)The text was updated successfully, but these errors were encountered: