sigs.k8s.io/release-sdk@v0.11.1-0.20240417074027-8061fb5e4952/github/internal/retry_test.go (about)

     1  /*
     2  Copyright 2019 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package internal_test
    18  
    19  import (
    20  	"fmt"
    21  	"io"
    22  	"os"
    23  	"testing"
    24  	"time"
    25  
    26  	"github.com/google/go-github/v60/github"
    27  	"github.com/sirupsen/logrus"
    28  
    29  	"sigs.k8s.io/release-sdk/github/internal"
    30  )
    31  
    32  func TestMain(m *testing.M) {
    33  	// logrus, shut up
    34  	logrus.SetOutput(io.Discard)
    35  	os.Exit(m.Run())
    36  }
    37  
    38  func TestGithubRetryer(t *testing.T) {
    39  	tests := map[string]struct {
    40  		maxTries        int
    41  		sleeper         func(time.Duration)
    42  		errs            []error
    43  		expectedResults []bool
    44  	}{
    45  		"never retry": {
    46  			maxTries: 0,
    47  		},
    48  		"when error is nil, don't retry": {
    49  			maxTries:        1,
    50  			sleeper:         nilSleeper,
    51  			errs:            []error{nil},
    52  			expectedResults: []bool{false},
    53  		},
    54  		"when error is a random error, don't retry": {
    55  			maxTries:        1,
    56  			sleeper:         nilSleeper,
    57  			errs:            []error{fmt.Errorf("some randm error")},
    58  			expectedResults: []bool{false},
    59  		},
    60  		"when the error is a github rate limit error, retry": {
    61  			maxTries:        1,
    62  			sleeper:         sleepChecker(t, 1*time.Minute),
    63  			errs:            []error{&github.RateLimitError{}},
    64  			expectedResults: []bool{true},
    65  		},
    66  		"when the error is a github rate limit error with time, retry": {
    67  			maxTries: 1,
    68  			sleeper: func(got time.Duration) {
    69  				if got.Round(time.Minute) != 30*time.Minute {
    70  					t.Errorf("Expected a time around 30min, got %v", got)
    71  				}
    72  			},
    73  			errs: []error{&github.RateLimitError{
    74  				Rate: github.Rate{
    75  					Reset: github.Timestamp{Time: time.Now().Add(30 * time.Minute)},
    76  				},
    77  			}},
    78  			expectedResults: []bool{true},
    79  		},
    80  		"when the error is a github abuse rate limit error, retry": {
    81  			maxTries:        1,
    82  			sleeper:         nilSleeper,
    83  			errs:            []error{&github.AbuseRateLimitError{}},
    84  			expectedResults: []bool{true},
    85  		},
    86  		"when hitting the secondary rate limit, sleep for random": {
    87  			maxTries:        1,
    88  			sleeper:         nilSleeper,
    89  			errs:            []error{fmt.Errorf("You have exceeded a secondary rate limit. Please wait a few minutes")},
    90  			expectedResults: []bool{true},
    91  		},
    92  		"when the error is a github abuse rate limit error but max tries have been reached, don't retry": {
    93  			maxTries: 2,
    94  			sleeper:  nilSleeper,
    95  			errs: []error{
    96  				&github.AbuseRateLimitError{},
    97  				&github.AbuseRateLimitError{},
    98  				&github.AbuseRateLimitError{},
    99  			},
   100  			expectedResults: []bool{
   101  				true, true, false,
   102  			},
   103  		},
   104  		"when no RetryAfter is specified on the abuse rate limit error, sleep the default amount of time": {
   105  			maxTries:        1,
   106  			sleeper:         sleepChecker(t, 1*time.Minute),
   107  			errs:            []error{&github.AbuseRateLimitError{}},
   108  			expectedResults: []bool{true},
   109  		},
   110  		"when a RetryAfter is specified on the abuse rate limit error, sleep that amount of time": {
   111  			maxTries:        1,
   112  			sleeper:         sleepChecker(t, 42*time.Minute),
   113  			errs:            []error{&github.AbuseRateLimitError{RetryAfter: durPtr(42 * time.Minute)}},
   114  			expectedResults: []bool{true},
   115  		},
   116  	}
   117  
   118  	for name, tc := range tests {
   119  		t.Run(name, func(t *testing.T) {
   120  			tc := tc
   121  			t.Parallel()
   122  
   123  			shouldRetry := internal.GithubErrChecker(tc.maxTries, tc.sleeper)
   124  
   125  			for i, err := range tc.errs {
   126  				if a, e := shouldRetry(err), tc.expectedResults[i]; e != a {
   127  					t.Errorf("Expected to get %t, got: %t", e, a)
   128  				}
   129  			}
   130  		})
   131  	}
   132  }
   133  
   134  func sleepChecker(t *testing.T, expectedSleep time.Duration) func(time.Duration) {
   135  	return func(d time.Duration) {
   136  		if d != expectedSleep {
   137  			t.Errorf("Expected the sleeper to be called with a duration %s, got called with %s", expectedSleep, d)
   138  		}
   139  	}
   140  }
   141  
   142  func nilSleeper(_ time.Duration) {
   143  }
   144  
   145  func durPtr(d time.Duration) *time.Duration {
   146  	return &d
   147  }