Skip to content

Commit 5442134

Browse files
committed
feat: add test for wait time
1 parent 2f8e716 commit 5442134

1 file changed

Lines changed: 52 additions & 8 deletions

File tree

caddy/caddy_test.go

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -840,8 +840,6 @@ func TestMaxWaitTime(t *testing.T) {
840840
}
841841
`, "caddyfile")
842842

843-
ctx := caddy.ActiveContext()
844-
845843
// send 10 requests simultaneously, at least one request should be stalled longer than 1ns
846844
// since we only have 1 thread, this will cause a 504 Gateway Timeout
847845
wg := sync.WaitGroup{}
@@ -859,19 +857,65 @@ func TestMaxWaitTime(t *testing.T) {
859857
wg.Wait()
860858

861859
require.True(t, success.Load(), "At least one request should have failed with a 504 Gateway Timeout status")
862-
// Check metrics
860+
}
861+
862+
func TestMaxWaitTimeWorker(t *testing.T) {
863+
tester := caddytest.NewTester(t)
864+
tester.InitServer(`
865+
{
866+
skip_install_trust
867+
admin localhost:2999
868+
metrics
869+
http_port `+testPort+`
870+
871+
frankenphp {
872+
max_wait_time 1ns
873+
worker {
874+
name service
875+
num 1
876+
file ../testdata/sleep.php
877+
}
878+
}
879+
}
880+
881+
localhost:`+testPort+` {
882+
route {
883+
root ../testdata
884+
php
885+
}
886+
}
887+
`, "caddyfile")
888+
889+
// send 10 requests simultaneously, at least one request should be stalled longer than 1ns
890+
// since we only have 1 thread, this will cause a 504 Gateway Timeout
891+
wg := sync.WaitGroup{}
892+
success := atomic.Bool{}
893+
wg.Add(10)
894+
for i := 0; i < 10; i++ {
895+
go func() {
896+
statusCode := getStatusCode("http://localhost:"+testPort+"/sleep.php?sleep=100", t)
897+
fmt.Printf("Status code: %d\n", statusCode)
898+
if statusCode == http.StatusGatewayTimeout {
899+
success.Store(true)
900+
}
901+
wg.Done()
902+
}()
903+
}
904+
wg.Wait()
905+
906+
require.True(t, success.Load(), "At least one request should have failed with a 504 Gateway Timeout status")
863907
expectedMetrics := `
864-
# HELP frankenphp_queue_depth Number of regular queued requests
865-
# TYPE frankenphp_queue_depth gauge
866-
frankenphp_queue_depth{worker="service"} 0
908+
# TYPE frankenphp_worker_queue_depth gauge
909+
frankenphp_worker_queue_depth{worker="service"} 0
867910
`
868911

912+
ctx := caddy.ActiveContext()
913+
fmt.Printf("Metrics: %d\n", testutil.CollectAndCount(ctx.GetMetricsRegistry(), "frankenphp_worker_queue_depth"))
869914
require.NoError(t,
870915
testutil.GatherAndCompare(
871916
ctx.GetMetricsRegistry(),
872917
strings.NewReader(expectedMetrics),
873-
"frankenphp_total_workers",
874-
"frankenphp_ready_workers",
918+
"frankenphp_worker_queue_depth",
875919
))
876920
}
877921

0 commit comments

Comments
 (0)