github.com/dtroyer-salad/og2/v2@v2.0.0-20240412154159-c47231610877/registry/remote/retry/client_test.go (about)

     1  /*
     2  Copyright The ORAS Authors.
     3  Licensed under the Apache License, Version 2.0 (the "License");
     4  you may not use this file except in compliance with the License.
     5  You may obtain a copy of the License at
     6  
     7  http://www.apache.org/licenses/LICENSE-2.0
     8  
     9  Unless required by applicable law or agreed to in writing, software
    10  distributed under the License is distributed on an "AS IS" BASIS,
    11  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  See the License for the specific language governing permissions and
    13  limitations under the License.
    14  */
    15  
    16  package retry
    17  
    18  import (
    19  	"bytes"
    20  	"net/http"
    21  	"net/http/httptest"
    22  	"testing"
    23  )
    24  
    25  func Test_Client(t *testing.T) {
    26  	testCases := []struct {
    27  		name        string
    28  		attempts    int
    29  		retryAfter  bool
    30  		StatusCode  int
    31  		expectedErr bool
    32  	}{
    33  		{
    34  			name:     "successful request with 0 retry",
    35  			attempts: 1, retryAfter: false, StatusCode: http.StatusOK, expectedErr: false,
    36  		},
    37  		{
    38  			name: "successful request with 1 retry caused by rate limit",
    39  			// 1 request + 1 retry = 2 attempts
    40  			attempts: 2, retryAfter: true, StatusCode: http.StatusTooManyRequests, expectedErr: false,
    41  		},
    42  		{
    43  			name: "successful request with 1 retry caused by 408",
    44  			// 1 request + 1 retry = 2 attempts
    45  			attempts: 2, retryAfter: false, StatusCode: http.StatusRequestTimeout, expectedErr: false,
    46  		},
    47  		{
    48  			name: "successful request with 2 retries caused by 429",
    49  			// 1 request + 2 retries = 3 attempts
    50  			attempts: 3, retryAfter: false, StatusCode: http.StatusTooManyRequests, expectedErr: false,
    51  		},
    52  		{
    53  			name: "unsuccessful request with 6 retries caused by too many retries",
    54  			// 1 request + 6 retries = 7 attempts
    55  			attempts: 7, retryAfter: false, StatusCode: http.StatusServiceUnavailable, expectedErr: true,
    56  		},
    57  	}
    58  	for _, tc := range testCases {
    59  		t.Run(tc.name, func(t *testing.T) {
    60  			count := 0
    61  			ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    62  				count++
    63  				if count < tc.attempts {
    64  					if tc.retryAfter {
    65  						w.Header().Set("Retry-After", "1")
    66  					}
    67  					http.Error(w, "error", tc.StatusCode)
    68  					return
    69  				}
    70  				w.WriteHeader(http.StatusOK)
    71  			}))
    72  			defer ts.Close()
    73  
    74  			req, err := http.NewRequest(http.MethodPost, ts.URL, bytes.NewReader([]byte("test")))
    75  			if err != nil {
    76  				t.Fatalf("failed to create test request: %v", err)
    77  			}
    78  
    79  			resp, err := DefaultClient.Do(req)
    80  			if err != nil {
    81  				t.Fatalf("failed to do test request: %v", err)
    82  			}
    83  			if tc.expectedErr {
    84  				if count != (tc.attempts - 1) {
    85  					t.Errorf("expected attempts %d, got %d", tc.attempts, count)
    86  				}
    87  				if resp.StatusCode != http.StatusServiceUnavailable {
    88  					t.Errorf("expected status code %d, got %d", http.StatusServiceUnavailable, resp.StatusCode)
    89  				}
    90  				return
    91  			}
    92  			if tc.attempts != count {
    93  				t.Errorf("expected attempts %d, got %d", tc.attempts, count)
    94  			}
    95  		})
    96  	}
    97  }