From d982963e0b3439aec30c390bb346ccc0574a502b Mon Sep 17 00:00:00 2001 From: Andrew Dunham Date: Fri, 14 Oct 2022 15:39:22 +0200 Subject: [PATCH] control/controlhttp: try to avoid flakes in TestDialPlan Updates tailscale/corp#7446 Signed-off-by: Andrew Dunham Change-Id: Ifcf3b5176f065c2e67cbb8943f6356dea720a9c5 (cherry picked from commit a4e707bcf0cac1a023e20c449a4150fb387a1ef1) --- control/controlhttp/http_test.go | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/control/controlhttp/http_test.go b/control/controlhttp/http_test.go index 3b1b66168..323327cbb 100644 --- a/control/controlhttp/http_test.go +++ b/control/controlhttp/http_test.go @@ -459,13 +459,26 @@ func TestDialPlan(t *testing.T) { const ( testProtocolVersion = 1 - - // We need consistent ports for each address; these are chosen - // randomly and we hope that they won't conflict during this test. - httpPort = "40080" - httpsPort = "40443" ) + getRandomPort := func() string { + ln, err := net.Listen("tcp", ":0") + if err != nil { + t.Fatalf("net.Listen: %v", err) + } + defer ln.Close() + _, port, err := net.SplitHostPort(ln.Addr().String()) + if err != nil { + t.Fatal(err) + } + return port + } + + // We need consistent ports for each address; these are chosen + // randomly and we hope that they won't conflict during this test. + httpPort := getRandomPort() + httpsPort := getRandomPort() + makeHandler := func(t *testing.T, name string, host netip.Addr, wrap func(http.Handler) http.Handler) { done := make(chan struct{}) t.Cleanup(func() {