|
| 1 | +package frankenphp_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "io" |
| 6 | + "net/http/httptest" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "strings" |
| 10 | + "testing" |
| 11 | + "time" |
| 12 | + |
| 13 | + "github.com/dunglas/frankenphp" |
| 14 | + "github.com/stretchr/testify/assert" |
| 15 | + "github.com/stretchr/testify/require" |
| 16 | +) |
| 17 | + |
| 18 | +// waitForBatchSentinel polls for a file to exist before the deadline. |
| 19 | +func waitForBatchSentinel(t *testing.T, path string, within time.Duration) bool { |
| 20 | + t.Helper() |
| 21 | + deadline := time.Now().Add(within) |
| 22 | + for time.Now().Before(deadline) { |
| 23 | + if _, err := os.Stat(path); err == nil { |
| 24 | + return true |
| 25 | + } |
| 26 | + time.Sleep(10 * time.Millisecond) |
| 27 | + } |
| 28 | + return false |
| 29 | +} |
| 30 | + |
| 31 | +// serveTestRequest issues an HTTP request through frankenphp against the |
| 32 | +// given script under testdata/ and returns the response body. ErrRejected |
| 33 | +// is treated as a non-fatal outcome so worker-mode quirks don't fail tests |
| 34 | +// that only care about the script's stdout. |
| 35 | +func serveTestRequest(t *testing.T, testDataDir, script, query string) string { |
| 36 | + t.Helper() |
| 37 | + url := "http://example.com/" + script |
| 38 | + if query != "" { |
| 39 | + url += "?" + query |
| 40 | + } |
| 41 | + req := httptest.NewRequest("GET", url, nil) |
| 42 | + fr, err := frankenphp.NewRequestWithContext(req, frankenphp.WithRequestDocumentRoot(testDataDir, false)) |
| 43 | + require.NoError(t, err) |
| 44 | + |
| 45 | + w := httptest.NewRecorder() |
| 46 | + if err := frankenphp.ServeHTTP(w, fr); err != nil && !errors.As(err, &frankenphp.ErrRejected{}) { |
| 47 | + t.Fatalf("serve %s: %v", script, err) |
| 48 | + } |
| 49 | + body, _ := io.ReadAll(w.Result().Body) |
| 50 | + return string(body) |
| 51 | +} |
| 52 | + |
| 53 | +// TestEnsureBackgroundWorkerBatch declares a single catch-all bg worker |
| 54 | +// and ensures three distinct names from a single ensure() call. Each |
| 55 | +// catch-all instance touches a per-name sentinel; the test asserts that |
| 56 | +// all three appear, proving the array form started one worker per name. |
| 57 | +func TestEnsureBackgroundWorkerBatch(t *testing.T) { |
| 58 | + cwd, _ := os.Getwd() |
| 59 | + testDataDir := cwd + "/testdata/" |
| 60 | + tmp := t.TempDir() |
| 61 | + |
| 62 | + require.NoError(t, frankenphp.Init( |
| 63 | + frankenphp.WithWorkers("", testDataDir+"background-worker-named.php", 0, |
| 64 | + frankenphp.WithWorkerBackground(), |
| 65 | + frankenphp.WithWorkerMaxThreads(8), |
| 66 | + frankenphp.WithWorkerEnv(map[string]string{"BG_SENTINEL_DIR": tmp}), |
| 67 | + ), |
| 68 | + frankenphp.WithNumThreads(8), |
| 69 | + )) |
| 70 | + t.Cleanup(frankenphp.Shutdown) |
| 71 | + |
| 72 | + body := serveTestRequest(t, testDataDir, "background-worker-batch-ensure.php", "") |
| 73 | + assert.Contains(t, body, "ok", "batch ensure script should echo ok, got: %q", body) |
| 74 | + |
| 75 | + for _, name := range []string{"batch-a", "batch-b", "batch-c"} { |
| 76 | + assert.True(t, |
| 77 | + waitForBatchSentinel(t, filepath.Join(tmp, name), 5*time.Second), |
| 78 | + "catch-all instance %q should have written its sentinel", name) |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +// TestEnsureBackgroundWorkerBatchEmpty exercises the C-side validation |
| 83 | +// that an empty array raises a ValueError before any worker is started. |
| 84 | +// The fixture catches the throwable and echoes its class. |
| 85 | +func TestEnsureBackgroundWorkerBatchEmpty(t *testing.T) { |
| 86 | + cwd, _ := os.Getwd() |
| 87 | + testDataDir := cwd + "/testdata/" |
| 88 | + |
| 89 | + require.NoError(t, frankenphp.Init( |
| 90 | + frankenphp.WithWorkers("", testDataDir+"background-worker-named.php", 0, |
| 91 | + frankenphp.WithWorkerBackground(), |
| 92 | + ), |
| 93 | + frankenphp.WithNumThreads(2), |
| 94 | + )) |
| 95 | + t.Cleanup(frankenphp.Shutdown) |
| 96 | + |
| 97 | + body := serveTestRequest(t, testDataDir, "background-worker-batch-errors.php", "mode=empty") |
| 98 | + assert.True(t, strings.Contains(body, "ValueError"), |
| 99 | + "empty array should raise ValueError, got: %q", body) |
| 100 | + assert.Contains(t, body, "must not be empty") |
| 101 | +} |
| 102 | + |
| 103 | +// TestEnsureBackgroundWorkerBatchNonString verifies a non-string element |
| 104 | +// raises a TypeError (PHP's standard for argument-type mismatches inside |
| 105 | +// our parsed array). |
| 106 | +func TestEnsureBackgroundWorkerBatchNonString(t *testing.T) { |
| 107 | + cwd, _ := os.Getwd() |
| 108 | + testDataDir := cwd + "/testdata/" |
| 109 | + |
| 110 | + require.NoError(t, frankenphp.Init( |
| 111 | + frankenphp.WithWorkers("", testDataDir+"background-worker-named.php", 0, |
| 112 | + frankenphp.WithWorkerBackground(), |
| 113 | + ), |
| 114 | + frankenphp.WithNumThreads(2), |
| 115 | + )) |
| 116 | + t.Cleanup(frankenphp.Shutdown) |
| 117 | + |
| 118 | + body := serveTestRequest(t, testDataDir, "background-worker-batch-errors.php", "mode=nonstring") |
| 119 | + assert.True(t, strings.Contains(body, "TypeError"), |
| 120 | + "non-string element should raise TypeError, got: %q", body) |
| 121 | +} |
| 122 | + |
| 123 | +// TestEnsureBackgroundWorkerBatchDuplicate verifies that duplicate names |
| 124 | +// in the same batch are rejected as a ValueError, matching the e17577e |
| 125 | +// reference behavior (no silent dedup). |
| 126 | +func TestEnsureBackgroundWorkerBatchDuplicate(t *testing.T) { |
| 127 | + cwd, _ := os.Getwd() |
| 128 | + testDataDir := cwd + "/testdata/" |
| 129 | + |
| 130 | + require.NoError(t, frankenphp.Init( |
| 131 | + frankenphp.WithWorkers("", testDataDir+"background-worker-named.php", 0, |
| 132 | + frankenphp.WithWorkerBackground(), |
| 133 | + ), |
| 134 | + frankenphp.WithNumThreads(2), |
| 135 | + )) |
| 136 | + t.Cleanup(frankenphp.Shutdown) |
| 137 | + |
| 138 | + body := serveTestRequest(t, testDataDir, "background-worker-batch-errors.php", "mode=duplicate") |
| 139 | + assert.True(t, strings.Contains(body, "ValueError"), |
| 140 | + "duplicate name should raise ValueError, got: %q", body) |
| 141 | + assert.Contains(t, body, "duplicate") |
| 142 | +} |
| 143 | + |
| 144 | +// TestBackgroundWorkerBgFlag asserts that a bg worker script sees |
| 145 | +// $_SERVER['FRANKENPHP_WORKER_BACKGROUND'] === true. The fixture writes |
| 146 | +// var_export() of the value to a sentinel so the test can read the exact |
| 147 | +// PHP-level representation. |
| 148 | +func TestBackgroundWorkerBgFlag(t *testing.T) { |
| 149 | + cwd, _ := os.Getwd() |
| 150 | + testDataDir := cwd + "/testdata/" |
| 151 | + |
| 152 | + tmp := t.TempDir() |
| 153 | + sentinel := filepath.Join(tmp, "bg-flag.sentinel") |
| 154 | + |
| 155 | + require.NoError(t, frankenphp.Init( |
| 156 | + frankenphp.WithWorkers("bg-flag", testDataDir+"background-worker-bg-flag.php", 1, |
| 157 | + frankenphp.WithWorkerBackground(), |
| 158 | + frankenphp.WithWorkerEnv(map[string]string{"BG_SENTINEL": sentinel}), |
| 159 | + ), |
| 160 | + frankenphp.WithNumThreads(2), |
| 161 | + )) |
| 162 | + t.Cleanup(frankenphp.Shutdown) |
| 163 | + |
| 164 | + require.True(t, waitForBatchSentinel(t, sentinel, 5*time.Second), |
| 165 | + "bg worker should have written the FRANKENPHP_WORKER_BACKGROUND sentinel") |
| 166 | + |
| 167 | + contents, err := os.ReadFile(sentinel) |
| 168 | + require.NoError(t, err) |
| 169 | + assert.Equal(t, "true", string(contents), |
| 170 | + "$_SERVER['FRANKENPHP_WORKER_BACKGROUND'] should be the bool true") |
| 171 | +} |
0 commit comments