TestWorkerMaxRequests verifies that a worker restarts after reaching max_requests.
(t *testing.T)
| 174 | |
| 175 | // TestWorkerMaxRequests verifies that a worker restarts after reaching max_requests. |
| 176 | func TestWorkerMaxRequests(t *testing.T) { |
| 177 | const maxRequests = 5 |
| 178 | const totalRequests = 20 |
| 179 | |
| 180 | var buf syncBuffer |
| 181 | logger := slog.New(slog.NewTextHandler(&buf, &slog.HandlerOptions{Level: slog.LevelDebug})) |
| 182 | |
| 183 | runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, _ int) { |
| 184 | instanceIDs := make(map[string]int) |
| 185 | |
| 186 | for i := 0; i < totalRequests; i++ { |
| 187 | body, resp := testGet("http://example.com/worker-counter-persistent.php", handler, t) |
| 188 | assert.Equal(t, 200, resp.StatusCode) |
| 189 | |
| 190 | parts := strings.Split(body, ",") |
| 191 | if len(parts) == 2 { |
| 192 | instanceID := strings.TrimPrefix(parts[0], "instance:") |
| 193 | instanceIDs[instanceID]++ |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | t.Logf("Unique worker instances seen: %d (expected >= %d)", len(instanceIDs), totalRequests/maxRequests) |
| 198 | for id, count := range instanceIDs { |
| 199 | t.Logf(" instance %s: handled %d requests", id, count) |
| 200 | } |
| 201 | |
| 202 | assert.GreaterOrEqual(t, len(instanceIDs), totalRequests/maxRequests) |
| 203 | |
| 204 | for id, count := range instanceIDs { |
| 205 | assert.LessOrEqual(t, count, maxRequests, |
| 206 | fmt.Sprintf("instance %s handled %d requests, exceeding max_requests=%d", id, count, maxRequests)) |
| 207 | } |
| 208 | |
| 209 | restartCount := strings.Count(buf.String(), "max requests reached, restarting") |
| 210 | t.Logf("Worker restarts observed: %d", restartCount) |
| 211 | assert.GreaterOrEqual(t, restartCount, 2) |
| 212 | }, &testOptions{ |
| 213 | workerScript: "worker-counter-persistent.php", |
| 214 | nbWorkers: 1, |
| 215 | nbParallelRequests: 1, |
| 216 | logger: logger, |
| 217 | initOpts: []frankenphp.Option{frankenphp.WithNumThreads(2), frankenphp.WithMaxRequests(maxRequests)}, |
| 218 | }) |
| 219 | } |
| 220 | |
| 221 | // TestWorkerMaxRequestsHighConcurrency verifies max_requests works under concurrent load. |
| 222 | func TestWorkerMaxRequestsHighConcurrency(t *testing.T) { |
nothing calls this directly
no test coverage detected