forked from php/frankenphp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkerextension_test.go
More file actions
86 lines (69 loc) · 2.44 KB
/
workerextension_test.go
File metadata and controls
86 lines (69 loc) · 2.44 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
package frankenphp
import (
"io"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestWorkersExtension(t *testing.T) {
t.Cleanup(Shutdown)
readyWorkers := 0
shutdownWorkers := 0
serverStarts := 0
serverShutDowns := 0
externalWorkers, o := WithExtensionWorkers(
"extensionWorkers",
"testdata/worker.php",
1,
WithWorkerOnReady(func(id int) {
readyWorkers++
}),
WithWorkerOnShutdown(func(id int) {
serverShutDowns++
}),
WithWorkerOnServerStartup(func() {
serverStarts++
}),
WithWorkerOnServerShutdown(func() {
shutdownWorkers++
}),
)
require.NoError(t, Init(o))
t.Cleanup(func() {
Shutdown()
assert.Equal(t, 1, shutdownWorkers, "Worker shutdown hook should have been called")
assert.Equal(t, 1, serverShutDowns, "Server shutdown hook should have been called")
})
assert.Equal(t, readyWorkers, 1, "Worker thread should have called onReady()")
assert.Equal(t, serverStarts, 1, "Server start hook should have been called")
assert.Equal(t, externalWorkers.NumThreads(), 1, "NumThreads() should report 1 thread")
// Create a test request
req := httptest.NewRequest("GET", "https://example.com/test/?foo=bar", nil)
req.Header.Set("X-Test-Header", "test-value")
w := httptest.NewRecorder()
// Inject the request into the worker through the extension
err := externalWorkers.SendRequest(w, req)
assert.NoError(t, err, "Sending request should not produce an error")
resp := w.Result()
body, _ := io.ReadAll(resp.Body)
// The worker.php script should output information about the request
// We're just checking that we got a response, not the specific content
assert.NotEmpty(t, body, "Response body should not be empty")
assert.Contains(t, string(body), "Requests handled: 0", "Response body should contain request information")
}
func TestWorkerExtensionSendMessage(t *testing.T) {
externalWorker, o := WithExtensionWorkers("extensionWorkers", "testdata/message-worker.php", 1)
err := Init(o)
require.NoError(t, err)
t.Cleanup(Shutdown)
ret, err := externalWorker.SendMessage(t.Context(), "Hello Workers", nil)
require.NoError(t, err)
assert.Equal(t, "received message: Hello Workers", ret)
}
func TestErrorIf2WorkersHaveSameName(t *testing.T) {
_, o1 := WithExtensionWorkers("duplicateWorker", "testdata/worker.php", 1)
_, o2 := WithExtensionWorkers("duplicateWorker", "testdata/worker2.php", 1)
t.Cleanup(Shutdown)
require.Error(t, Init(o1, o2))
}