Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unknown duration unit when using Sleep::for() with integers/floats #49887

Closed
gabidj opened this issue Jan 29, 2024 · 4 comments
Closed

Unknown duration unit when using Sleep::for() with integers/floats #49887

gabidj opened this issue Jan 29, 2024 · 4 comments

Comments

@gabidj
Copy link

gabidj commented Jan 29, 2024

Laravel Version

10.40.0

PHP Version

8.2

Database Driver & Version

No DB, macOS 14.3 (23D56) with Herd 1.0.0

Description

As per Sleep class in Laravel int and floats are accepted, but actually sending integers or floats will trigger an error.

Sleep::for(); accepts \DateInterval|int|float $duration // according to phpdoc
which calls the Sleep constructor, which also accepts int|float|\DateInterval $duration // according to phpdoc
which calls Sleep::duration(), which also accepts \DateInterval|int|float $duration // according to phpdoc

All functions will execute successfully however when Sleep::__destruct() is called the exception is thrown due to $this->pending not being a null value.

    public function __destruct()
    {
        if (! $this->shouldSleep) {
            return;
        }

        if ($this->pending !== null) {
            throw new RuntimeException('Unknown duration unit.');
        }
// ...

Is this an expected behaviour keeping possible concurrency in mind?
Am I missing something?

Steps To Reproduce

Write a new unit test, and call Sleep::for($x).
The variable $x must be integer or a float.

The following exception
• Tests\Feature\SleepTest > pretendSleep
PHPUnit\Framework\ExceptionWrapper

Unknown duration unit.

The same can be achieved by running the snippet below

use Illuminate\Support\Sleep;

Sleep::for($x);

In this case a \RuntimeException will be thrown with the same message "Unknown duration unit."

@gabidj
Copy link
Author

gabidj commented Jan 29, 2024

If using method Sleep::fake() or Sleep::fake(false) they all work, however the DateInterval works even if the Sleep::fake method is not called before.

@driesvints
Copy link
Member

Heya, thanks for reporting.

We'll need more info and/or code to debug this further. Can you please create a repository with the command below, commit the code that reproduces the issue as one separate commit on the main/master branch and share the repository here? Please make sure that you have the latest version of the Laravel installer in order to run this command. Please also make sure you have both Git & the GitHub CLI tool properly set up.

laravel new bug-report --github="--public"

Please do not amend and create a separate commit with your custom changes. After you've posted the repository, we'll try to reproduce the issue.

Thanks!

@timacdonald
Copy link
Member

timacdonald commented Jan 29, 2024

I am unable to reproduce this exception.

I created a fresh laravel application and added the following command to the routes/console.php file.

Artisan::command('sleep', function () {
    \Illuminate\Support\Sleep::for(CarbonInterval::seconds(1));
    \Illuminate\Support\Sleep::for(1.5)->seconds();
    \Illuminate\Support\Sleep::for(1.5)->milliseconds();
});

The artisan command runs as expected and no exception is not thrown.

If the exception being thrown has the message "Unknown duration unit.", this indicates that you did not specify the duration's units.

e.g. Sleep::for(1.5) is not valid usage of the Sleep class. You need to specify the units when passing a scalar, e.g., Sleep::for(1.5)->seconds().

See the docs for usage examples: https://laravel.com/docs/10.x/helpers#sleep

@gabidj
Copy link
Author

gabidj commented Feb 5, 2024

You are right. I did not add the scalar, I thought if none is set a default one will be assumed (seconds or u-seconds).

Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants