-
Notifications
You must be signed in to change notification settings - Fork 445
Expand file tree
/
Copy pathmaxrequests_regular_test.go
More file actions
69 lines (60 loc) · 2.07 KB
/
maxrequests_regular_test.go
File metadata and controls
69 lines (60 loc) · 2.07 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
package frankenphp_test
import (
"log/slog"
"net/http"
"net/http/httptest"
"strings"
"sync"
"testing"
"github.com/dunglas/frankenphp"
"github.com/stretchr/testify/assert"
)
// TestModuleMaxRequests verifies that regular (non-worker) PHP threads restart
// after reaching max_requests by checking debug logs for restart messages.
func TestModuleMaxRequests(t *testing.T) {
const maxRequests = 5
const totalRequests = 30
var buf syncBuffer
logger := slog.New(slog.NewTextHandler(&buf, &slog.HandlerOptions{Level: slog.LevelDebug}))
runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, _ int) {
for i := 0; i < totalRequests; i++ {
body, resp := testGet("http://example.com/index.php", handler, t)
assert.Equal(t, 200, resp.StatusCode)
assert.Contains(t, body, "I am by birth a Genevese")
}
restartCount := strings.Count(buf.String(), "max requests reached, restarting thread")
t.Logf("Thread restarts observed: %d", restartCount)
assert.GreaterOrEqual(t, restartCount, 2,
"with maxRequests=%d and %d requests on 2 threads, at least 2 restarts should occur", maxRequests, totalRequests)
}, &testOptions{
logger: logger,
initOpts: []frankenphp.Option{
frankenphp.WithNumThreads(2),
frankenphp.WithMaxRequests(maxRequests),
},
})
}
// TestModuleMaxRequestsConcurrent verifies max_requests works under concurrent load
// in module mode. All requests must succeed despite threads restarting.
func TestModuleMaxRequestsConcurrent(t *testing.T) {
const maxRequests = 10
const totalRequests = 200
runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, _ int) {
var wg sync.WaitGroup
for i := 0; i < totalRequests; i++ {
wg.Add(1)
go func() {
defer wg.Done()
body, resp := testGet("http://example.com/index.php", handler, t)
assert.Equal(t, 200, resp.StatusCode)
assert.Contains(t, body, "I am by birth a Genevese")
}()
}
wg.Wait()
}, &testOptions{
initOpts: []frankenphp.Option{
frankenphp.WithNumThreads(8),
frankenphp.WithMaxRequests(maxRequests),
},
})
}