Skip to content

Commit

Permalink
client-go: add unit test for Request thread safety
Browse files Browse the repository at this point in the history
Kubernetes-commit: ca492a9b62a586caf3e3cd22bc020108a63fb395
  • Loading branch information
tkashem authored and k8s-publishing-bot committed Aug 26, 2022
1 parent b135083 commit 67f1485
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions rest/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"reflect"
"strings"
"sync"
"sync/atomic"
"syscall"
"testing"
"time"
Expand Down Expand Up @@ -3987,3 +3988,50 @@ func TestRetryableConditions(t *testing.T) {
}
}
}

func TestRequestConcurrencyWithRetry(t *testing.T) {
var attempts int32
client := clientForFunc(func(req *http.Request) (*http.Response, error) {
defer func() {
atomic.AddInt32(&attempts, 1)
}()

// always send a retry-after response
return &http.Response{
StatusCode: http.StatusInternalServerError,
Header: http.Header{"Retry-After": []string{"1"}},
}, nil
})

req := &Request{
verb: "POST",
c: &RESTClient{
content: defaultContentConfig(),
Client: client,
},
backoff: &noSleepBackOff{},
maxRetries: 9, // 10 attempts in total, including the first
retryFn: defaultRequestRetryFn,
}

concurrency := 20
wg := sync.WaitGroup{}
wg.Add(concurrency)
startCh := make(chan struct{})
for i := 0; i < concurrency; i++ {
go func() {
defer wg.Done()
<-startCh
req.Do(context.Background())
}()
}

close(startCh)
wg.Wait()

// we expect (concurrency*req.maxRetries+1) attempts to be recorded
expected := concurrency * (req.maxRetries + 1)
if atomic.LoadInt32(&attempts) != int32(expected) {
t.Errorf("Expected attempts: %d, but got: %d", expected, attempts)
}
}

0 comments on commit 67f1485

Please sign in to comment.