Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: many concurrent reload requests #390

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 56 additions & 2 deletions caddy/caddy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -37,8 +39,9 @@ func TestPHP(t *testing.T) {
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()
Expand Down Expand Up @@ -102,8 +105,9 @@ func TestWorker(t *testing.T) {
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()
Expand Down Expand Up @@ -189,3 +193,53 @@ 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() {
defer wg.Done()

resp1, err := tester.Client.Get(configURL)
require.NoError(t, err)

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")

resp, err := tester.Client.Do(r)
require.NoError(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)
}()
}
wg.Wait()

tester.AssertGetResponse("http://localhost:9080", http.StatusOK, "I am by birth a Genevese (i not set)")
}
3 changes: 3 additions & 0 deletions caddy/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading