laravel / octane

Supercharge your Laravel application's performance.
https://laravel.com/docs/octane
MIT License
3.78k stars 296 forks source link

SwooleHttpTaskDispatcher and Http::preventStrayRequests() #879

Closed InfluxOW closed 7 months ago

InfluxOW commented 7 months ago

Octane Version

2.3.8

Laravel Version

11.5.0

PHP Version

8.3

What server type are you using?

Swoole

Server Version

Laravel Sail

Database Driver & Version

No response

Description

I'm not sure if it's more of a Laravel or Laravel Octane issue, but I decided to start here. Using Http::preventStrayRequests() and Octane::concurrently(<tasks>) in a test leads to RuntimeException: Attempted request to [http://0.0.0.0/octane/resolve-tasks] without a matching fake. I guess, Octane-related routes should be excluded from routes with matching fakes or you should have an option to allow performing real requests on some routes. Currently, I have to set Http::allowStrayRequests() in a tests with actions that uses Octane::concurrently(<tasks>) under the hood.

Steps To Reproduce

Use Laravel Sail.

composer require laravel/octane

.env

OCTANE_SERVER=swoole

test

    public function testItThrowsAnError(): void
    {
        \Illuminate\Support\Facades\Http::preventStrayRequests();
        \Laravel\Octane\Facades\Octane::concurrently([fn (): int => 1]); // RuntimeException: Attempted request to [http://0.0.0.0/octane/resolve-tasks] without a matching fake.
    }
driesvints commented 7 months ago

I'm not sure what you wish to achieve here? You're preventing all stray requests and then invoking concurrently. That won't ever work since concurrently dispatches requests. These two cannot be combined unless you instruct the Http::fake to mock the /octane/resolve-tasks request.

InfluxOW commented 7 months ago

@driesvints

I understand that concurrently dispatches requests. But it dispatches requests within the same app. It doesn't make any third-party requests.

Doesn't it seem logical that this example should work?

        \Illuminate\Support\Facades\Http::preventStrayRequests();
        \Laravel\Octane\Facades\Octane::concurrently([fn (): int => 1]);

And this one shouldn't?

        \Illuminate\Support\Facades\Http::preventStrayRequests();
        \Laravel\Octane\Facades\Octane::concurrently([fn () => \Illuminate\Support\Facades\Http::get('example.com'))]);

For me, it seams reasonable to fake only example.com response. But additional layer of faking /octane/resolve-tasks seems overwhelming.

driesvints commented 7 months ago

No because Swoole's HTTP task dispatcher doesn't operates on the same app url. It dispatches either from a custom host or 127.0.0.1 on its specific port.