|
| 1 | +package frankenphp_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "log/slog" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "strings" |
| 8 | + "sync" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/dunglas/frankenphp" |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | +) |
| 14 | + |
| 15 | +// TestModuleMaxRequests verifies that regular (non-worker) PHP threads restart |
| 16 | +// after reaching max_requests by checking debug logs for restart messages. |
| 17 | +func TestModuleMaxRequests(t *testing.T) { |
| 18 | + const maxRequests = 5 |
| 19 | + const totalRequests = 30 |
| 20 | + |
| 21 | + var buf syncBuffer |
| 22 | + logger := slog.New(slog.NewTextHandler(&buf, &slog.HandlerOptions{Level: slog.LevelDebug})) |
| 23 | + |
| 24 | + runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, _ int) { |
| 25 | + for i := 0; i < totalRequests; i++ { |
| 26 | + body, resp := testGet("http://example.com/index.php", handler, t) |
| 27 | + assert.Equal(t, 200, resp.StatusCode) |
| 28 | + assert.Contains(t, body, "I am by birth a Genevese") |
| 29 | + } |
| 30 | + |
| 31 | + restartCount := strings.Count(buf.String(), "max requests reached, restarting thread") |
| 32 | + t.Logf("Thread restarts observed: %d", restartCount) |
| 33 | + assert.GreaterOrEqual(t, restartCount, 2, |
| 34 | + "with maxRequests=%d and %d requests on 2 threads, at least 2 restarts should occur", maxRequests, totalRequests) |
| 35 | + }, &testOptions{ |
| 36 | + logger: logger, |
| 37 | + initOpts: []frankenphp.Option{ |
| 38 | + frankenphp.WithNumThreads(2), |
| 39 | + frankenphp.WithMaxRequests(maxRequests), |
| 40 | + }, |
| 41 | + }) |
| 42 | +} |
| 43 | + |
| 44 | +// TestModuleMaxRequestsConcurrent verifies max_requests works under concurrent load |
| 45 | +// in module mode. All requests must succeed despite threads restarting. |
| 46 | +func TestModuleMaxRequestsConcurrent(t *testing.T) { |
| 47 | + const maxRequests = 10 |
| 48 | + const totalRequests = 200 |
| 49 | + |
| 50 | + runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, _ int) { |
| 51 | + var wg sync.WaitGroup |
| 52 | + |
| 53 | + for i := 0; i < totalRequests; i++ { |
| 54 | + wg.Add(1) |
| 55 | + go func() { |
| 56 | + defer wg.Done() |
| 57 | + body, resp := testGet("http://example.com/index.php", handler, t) |
| 58 | + assert.Equal(t, 200, resp.StatusCode) |
| 59 | + assert.Contains(t, body, "I am by birth a Genevese") |
| 60 | + }() |
| 61 | + } |
| 62 | + wg.Wait() |
| 63 | + }, &testOptions{ |
| 64 | + initOpts: []frankenphp.Option{ |
| 65 | + frankenphp.WithNumThreads(8), |
| 66 | + frankenphp.WithMaxRequests(maxRequests), |
| 67 | + }, |
| 68 | + }) |
| 69 | +} |
0 commit comments