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