package server import ( "context" "net/http" "testing" "edge-infra.dev/pkg/lib/server/inmemorylistener" ) func TestReadyFuncOption(t *testing.T) { var l = inmemorylistener.New() var httpc = l.HTTPClient() var s, ready, err = NewReadyServer(OptionListener(l)) if err != nil { t.Fatal(err) } go s.Run() //nolint:errcheck defer s.Shutdown(context.Background()) //nolint:errcheck // Check that the ready function is serving HTTP status 503 (aka http.StatusServiceUnavailable) for i := 0; i < 5; i++ { if resp, err := httpc.Get("http://localhost/readyz"); err != nil { t.Fatal(err) } else if resp.StatusCode != http.StatusServiceUnavailable { const msg = "Want http.StatusServiceUnavailable status code %d before the ready func has been called. Got status code %d" t.Fatalf(msg, http.StatusServiceUnavailable, resp.StatusCode) } } t.Logf("Yay, got status %d before ready function was called", http.StatusServiceUnavailable) // Call the ready function so the server serves 200 OK. t.Logf("Calling ready function.") ready() t.Logf("Ready function called.") // Check that the ready function is serving 200 OK for i := 0; i < 5; i++ { if resp, err := httpc.Get("http://localhost/readyz"); err != nil { t.Fatal(err) } else if resp.StatusCode != http.StatusOK { const msg = "Want http.StatusOk status code %d after the ready func has been called. Got status code %d" t.Fatalf(msg, http.StatusOK, resp.StatusCode) } } t.Logf("Yay, got status %d after ready function was called", http.StatusOK) }