This commit is contained in:
@@ -144,6 +144,61 @@ func TestClientWingbitsEndpoints(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestClientRetry(t *testing.T) {
|
||||
// Fail with 503 for the first two attempts, then succeed; the client must
|
||||
// retry past the failures and return the eventual body.
|
||||
var hits int
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
if hits++; hits <= 2 {
|
||||
http.Error(w, "warming up", http.StatusServiceUnavailable)
|
||||
return
|
||||
}
|
||||
w.Write([]byte(`{}`))
|
||||
}))
|
||||
t.Cleanup(srv.Close)
|
||||
|
||||
host, port := splitHostPort(t, strings.TrimPrefix(srv.URL, "http://"))
|
||||
c, err := New(
|
||||
WithWingbitsEndpoint(Endpoint{Host: host, Port: port}),
|
||||
WithRetry(RetryConfig{MaxRetries: 3, BaseDelay: time.Millisecond, MaxDelay: 5 * time.Millisecond}),
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := c.Status(context.Background()); err != nil {
|
||||
t.Fatalf("status after retries: %v", err)
|
||||
}
|
||||
if hits != 3 {
|
||||
t.Fatalf("expected 3 attempts, got %d", hits)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClientRetryGivesUp(t *testing.T) {
|
||||
// A 404 is not retryable: the client must fail after a single attempt even
|
||||
// with retries configured.
|
||||
var hits int
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
hits++
|
||||
http.Error(w, "nope", http.StatusNotFound)
|
||||
}))
|
||||
t.Cleanup(srv.Close)
|
||||
|
||||
host, port := splitHostPort(t, strings.TrimPrefix(srv.URL, "http://"))
|
||||
c, err := New(
|
||||
WithWingbitsEndpoint(Endpoint{Host: host, Port: port}),
|
||||
WithRetry(RetryConfig{MaxRetries: 3, BaseDelay: time.Millisecond}),
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := c.Status(context.Background()); err == nil {
|
||||
t.Fatal("expected error on 404")
|
||||
}
|
||||
if hits != 1 {
|
||||
t.Fatalf("expected 1 attempt for non-retryable status, got %d", hits)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPollAircraft(t *testing.T) {
|
||||
c := newTestClient(t)
|
||||
ctx := t.Context()
|
||||
|
||||
Reference in New Issue
Block a user