|
| 1 | +package frankenphp_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "io" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "strings" |
| 8 | + "sync" |
| 9 | + "testing" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/dunglas/frankenphp" |
| 13 | + "github.com/stretchr/testify/assert" |
| 14 | + "github.com/stretchr/testify/require" |
| 15 | +) |
| 16 | + |
| 17 | +// TestModuleMaxRequests verifies that regular (non-worker) PHP threads restart |
| 18 | +// after reaching max_requests. This is the module-mode equivalent of php-fpm's |
| 19 | +// pm.max_requests, cleaning up all ZTS state including leaky extensions. |
| 20 | +func TestModuleMaxRequests(t *testing.T) { |
| 21 | + const maxRequests = 5 |
| 22 | + const totalRequests = 30 |
| 23 | + |
| 24 | + runTest(t, func(_ func(http.ResponseWriter, *http.Request), ts *httptest.Server, _ int) { |
| 25 | + require.NotNil(t, ts) |
| 26 | + client := &http.Client{Timeout: 5 * time.Second} |
| 27 | + |
| 28 | + for i := 0; i < totalRequests; i++ { |
| 29 | + resp, err := client.Get(ts.URL + "/index.php") |
| 30 | + require.NoError(t, err, "request %d should succeed", i) |
| 31 | + |
| 32 | + body, err := io.ReadAll(resp.Body) |
| 33 | + require.NoError(t, err) |
| 34 | + _ = resp.Body.Close() |
| 35 | + |
| 36 | + assert.Equal(t, 200, resp.StatusCode, "request %d should return 200, got body: %s", i, string(body)) |
| 37 | + assert.Contains(t, string(body), "I am by birth a Genevese", |
| 38 | + "request %d should return correct body", i) |
| 39 | + } |
| 40 | + }, &testOptions{ |
| 41 | + realServer: true, |
| 42 | + initOpts: []frankenphp.Option{ |
| 43 | + frankenphp.WithNumThreads(2), |
| 44 | + frankenphp.WithMaxRequests(maxRequests), |
| 45 | + }, |
| 46 | + }) |
| 47 | +} |
| 48 | + |
| 49 | +// TestModuleMaxRequestsConcurrent verifies max_requests works under concurrent load |
| 50 | +// in module mode. All requests must succeed despite threads restarting. |
| 51 | +func TestModuleMaxRequestsConcurrent(t *testing.T) { |
| 52 | + const maxRequests = 10 |
| 53 | + const totalRequests = 200 |
| 54 | + const concurrency = 20 |
| 55 | + |
| 56 | + runTest(t, func(_ func(http.ResponseWriter, *http.Request), ts *httptest.Server, _ int) { |
| 57 | + require.NotNil(t, ts) |
| 58 | + client := &http.Client{Timeout: 10 * time.Second} |
| 59 | + |
| 60 | + var successCount int |
| 61 | + var mu sync.Mutex |
| 62 | + sem := make(chan struct{}, concurrency) |
| 63 | + var wg sync.WaitGroup |
| 64 | + |
| 65 | + for i := 0; i < totalRequests; i++ { |
| 66 | + wg.Add(1) |
| 67 | + sem <- struct{}{} |
| 68 | + go func(i int) { |
| 69 | + defer func() { <-sem; wg.Done() }() |
| 70 | + |
| 71 | + resp, err := client.Get(ts.URL + "/index.php") |
| 72 | + if err != nil { |
| 73 | + return |
| 74 | + } |
| 75 | + body, _ := io.ReadAll(resp.Body) |
| 76 | + _ = resp.Body.Close() |
| 77 | + |
| 78 | + if resp.StatusCode == 200 && strings.Contains(string(body), "I am by birth a Genevese") { |
| 79 | + mu.Lock() |
| 80 | + successCount++ |
| 81 | + mu.Unlock() |
| 82 | + } |
| 83 | + }(i) |
| 84 | + } |
| 85 | + wg.Wait() |
| 86 | + |
| 87 | + t.Logf("Success: %d/%d", successCount, totalRequests) |
| 88 | + assert.Equal(t, totalRequests, successCount, |
| 89 | + "all requests should succeed despite regular thread restarts") |
| 90 | + }, &testOptions{ |
| 91 | + realServer: true, |
| 92 | + initOpts: []frankenphp.Option{ |
| 93 | + frankenphp.WithNumThreads(4), |
| 94 | + frankenphp.WithMaxRequests(maxRequests), |
| 95 | + }, |
| 96 | + }) |
| 97 | +} |
| 98 | + |
| 99 | +// TestModuleMaxRequestsZeroIsUnlimited verifies that max_requests=0 (default) |
| 100 | +// means threads never restart. |
| 101 | +func TestModuleMaxRequestsZeroIsUnlimited(t *testing.T) { |
| 102 | + runTest(t, func(_ func(http.ResponseWriter, *http.Request), ts *httptest.Server, _ int) { |
| 103 | + require.NotNil(t, ts) |
| 104 | + client := &http.Client{Timeout: 5 * time.Second} |
| 105 | + |
| 106 | + for i := 0; i < 50; i++ { |
| 107 | + resp, err := client.Get(ts.URL + "/index.php") |
| 108 | + require.NoError(t, err) |
| 109 | + body, _ := io.ReadAll(resp.Body) |
| 110 | + _ = resp.Body.Close() |
| 111 | + |
| 112 | + assert.Equal(t, 200, resp.StatusCode) |
| 113 | + assert.Contains(t, string(body), "I am by birth a Genevese") |
| 114 | + } |
| 115 | + }, &testOptions{ |
| 116 | + realServer: true, |
| 117 | + initOpts: []frankenphp.Option{frankenphp.WithNumThreads(2)}, |
| 118 | + }) |
| 119 | +} |
0 commit comments