|
| 1 | +package frankenphp_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/dunglas/frankenphp" |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | +) |
| 13 | + |
| 14 | +// countSentinels returns the number of files under dir. |
| 15 | +func countSentinels(t *testing.T, dir string) int { |
| 16 | + t.Helper() |
| 17 | + entries, err := os.ReadDir(dir) |
| 18 | + if err != nil { |
| 19 | + t.Fatalf("read sentinel dir %q: %v", dir, err) |
| 20 | + } |
| 21 | + return len(entries) |
| 22 | +} |
| 23 | + |
| 24 | +// TestBackgroundWorkerPool declares a named bg worker with num=3 (pool of |
| 25 | +// three threads). Each thread touches a unique sentinel under |
| 26 | +// BG_SENTINEL_DIR via tempnam(), so the test can assert that all three |
| 27 | +// pool threads booted independently. Covers the lifted num>1 + |
| 28 | +// max_threads>1 constraints and the per-thread stop pipe. |
| 29 | +func TestBackgroundWorkerPool(t *testing.T) { |
| 30 | + cwd, _ := os.Getwd() |
| 31 | + testDataDir := cwd + "/testdata/" |
| 32 | + |
| 33 | + tmp := t.TempDir() |
| 34 | + |
| 35 | + require.NoError(t, frankenphp.Init( |
| 36 | + frankenphp.WithWorkers("pool-worker", testDataDir+"background-worker-pool.php", 3, |
| 37 | + frankenphp.WithWorkerBackground(), |
| 38 | + frankenphp.WithWorkerMaxThreads(3), |
| 39 | + frankenphp.WithWorkerEnv(map[string]string{"BG_SENTINEL_DIR": tmp}), |
| 40 | + ), |
| 41 | + frankenphp.WithNumThreads(6), |
| 42 | + )) |
| 43 | + t.Cleanup(frankenphp.Shutdown) |
| 44 | + |
| 45 | + require.Eventually(t, func() bool { |
| 46 | + return countSentinels(t, tmp) == 3 |
| 47 | + }, 5*time.Second, 25*time.Millisecond, |
| 48 | + "expected 3 distinct pool sentinels, got %d", countSentinels(t, tmp)) |
| 49 | +} |
| 50 | + |
| 51 | +// TestBackgroundWorkerMultiEntrypoint declares two named bg workers that |
| 52 | +// share the same entrypoint file. Each gets its own registry entry, so |
| 53 | +// both Init successfully (no filename-collision rejection) and both |
| 54 | +// produce sentinels. |
| 55 | +func TestBackgroundWorkerMultiEntrypoint(t *testing.T) { |
| 56 | + cwd, _ := os.Getwd() |
| 57 | + testDataDir := cwd + "/testdata/" |
| 58 | + |
| 59 | + tmp := t.TempDir() |
| 60 | + |
| 61 | + require.NoError(t, frankenphp.Init( |
| 62 | + frankenphp.WithWorkers("shared-a", testDataDir+"background-worker-named.php", 1, |
| 63 | + frankenphp.WithWorkerBackground(), |
| 64 | + frankenphp.WithWorkerEnv(map[string]string{"BG_SENTINEL_DIR": tmp}), |
| 65 | + ), |
| 66 | + frankenphp.WithWorkers("shared-b", testDataDir+"background-worker-named.php", 1, |
| 67 | + frankenphp.WithWorkerBackground(), |
| 68 | + frankenphp.WithWorkerEnv(map[string]string{"BG_SENTINEL_DIR": tmp}), |
| 69 | + ), |
| 70 | + frankenphp.WithNumThreads(5), |
| 71 | + )) |
| 72 | + t.Cleanup(frankenphp.Shutdown) |
| 73 | + |
| 74 | + for _, name := range []string{"shared-a", "shared-b"} { |
| 75 | + assert.Eventually(t, func() bool { |
| 76 | + _, err := os.Stat(filepath.Join(tmp, name)) |
| 77 | + return err == nil |
| 78 | + }, 5*time.Second, 25*time.Millisecond, |
| 79 | + "shared bg worker %q did not touch its sentinel", name) |
| 80 | + } |
| 81 | +} |
0 commit comments