-
Notifications
You must be signed in to change notification settings - Fork 473
Expand file tree
/
Copy pathbgworkerscope_test.go
More file actions
89 lines (79 loc) 路 3.17 KB
/
Copy pathbgworkerscope_test.go
File metadata and controls
89 lines (79 loc) 路 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package frankenphp
import (
"os"
"path/filepath"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// setupBgWorker boots FrankenPHP with the given options (internal-package
// variant), registers Shutdown as a t.Cleanup, and returns the absolute
// path to the testdata directory.
func setupBgWorker(t *testing.T, opts ...Option) (testDataDir string) {
t.Helper()
cwd, err := os.Getwd()
require.NoError(t, err)
testDataDir = cwd + "/testdata/"
require.NoError(t, Init(opts...))
t.Cleanup(Shutdown)
return
}
// requireSentinelEventually asserts that `path` appears on disk before the
// deadline. Wraps require.Eventually so call sites stay short.
func requireSentinelEventually(t testing.TB, path string, msgAndArgs ...any) {
t.Helper()
require.Eventually(t, func() bool {
_, err := os.Stat(path)
return err == nil
}, 5*time.Second, 10*time.Millisecond, msgAndArgs...)
}
// TestNextScopeIsDistinct verifies the scope counter
// hands out unique values on consecutive calls.
func TestNextScopeIsDistinct(t *testing.T) {
a := NextScope()
b := NextScope()
assert.NotEqual(t, a, b, "consecutive scopes must differ")
assert.NotZero(t, a, "scopes must be non-zero (zero is the global scope)")
assert.NotZero(t, b, "scopes must be non-zero (zero is the global scope)")
}
// TestBackgroundWorkerSameNameDifferentScope declares two named bg
// workers with the same user-facing name in distinct scopes. Both must
// Init successfully (the global workersByName collision check must
// recognize bg workers as scope-isolated), each must produce its own
// sentinel under its scope-specific directory.
func TestBackgroundWorkerSameNameDifferentScope(t *testing.T) {
scopeA := NextScope()
scopeB := NextScope()
tmp := t.TempDir()
dirA := filepath.Join(tmp, "a")
dirB := filepath.Join(tmp, "b")
require.NoError(t, os.MkdirAll(dirA, 0o755))
require.NoError(t, os.MkdirAll(dirB, 0o755))
setupBgWorker(t,
WithWorkers("shared", "testdata/bgworker/named.php", 1,
WithWorkerBackground(),
WithWorkerScope(scopeA),
WithWorkerEnv(map[string]string{"BG_SENTINEL_DIR": dirA}),
),
WithWorkers("shared", "testdata/bgworker/named.php", 1,
WithWorkerBackground(),
WithWorkerScope(scopeB),
WithWorkerEnv(map[string]string{"BG_SENTINEL_DIR": dirB}),
),
WithNumThreads(4),
)
// Both lookups must exist and resolve "shared" to a *worker.
require.NotNil(t, backgroundLookups[scopeA], "scope A lookup missing")
require.NotNil(t, backgroundLookups[scopeB], "scope B lookup missing")
assert.NotNil(t, backgroundLookups[scopeA]["shared"], "scope A must resolve 'shared'")
assert.NotNil(t, backgroundLookups[scopeB]["shared"], "scope B must resolve 'shared'")
// And they must be distinct *worker instances (not the same pointer).
assert.NotSame(t,
backgroundLookups[scopeA]["shared"],
backgroundLookups[scopeB]["shared"],
"each scope must own a distinct *worker for the same name")
// Each scope's worker writes its sentinel under its own dir.
requireSentinelEventually(t, filepath.Join(dirA, "shared"), "scope A worker did not touch sentinel")
requireSentinelEventually(t, filepath.Join(dirB, "shared"), "scope B worker did not touch sentinel")
}