mirror of
https://github.com/kubernetes/client-go.git
synced 2025-09-11 12:02:30 +00:00
client-go/rest: backoff with context support
The BackoffManager interface sleeps without considering the caller's context, i.e. cancellation is not supported. This alone is reason enough to deprecate it and to replace it with an interface that supports a context parameter. The other reason is that contextual logging needs that parameter. Kubernetes-commit: b15a1943d51adfb8c5e0185d58d25e038c3d6ade
This commit is contained in:
committed by
Kubernetes Publisher
parent
2b2015d460
commit
7aa9904196
@@ -17,10 +17,14 @@ limitations under the License.
|
||||
package rest
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/url"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"k8s.io/client-go/util/flowcontrol"
|
||||
)
|
||||
|
||||
@@ -77,3 +81,38 @@ func TestURLBackoffFunctionality(t *testing.T) {
|
||||
t.Errorf("The final return code %v should have resulted in a backoff ! ", returnCodes[7])
|
||||
}
|
||||
}
|
||||
|
||||
func TestBackoffManagerNopContext(t *testing.T) {
|
||||
mock := NewMockBackoffManager(t)
|
||||
|
||||
sleepDuration := 42 * time.Second
|
||||
mock.On("Sleep", sleepDuration).Return()
|
||||
url := &url.URL{}
|
||||
mock.On("CalculateBackoff", url).Return(time.Second)
|
||||
err := errors.New("fake error")
|
||||
responseCode := 404
|
||||
mock.On("UpdateBackoff", url, err, responseCode).Return()
|
||||
|
||||
ctx := context.Background()
|
||||
wrapper := backoffManagerNopContext{BackoffManager: mock}
|
||||
wrapper.SleepWithContext(ctx, sleepDuration)
|
||||
wrapper.CalculateBackoffWithContext(ctx, url)
|
||||
wrapper.UpdateBackoffWithContext(ctx, url, err, responseCode)
|
||||
}
|
||||
|
||||
func TestNoBackoff(t *testing.T) {
|
||||
var backoff NoBackoff
|
||||
assert.Equal(t, 0*time.Second, backoff.CalculateBackoff(nil))
|
||||
assert.Equal(t, 0*time.Second, backoff.CalculateBackoffWithContext(context.Background(), nil))
|
||||
|
||||
start := time.Now()
|
||||
backoff.Sleep(0 * time.Second)
|
||||
assert.WithinDuration(t, start, time.Now(), time.Minute /* pretty generous, but we don't want to flake */, time.Since(start), "backoff.Sleep")
|
||||
|
||||
// Cancel right away to prevent sleeping.
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
start = time.Now()
|
||||
backoff.SleepWithContext(ctx, 10*time.Minute)
|
||||
assert.WithinDuration(t, start, time.Now(), time.Minute /* pretty generous, but we don't want to flake */, time.Since(start), "backoff.SleepWithContext")
|
||||
}
|
||||
|
Reference in New Issue
Block a user