...

Source file src/edge-infra.dev/pkg/lib/server/readyoption_test.go

Documentation: edge-infra.dev/pkg/lib/server

     1  package server
     2  
     3  import (
     4  	"context"
     5  	"net/http"
     6  	"testing"
     7  
     8  	"edge-infra.dev/pkg/lib/server/inmemorylistener"
     9  )
    10  
    11  func TestReadyFuncOption(t *testing.T) {
    12  	var l = inmemorylistener.New()
    13  	var httpc = l.HTTPClient()
    14  	var s, ready, err = NewReadyServer(OptionListener(l))
    15  	if err != nil {
    16  		t.Fatal(err)
    17  	}
    18  	go s.Run()                             //nolint:errcheck
    19  	defer s.Shutdown(context.Background()) //nolint:errcheck
    20  
    21  	// Check that the ready function is serving HTTP status 503 (aka http.StatusServiceUnavailable)
    22  	for i := 0; i < 5; i++ {
    23  		if resp, err := httpc.Get("http://localhost/readyz"); err != nil {
    24  			t.Fatal(err)
    25  		} else if resp.StatusCode != http.StatusServiceUnavailable {
    26  			const msg = "Want http.StatusServiceUnavailable status code %d before the ready func has been called. Got status code %d"
    27  			t.Fatalf(msg, http.StatusServiceUnavailable, resp.StatusCode)
    28  		}
    29  	}
    30  	t.Logf("Yay, got status %d before ready function was called", http.StatusServiceUnavailable)
    31  
    32  	// Call the ready function so the server serves 200 OK.
    33  	t.Logf("Calling ready function.")
    34  	ready()
    35  	t.Logf("Ready function called.")
    36  
    37  	// Check that the ready function is serving 200 OK
    38  	for i := 0; i < 5; i++ {
    39  		if resp, err := httpc.Get("http://localhost/readyz"); err != nil {
    40  			t.Fatal(err)
    41  		} else if resp.StatusCode != http.StatusOK {
    42  			const msg = "Want http.StatusOk status code %d after the ready func has been called. Got status code %d"
    43  			t.Fatalf(msg, http.StatusOK, resp.StatusCode)
    44  		}
    45  	}
    46  	t.Logf("Yay, got status %d after ready function was called", http.StatusOK)
    47  }
    48  

View as plain text