Skip to content

Add PHPProcessManager test for request queuing and instance reuse#3233

Open
JanJakes wants to merge 5 commits intotrunkfrom
fix-bad-gateway-tests
Open

Add PHPProcessManager test for request queuing and instance reuse#3233
JanJakes wants to merge 5 commits intotrunkfrom
fix-bad-gateway-tests

Conversation

@JanJakes
Copy link
Member

@JanJakes JanJakes commented Feb 2, 2026

Motivation for the change, related issues

This PR adds a regression test for the Bad Gateway fix implemented in #3219. The test ensures that the PHPProcessManager correctly queues requests when all PHP instances are busy and properly reuses instances once they become available.

Implementation details

The test simulates 6 concurrent requests with only 2 PHP instances available, each request taking 50ms to complete. With proper queuing and instance reuse, all 6 requests should complete within the 200ms timeout (3 batches × 50ms = 150ms).

The test pre-boots both PHP instances before starting the concurrent requests to eliminate instance spawning time from the timing calculations.

Testing Instructions (or ideally a Blueprint)

CI should be green. The new test can be run using:

npx nx run php-wasm-node:test-group-4-jspi --testNamePattern="should correctly queue requests and reuse PHP instances"

Properly dispose PHP instances after each test to prevent memory
accumulation during test runs.
This test covers the Bad Gateway issue that was addressed in
#3219.
@JanJakes JanJakes requested a review from a team February 2, 2026 13:44
Copy link
Member

@ashfame ashfame left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good 👍

mgr = new PHPProcessManager({
phpFactory,
maxPhpInstances: 2,
timeout: 200,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be set more generously so that it realistically never hits due to scheduling delays in CI when under high load?

Additionally, adding the intent to timeout values (here in this test and in other tests) when they are not under test, might help in making the intent clearer.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be set more generously so that it realistically never hits due to scheduling delays in CI when under high load?

I think we can't do that, because that's what this is testing—previously, when the scheduling was incorrect, this could've taken a bit more time than strictly necessary. We need to ensure that we meet the timings quite precisely.

To do that in a better way, I added vi.useFakeTimers(), and even lowered the timeout further. I also fixed the docs a bit to explain that the timeout actually represents "max. time in the queue" rather than queue + request duration.

@ashfame ashfame requested a review from Copilot February 4, 2026 17:02
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a regression test to ensure PHPProcessManager queues concurrent requests when instances are exhausted and reuses instances as they’re released (related to the Bad Gateway fix in #3219).

Changes:

  • Introduces afterEach cleanup to dispose the process manager between tests.
  • Updates existing tests to reuse a shared mgr variable.
  • Adds a new concurrency/queuing test that simulates 6 concurrent “requests” with a max of 2 PHP instances.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Co-authored-by: Ashish Kumar <ashfame@users.noreply.github.com>
simulateRequest(),
]);

// All 6 requests should complete successfully within the 200ms timeout.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we check if it's 6x 200 response code?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@adamziel This is "requests" as in "acquirePHPInstance() and hold it for 50ms" requests. There is no HTTP response, not even a PHP code execution, it's only about scheduling—whether the timeout is met and whether there the correct number of PHP instances is acquired.

@JanJakes JanJakes requested a review from ashfame February 5, 2026 12:20
@JanJakes
Copy link
Member Author

JanJakes commented Feb 5, 2026

I addressed the PR feedback. It should be ready for a final check.

Copy link
Member

@ashfame ashfame left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tiny nitpicks, feel free to merge afterwards.

mgr = new PHPProcessManager({
phpFactory,
maxPhpInstances: 2,
timeout: 110,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add a note on why we are trying to add buffer to the needed 100ms timeout? I noticed test fails with 100 but passes with 101, so I guess we are just waiting for fake timer to do the next tick? Is that the reason you had in mind too or something else?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ashfame Yes, that's how it works, but I think it's explained right above this call, isn't it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wasn't apparent to me on why we need 10ms buffer. I thought 100ms should have been sufficient, which made me run the test myself with diff timeout values. But totally fine to not give too much mind to my comment.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ashfame Oh, OK, I'll add something like // 100ms for 4 requests + 10ms extra to start the remaining 2.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants