Skip to content
Merged
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
22 changes: 22 additions & 0 deletions caddy/watcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,32 @@
package caddy_test

import (
"net"
"net/http"
"testing"
"time"

"github.com/caddyserver/caddy/v2/caddytest"
"github.com/stretchr/testify/require"
)

// waitForListener polls a raw TCP connection instead of an HTTP request so it
// doesn't consume a request from the worker's counter (unlike the initServer
// helper), while still covering the same listener hot-swap race it guards against.
func waitForListener(t *testing.T, addr string) {
t.Helper()

require.Eventually(t, func() bool {
conn, err := net.DialTimeout("tcp", addr, 200*time.Millisecond)
if err != nil {
return false
}
_ = conn.Close()

return true
}, 5*time.Second, 100*time.Millisecond, "server failed to become ready")
}

func TestWorkerWithInactiveWatcher(t *testing.T) {
tester := caddytest.NewTester(t)
tester.InitServer(`
Expand All @@ -33,6 +53,8 @@ func TestWorkerWithInactiveWatcher(t *testing.T) {
}
`, "caddyfile")

waitForListener(t, "localhost:"+testPort)

tester.AssertGetResponse("http://localhost:"+testPort, http.StatusOK, "requests:1")
tester.AssertGetResponse("http://localhost:"+testPort, http.StatusOK, "requests:2")
}
Loading