From 09d626c5c36ecaf9f325f59ce26632ddd4752e50 Mon Sep 17 00:00:00 2001 From: Andrew Garner Date: Fri, 7 Nov 2025 10:59:38 -0600 Subject: [PATCH] Replace fixed timeout with an eventually with a higher timeout Observed a test failure in a slow CI worker because the hardcoded "sleep" in this test was not long enough. Now the test polls until it observes the expected backend change - or times out. --- .../switchboard/cmd/proxy/main_test.go | 32 +++++++++++++------ 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/src/github.com/cloudfoundry-incubator/switchboard/cmd/proxy/main_test.go b/src/github.com/cloudfoundry-incubator/switchboard/cmd/proxy/main_test.go index 4d8b42dd5..021fdc63a 100644 --- a/src/github.com/cloudfoundry-incubator/switchboard/cmd/proxy/main_test.go +++ b/src/github.com/cloudfoundry-incubator/switchboard/cmd/proxy/main_test.go @@ -1253,20 +1253,32 @@ var _ = Describe("Switchboard", func() { }) It("reports the correct active backend", func() { - time.Sleep(3 * time.Second) + var expectedName string + Eventually(func() error { + url := fmt.Sprintf("https://localhost:%d/v0/cluster", switchboardAPIPort) + req, err := http.NewRequest("GET", url, nil) + if err != nil { + return err + } - // Get active backend from API - url := fmt.Sprintf("https://localhost:%d/v0/cluster", switchboardAPIPort) - req, err := http.NewRequest("GET", url, nil) - Expect(err).NotTo(HaveOccurred()) + req.SetBasicAuth("username", "password") + cluster := getClusterFromAPI(httpClient, req) - req.SetBasicAuth("username", "password") - cluster := getClusterFromAPI(httpClient, req) + activeBackend, ok := cluster["activeBackend"].(map[string]interface{}) + if !ok || activeBackend == nil { + return errors.New("no active backend available yet") + } - activeBackend := cluster["activeBackend"].(map[string]interface{}) - expectedName := activeBackend["name"].(string) + name, ok := activeBackend["name"].(string) + if !ok { + return errors.New("active backend name not available") + } - Eventually(switchboardRunner.Buffer()).Should( + expectedName = name + return nil + }, "10s", "500ms").Should(Succeed()) + + Eventually(switchboardRunner.Buffer(), "5s", "100ms").Should( gbytes.Say(fmt.Sprintf(`"active_backend":"%s"`, expectedName))) })