|
| 1 | +package frankenphp_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "io" |
| 6 | + "net/http/httptest" |
| 7 | + "os" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/dunglas/frankenphp" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | +) |
| 14 | + |
| 15 | +// TestBackgroundWorkerPool declares a named bg worker with num=3 (pool |
| 16 | +// of three threads). All three threads should boot, share the same |
| 17 | +// registered backgroundWorkerState, and the reader can see the pool's |
| 18 | +// vars. This covers the lifted num>1 + max_threads>1 constraint. |
| 19 | +func TestBackgroundWorkerPool(t *testing.T) { |
| 20 | + cwd, _ := os.Getwd() |
| 21 | + testDataDir := cwd + "/testdata/" |
| 22 | + |
| 23 | + require.NoError(t, frankenphp.Init( |
| 24 | + frankenphp.WithWorkers("pool-worker", testDataDir+"background-worker-pool.php", 3, |
| 25 | + frankenphp.WithWorkerBackground(), |
| 26 | + frankenphp.WithWorkerMaxThreads(3)), |
| 27 | + frankenphp.WithNumThreads(6), |
| 28 | + )) |
| 29 | + t.Cleanup(frankenphp.Shutdown) |
| 30 | + |
| 31 | + // Read the pool worker's vars via get_vars; all three threads share |
| 32 | + // the same state so we don't need to target a specific one. ensure() |
| 33 | + // waits for at least one pool thread's first set_vars so the eager |
| 34 | + // start can't race the reader. |
| 35 | + php := `<?php |
| 36 | +frankenphp_ensure_background_worker('pool-worker'); |
| 37 | +$vars = frankenphp_get_vars('pool-worker'); |
| 38 | +echo 'name=', $vars['name'] ?? 'MISSING', "\n"; |
| 39 | +echo 'has-pid=', isset($vars['pid']) ? '1' : '0', "\n"; |
| 40 | +` |
| 41 | + tmp := testDataDir + "pool-reader.php" |
| 42 | + require.NoError(t, os.WriteFile(tmp, []byte(php), 0644)) |
| 43 | + t.Cleanup(func() { _ = os.Remove(tmp) }) |
| 44 | + |
| 45 | + req := httptest.NewRequest("GET", "http://example.com/pool-reader.php", nil) |
| 46 | + fr, err := frankenphp.NewRequestWithContext(req, frankenphp.WithRequestDocumentRoot(testDataDir, false)) |
| 47 | + require.NoError(t, err) |
| 48 | + |
| 49 | + w := httptest.NewRecorder() |
| 50 | + if err := frankenphp.ServeHTTP(w, fr); err != nil && !errors.As(err, &frankenphp.ErrRejected{}) { |
| 51 | + t.Fatalf("serve: %v", err) |
| 52 | + } |
| 53 | + body, _ := io.ReadAll(w.Result().Body) |
| 54 | + out := string(body) |
| 55 | + |
| 56 | + assert.NotContains(t, out, "MISSING", "pool worker should have published its vars:\n"+out) |
| 57 | + assert.Contains(t, out, "name=pool-worker") |
| 58 | + assert.Contains(t, out, "has-pid=1") |
| 59 | +} |
| 60 | + |
| 61 | +// TestBackgroundWorkerMultiEntrypoint declares two named bg workers that |
| 62 | +// share the same entrypoint file. Each registry is independent, so ensure() |
| 63 | +// + get_vars resolve to the correct instance by name. |
| 64 | +func TestBackgroundWorkerMultiEntrypoint(t *testing.T) { |
| 65 | + cwd, _ := os.Getwd() |
| 66 | + testDataDir := cwd + "/testdata/" |
| 67 | + |
| 68 | + require.NoError(t, frankenphp.Init( |
| 69 | + frankenphp.WithWorkers("shared-a", testDataDir+"background-worker-named.php", 1, |
| 70 | + frankenphp.WithWorkerBackground()), |
| 71 | + frankenphp.WithWorkers("shared-b", testDataDir+"background-worker-named.php", 1, |
| 72 | + frankenphp.WithWorkerBackground()), |
| 73 | + frankenphp.WithNumThreads(5), |
| 74 | + )) |
| 75 | + t.Cleanup(frankenphp.Shutdown) |
| 76 | + |
| 77 | + read := func(name string) string { |
| 78 | + php := `<?php |
| 79 | +frankenphp_ensure_background_worker(` + "'" + name + "'" + `); |
| 80 | +$vars = frankenphp_get_vars(` + "'" + name + "'" + `); |
| 81 | +echo $vars['FRANKENPHP_WORKER_NAME'] ?? 'MISSING'; |
| 82 | +` |
| 83 | + tmp := testDataDir + "multi-entry-reader-" + name + ".php" |
| 84 | + require.NoError(t, os.WriteFile(tmp, []byte(php), 0644)) |
| 85 | + t.Cleanup(func() { _ = os.Remove(tmp) }) |
| 86 | + |
| 87 | + req := httptest.NewRequest("GET", "http://example.com/multi-entry-reader-"+name+".php", nil) |
| 88 | + fr, err := frankenphp.NewRequestWithContext(req, frankenphp.WithRequestDocumentRoot(testDataDir, false)) |
| 89 | + require.NoError(t, err) |
| 90 | + w := httptest.NewRecorder() |
| 91 | + _ = frankenphp.ServeHTTP(w, fr) |
| 92 | + body, _ := io.ReadAll(w.Result().Body) |
| 93 | + return string(body) |
| 94 | + } |
| 95 | + |
| 96 | + assert.Equal(t, "shared-a", read("shared-a"), "shared-a name must resolve to its own worker") |
| 97 | + assert.Equal(t, "shared-b", read("shared-b"), "shared-b name must resolve to its own worker") |
| 98 | +} |
0 commit comments