-
Notifications
You must be signed in to change notification settings - Fork 450
feat: background workers (config + ensure) #2393
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
Open
nicolas-grekas
wants to merge
1
commit into
php:main
Choose a base branch
from
nicolas-grekas:sidekicks-config
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| package frankenphp_test | ||
|
|
||
| import ( | ||
| "path/filepath" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/dunglas/frankenphp" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| // TestBackgroundWorkerLifecycle boots a background worker that touches a | ||
| // sentinel file then parks on the stop pipe. It proves the bg worker runs | ||
| // (sentinel appears) and that Shutdown returns within a reasonable time. | ||
| func TestBackgroundWorkerLifecycle(t *testing.T) { | ||
| tmp := t.TempDir() | ||
| sentinel := filepath.Join(tmp, "bg-lifecycle.sentinel") | ||
|
|
||
| require.NoError(t, frankenphp.Init( | ||
| frankenphp.WithWorkers("bg-lifecycle", "testdata/bgworker/basic.php", 1, | ||
| frankenphp.WithWorkerBackground(), | ||
| frankenphp.WithWorkerEnv(map[string]string{"BG_SENTINEL": sentinel}), | ||
| ), | ||
| frankenphp.WithNumThreads(2), | ||
| )) | ||
| // Note: this test asserts on Shutdown timing, so it manages Shutdown | ||
| // itself instead of using setupFrankenPHP's t.Cleanup hook. | ||
|
|
||
| requireFileEventually(t, sentinel, "background worker did not touch sentinel") | ||
|
|
||
| done := make(chan struct{}) | ||
| go func() { | ||
| frankenphp.Shutdown() | ||
| close(done) | ||
| }() | ||
|
|
||
| select { | ||
| case <-done: | ||
| case <-time.After(10 * time.Second): | ||
| t.Fatalf("Shutdown did not return within 10s") | ||
| } | ||
| } | ||
|
|
||
| // TestBackgroundWorkerCrashRestarts boots a worker that exit(1)s on its | ||
| // first run and touches a "restarted" sentinel on its second run. The | ||
| // sentinel proves the crash-restart loop fired. | ||
| func TestBackgroundWorkerCrashRestarts(t *testing.T) { | ||
| tmp := t.TempDir() | ||
| crashMarker := filepath.Join(tmp, "bg-crash.marker") | ||
| restarted := filepath.Join(tmp, "bg-crash.restarted") | ||
|
|
||
| setupFrankenPHP(t, | ||
| frankenphp.WithWorkers("bg-crash", "testdata/bgworker/crash.php", 1, | ||
| frankenphp.WithWorkerBackground(), | ||
| frankenphp.WithWorkerEnv(map[string]string{ | ||
| "BG_CRASH_MARKER": crashMarker, | ||
| "BG_RESTARTED_SENTINEL": restarted, | ||
| }), | ||
| ), | ||
| frankenphp.WithNumThreads(2), | ||
| ) | ||
|
|
||
| requireFileEventually(t, restarted, "background worker did not restart after crash") | ||
| } | ||
|
|
||
| // TestBackgroundWorkerWithoutHTTP confirms that a request to a script | ||
| // unrelated to the bg worker still works: the bg worker doesn't intercept | ||
| // HTTP traffic. | ||
| func TestBackgroundWorkerWithoutHTTP(t *testing.T) { | ||
| tmp := t.TempDir() | ||
| sentinel := filepath.Join(tmp, "bg-nohttp.sentinel") | ||
|
|
||
| testDataDir := setupFrankenPHP(t, | ||
| frankenphp.WithWorkers("bg-nohttp", "testdata/bgworker/basic.php", 1, | ||
| frankenphp.WithWorkerBackground(), | ||
| frankenphp.WithWorkerEnv(map[string]string{"BG_SENTINEL": sentinel}), | ||
| ), | ||
| frankenphp.WithNumThreads(2), | ||
| ) | ||
|
|
||
| requireFileEventually(t, sentinel, "background worker did not touch sentinel") | ||
|
|
||
| body := serveBody(t, testDataDir, "index.php") | ||
| assert.NotEmpty(t, body, "expected non-empty body from index.php") | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| package frankenphp_test | ||
|
|
||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
|
|
||
| "github.com/dunglas/frankenphp" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| // TestEnsureBackgroundWorkerBatch declares a single catch-all bg worker | ||
| // and ensures three distinct names from a single ensure() call. Each | ||
| // catch-all instance touches a per-name sentinel; the test asserts that | ||
| // all three appear, proving the array form started one worker per name. | ||
| func TestEnsureBackgroundWorkerBatch(t *testing.T) { | ||
| tmp := t.TempDir() | ||
| testDataDir := setupFrankenPHP(t, | ||
| frankenphp.WithWorkers("", "testdata/bgworker/named.php", 0, | ||
| frankenphp.WithWorkerBackground(), | ||
| frankenphp.WithWorkerMaxThreads(8), | ||
| frankenphp.WithWorkerEnv(map[string]string{"BG_SENTINEL_DIR": tmp}), | ||
| ), | ||
| frankenphp.WithNumThreads(8), | ||
| ) | ||
|
|
||
| body := serveBody(t, testDataDir, "bgworker/batch-ensure.php") | ||
| assert.Contains(t, body, "ok", "batch ensure script should echo ok, got: %q", body) | ||
|
|
||
| for _, name := range []string{"batch-a", "batch-b", "batch-c"} { | ||
| requireFileEventually(t, filepath.Join(tmp, name), | ||
| "catch-all instance %q should have written its sentinel", name) | ||
| } | ||
| } | ||
|
|
||
| // TestEnsureBackgroundWorkerBatchEmpty exercises the C-side validation | ||
| // that an empty array raises a ValueError before any worker is started. | ||
| // The fixture catches the throwable and echoes its class. | ||
| func TestEnsureBackgroundWorkerBatchEmpty(t *testing.T) { | ||
| testDataDir := setupFrankenPHP(t, | ||
| frankenphp.WithWorkers("", "testdata/bgworker/named.php", 0, | ||
| frankenphp.WithWorkerBackground(), | ||
| ), | ||
| frankenphp.WithNumThreads(2), | ||
| ) | ||
|
|
||
| body := serveBody(t, testDataDir, "bgworker/batch-errors.php?mode=empty") | ||
| assert.Contains(t, body, "ValueError", "empty array should raise ValueError, got: %q", body) | ||
| assert.Contains(t, body, "must not be empty") | ||
| } | ||
|
|
||
| // TestEnsureBackgroundWorkerBatchNonString verifies a non-string element | ||
| // raises a TypeError (PHP's standard for argument-type mismatches inside | ||
| // our parsed array). | ||
| func TestEnsureBackgroundWorkerBatchNonString(t *testing.T) { | ||
| testDataDir := setupFrankenPHP(t, | ||
| frankenphp.WithWorkers("", "testdata/bgworker/named.php", 0, | ||
| frankenphp.WithWorkerBackground(), | ||
| ), | ||
| frankenphp.WithNumThreads(2), | ||
| ) | ||
|
|
||
| body := serveBody(t, testDataDir, "bgworker/batch-errors.php?mode=nonstring") | ||
| assert.Contains(t, body, "TypeError", "non-string element should raise TypeError, got: %q", body) | ||
| } | ||
|
|
||
| // TestEnsureBackgroundWorkerBatchDuplicate verifies that duplicate names | ||
| // in the same batch are rejected as a ValueError, matching the e17577e | ||
| // reference behavior (no silent dedup). | ||
| func TestEnsureBackgroundWorkerBatchDuplicate(t *testing.T) { | ||
| testDataDir := setupFrankenPHP(t, | ||
| frankenphp.WithWorkers("", "testdata/bgworker/named.php", 0, | ||
| frankenphp.WithWorkerBackground(), | ||
| ), | ||
| frankenphp.WithNumThreads(2), | ||
| ) | ||
|
|
||
| body := serveBody(t, testDataDir, "bgworker/batch-errors.php?mode=duplicate") | ||
| assert.Contains(t, body, "ValueError", "duplicate name should raise ValueError, got: %q", body) | ||
| assert.Contains(t, body, "duplicate") | ||
| } | ||
|
|
||
| // TestBackgroundWorkerBgFlag asserts that a bg worker script sees | ||
| // $_SERVER['FRANKENPHP_WORKER_BACKGROUND'] === true. The fixture writes | ||
| // var_export() of the value to a sentinel so the test can read the exact | ||
| // PHP-level representation. | ||
| func TestBackgroundWorkerBgFlag(t *testing.T) { | ||
| tmp := t.TempDir() | ||
| sentinel := filepath.Join(tmp, "bg-flag.sentinel") | ||
|
|
||
| setupFrankenPHP(t, | ||
| frankenphp.WithWorkers("bg-flag", "testdata/bgworker/bg-flag.php", 1, | ||
| frankenphp.WithWorkerBackground(), | ||
| frankenphp.WithWorkerEnv(map[string]string{"BG_SENTINEL": sentinel}), | ||
| ), | ||
| frankenphp.WithNumThreads(2), | ||
| ) | ||
|
|
||
| requireFileEventually(t, sentinel, | ||
| "bg worker should have written the FRANKENPHP_WORKER_BACKGROUND sentinel") | ||
|
|
||
| contents, err := os.ReadFile(sentinel) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, "true", string(contents), | ||
| "$_SERVER['FRANKENPHP_WORKER_BACKGROUND'] should be the bool true") | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.