From 7cc7d661f97a4fb828d47886104cd298049cd086 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Fri, 15 Dec 2023 12:06:45 +0100 Subject: [PATCH 1/3] test: many concurrent reload requests --- caddy/caddy_test.go | 53 +++++++++++++++++++++++++++++++++++++++++++-- caddy/go.mod | 3 +++ 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/caddy/caddy_test.go b/caddy/caddy_test.go index e487c2861..45d4313ce 100644 --- a/caddy/caddy_test.go +++ b/caddy/caddy_test.go @@ -9,6 +9,8 @@ import ( "testing" "github.com/caddyserver/caddy/v2/caddytest" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestPHP(t *testing.T) { @@ -35,7 +37,6 @@ func TestPHP(t *testing.T) { for i := 0; i < 100; i++ { wg.Add(1) - go func(i int) { tester.AssertGetResponse(fmt.Sprintf("http://localhost:9080/index.php?i=%d", i), http.StatusOK, fmt.Sprintf("I am by birth a Genevese (%d)", i)) wg.Done() @@ -100,7 +101,6 @@ func TestWorker(t *testing.T) { for i := 0; i < 100; i++ { wg.Add(1) - go func(i int) { tester.AssertGetResponse(fmt.Sprintf("http://localhost:9080/index.php?i=%d", i), http.StatusOK, fmt.Sprintf("I am by birth a Genevese (%d)", i)) wg.Done() @@ -189,3 +189,52 @@ func TestPHPServerDirectiveDisableFileServer(t *testing.T) { tester.AssertGetResponse("http://localhost:9080", http.StatusOK, "I am by birth a Genevese (i not set)") tester.AssertGetResponse("http://localhost:9080/hello.txt", http.StatusNotFound, "Not found") } + +// TestReload sends many concurrent reload requests, as done by Laravel Octane. +// Better run this test with -race. +func TestReload(t *testing.T) { + tester := caddytest.NewTester(t) + tester.InitServer(` + { + skip_install_trust + admin localhost:2999 + http_port 9080 + https_port 9443 + + frankenphp { + worker ../testdata/index.php + } + order php_server before respond + } + + localhost:9080 { + root * ../testdata + php_server + } + `, "caddyfile") + + const configURL = "http://localhost:2999/config/apps/frankenphp" + + var wg sync.WaitGroup + for i := 0; i < 20; i++ { + wg.Add(1) + go func() { + resp1, err := tester.Client.Get(configURL) + require.NoError(t, err) + + r, err := http.NewRequest("POST", configURL, resp1.Body) + require.NoError(t, err) + r.Header.Add("Content-Type", "application/json") + r.Header.Add("Cache-Control", "must-revalidate") + + resp, err := tester.Client.Do(r) + require.NoError(t, err) + assert.Equal(t, http.StatusOK, resp.StatusCode) + + wg.Done() + }() + } + wg.Wait() + + tester.AssertGetResponse("http://localhost:9080", http.StatusOK, "I am by birth a Genevese (i not set)") +} diff --git a/caddy/go.mod b/caddy/go.mod index acd64c004..54568c0f1 100644 --- a/caddy/go.mod +++ b/caddy/go.mod @@ -13,6 +13,7 @@ require ( github.com/dunglas/mercure/caddy v0.15.6 github.com/dunglas/vulcain/caddy v1.0.0 github.com/spf13/cobra v1.8.0 + github.com/stretchr/testify v1.8.4 go.uber.org/automaxprocs v1.5.3 go.uber.org/zap v1.26.0 ) @@ -37,6 +38,7 @@ require ( github.com/cespare/xxhash/v2 v2.2.0 // indirect github.com/chzyer/readline v1.5.1 // indirect github.com/cpuguy83/go-md2man/v2 v2.0.3 // indirect + github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect github.com/dgraph-io/badger v1.6.2 // indirect github.com/dgraph-io/badger/v2 v2.2007.4 // indirect github.com/dgraph-io/ristretto v0.1.1 // indirect @@ -113,6 +115,7 @@ require ( github.com/pelletier/go-toml/v2 v2.1.0 // indirect github.com/perimeterx/marshmallow v1.1.5 // indirect github.com/pkg/errors v0.9.1 // indirect + github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect github.com/prometheus/client_golang v1.17.0 // indirect github.com/prometheus/client_model v0.5.0 // indirect github.com/prometheus/common v0.45.0 // indirect From 0048f847414079357e2b9a66cc674265b284c357 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Fri, 15 Dec 2023 18:18:01 +0100 Subject: [PATCH 2/3] some fixes --- caddy/caddy_test.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/caddy/caddy_test.go b/caddy/caddy_test.go index 45d4313ce..cba9c05b9 100644 --- a/caddy/caddy_test.go +++ b/caddy/caddy_test.go @@ -37,9 +37,11 @@ func TestPHP(t *testing.T) { for i := 0; i < 100; i++ { wg.Add(1) + go func(i int) { + defer wg.Done() + tester.AssertGetResponse(fmt.Sprintf("http://localhost:9080/index.php?i=%d", i), http.StatusOK, fmt.Sprintf("I am by birth a Genevese (%d)", i)) - wg.Done() }(i) } wg.Wait() @@ -101,9 +103,11 @@ func TestWorker(t *testing.T) { for i := 0; i < 100; i++ { wg.Add(1) + go func(i int) { + defer wg.Done() + tester.AssertGetResponse(fmt.Sprintf("http://localhost:9080/index.php?i=%d", i), http.StatusOK, fmt.Sprintf("I am by birth a Genevese (%d)", i)) - wg.Done() }(i) } wg.Wait() @@ -218,7 +222,10 @@ func TestReload(t *testing.T) { var wg sync.WaitGroup for i := 0; i < 20; i++ { wg.Add(1) + go func() { + defer wg.Done() + resp1, err := tester.Client.Get(configURL) require.NoError(t, err) @@ -230,8 +237,6 @@ func TestReload(t *testing.T) { resp, err := tester.Client.Do(r) require.NoError(t, err) assert.Equal(t, http.StatusOK, resp.StatusCode) - - wg.Done() }() } wg.Wait() From c2524a50224005536adb82c96fb9105c1c9bca41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Sat, 16 Dec 2023 17:35:39 +0100 Subject: [PATCH 3/3] fix test --- caddy/caddy_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/caddy/caddy_test.go b/caddy/caddy_test.go index cba9c05b9..313dcf823 100644 --- a/caddy/caddy_test.go +++ b/caddy/caddy_test.go @@ -229,7 +229,7 @@ func TestReload(t *testing.T) { resp1, err := tester.Client.Get(configURL) require.NoError(t, err) - r, err := http.NewRequest("POST", configURL, resp1.Body) + r, err := http.NewRequest("PATCH", configURL, resp1.Body) require.NoError(t, err) r.Header.Add("Content-Type", "application/json") r.Header.Add("Cache-Control", "must-revalidate")