Skip to content

Commit

Permalink
Fix sleep tests (#602)
Browse files Browse the repository at this point in the history
* Refactor usleep function call to use value function in retry function

* Refactor Sleep class to add syncWithCarbon property

---------

Co-authored-by: Deeka Wong <[email protected]>
  • Loading branch information
huangdijia and huangdijia committed Apr 7, 2024
1 parent c61b05f commit 90dee96
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/Functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ function retry($times, callable $callback, $sleepMilliseconds = 0, $when = null)
$sleepMilliseconds = $backoff[$attempts - 1] ?? $sleepMilliseconds;

if ($sleepMilliseconds) {
Sleep::usleep(value($sleepMilliseconds, $attempts, $e) * 1000);
usleep(value($sleepMilliseconds, $attempts, $e) * 1000);
}

goto beginning;
Expand Down
45 changes: 34 additions & 11 deletions src/Sleep.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,21 @@ class Sleep
/**
* The total duration to sleep.
*
* @var CarbonInterval
* @var \Carbon\CarbonInterval
*/
public $duration;

/**
* Keep Carbon's "now" in sync when sleeping.
*
* @var bool
*/
protected static $syncWithCarbon = false;

/**
* The pending duration to sleep.
*
* @var float|int|null
* @var int|float|null
*/
protected $pending;

Expand Down Expand Up @@ -73,7 +80,7 @@ class Sleep
/**
* Create a new class instance.
*
* @param DateInterval|float|int $duration
* @param int|float|DateInterval $duration
*/
public function __construct($duration)
{
Expand All @@ -96,6 +103,10 @@ public function __destruct()
if (static::$fake) {
static::$sequence[] = $this->duration;

if (static::$syncWithCarbon) {
Carbon::setTestNow(Carbon::now()->add($this->duration));
}

foreach (static::$fakeSleepCallbacks as $callback) {
$callback($this->duration);
}
Expand Down Expand Up @@ -123,7 +134,7 @@ public function __destruct()
/**
* Sleep for the given duration.
*
* @param DateInterval|float|int $duration
* @param DateInterval|int|float $duration
* @return static
*/
public static function for($duration)
Expand All @@ -140,7 +151,7 @@ public static function for($duration)
public static function until($timestamp)
{
if (is_numeric($timestamp)) {
$timestamp = Carbon::createFromTimestamp($timestamp);
$timestamp = Carbon::createFromTimestamp($timestamp, date_default_timezone_get());
}

return new static(Carbon::now()->diff($timestamp));
Expand All @@ -160,7 +171,7 @@ public static function usleep($duration)
/**
* Sleep for the given number of seconds.
*
* @param float|int $duration
* @param int|float $duration
* @return static
*/
public static function sleep($duration)
Expand Down Expand Up @@ -259,7 +270,7 @@ public function microsecond()
/**
* Add additional time to sleep for.
*
* @param float|int $duration
* @param int|float $duration
* @return $this
*/
public function and($duration)
Expand All @@ -273,13 +284,15 @@ public function and($duration)
* Stay awake and capture any attempts to sleep.
*
* @param bool $value
* @param bool $syncWithCarbon
*/
public static function fake($value = true)
public static function fake($value = true, $syncWithCarbon = false)
{
static::$fake = $value;

static::$sequence = [];
static::$fakeSleepCallbacks = [];
static::$syncWithCarbon = $syncWithCarbon;
}

/**
Expand Down Expand Up @@ -359,7 +372,7 @@ public static function assertInsomniac()
}

foreach (static::$sequence as $duration) {
PHPUnit::assertSame(0, $duration->totalMicroseconds, vsprintf('Unexpected sleep duration of [%s] found.', [
PHPUnit::assertSame(0, (int) $duration->totalMicroseconds, vsprintf('Unexpected sleep duration of [%s] found.', [
$duration->cascade()->forHumans([
'options' => 0,
'minimumUnit' => 'microsecond',
Expand Down Expand Up @@ -404,10 +417,20 @@ public static function whenFakingSleep($callback)
static::$fakeSleepCallbacks[] = $callback;
}

/**
* Indicate that Carbon's "now" should be kept in sync when sleeping.
*
* @param mixed $value
*/
public static function syncWithCarbon($value = true)
{
static::$syncWithCarbon = $value;
}

/**
* Sleep for the given duration. Replaces any previously defined duration.
*
* @param DateInterval|float|int $duration
* @param DateInterval|int|float $duration
* @return $this
*/
protected function duration($duration)
Expand All @@ -433,7 +456,7 @@ protected function duration($duration)
/**
* Resolve the pending duration.
*
* @return float|int
* @return int|float
*/
protected function pullPending()
{
Expand Down

0 comments on commit 90dee96

Please sign in to comment.