0
0
Fork 0
mirror of https://github.com/strukturag/nextcloud-spreed-signaling.git synced 2025-05-12 18:51:47 +00:00

Only simulate sleeping in "TestBackoff_Exponential".

Add another test that (shortly) sleeps.
This commit is contained in:
Joachim Bauch 2025-04-16 15:59:49 +02:00
parent 271b45c963
commit 7bd4bf96f5
No known key found for this signature in database
GPG key ID: 77C1D22D53E15F02
2 changed files with 22 additions and 6 deletions

View file

@ -37,6 +37,8 @@ type exponentialBackoff struct {
initial time.Duration
maxWait time.Duration
nextWait time.Duration
getContextWithTimeout func(parent context.Context, timeout time.Duration) (context.Context, context.CancelFunc)
}
func NewExponentialBackoff(initial time.Duration, maxWait time.Duration) (Backoff, error) {
@ -52,6 +54,8 @@ func NewExponentialBackoff(initial time.Duration, maxWait time.Duration) (Backof
maxWait: maxWait,
nextWait: initial,
getContextWithTimeout: context.WithTimeout,
}, nil
}
@ -64,7 +68,7 @@ func (b *exponentialBackoff) NextWait() time.Duration {
}
func (b *exponentialBackoff) Wait(ctx context.Context) {
waiter, cancel := context.WithTimeout(ctx, b.nextWait)
waiter, cancel := b.getContextWithTimeout(ctx, b.nextWait)
defer cancel()
b.nextWait = b.nextWait * 2

View file

@ -31,7 +31,6 @@ import (
)
func TestBackoff_Exponential(t *testing.T) {
t.Parallel()
assert := assert.New(t)
minWait := 100 * time.Millisecond
backoff, err := NewExponentialBackoff(minWait, 500*time.Millisecond)
@ -47,14 +46,27 @@ func TestBackoff_Exponential(t *testing.T) {
for _, wait := range waitTimes {
assert.Equal(wait, backoff.NextWait())
a := time.Now()
backoff.(*exponentialBackoff).getContextWithTimeout = func(parent context.Context, timeout time.Duration) (context.Context, context.CancelFunc) {
assert.Equal(wait, timeout)
return context.WithTimeout(parent, time.Millisecond)
}
backoff.Wait(context.Background())
b := time.Now()
assert.GreaterOrEqual(b.Sub(a), wait)
}
backoff.Reset()
backoff.(*exponentialBackoff).getContextWithTimeout = func(parent context.Context, timeout time.Duration) (context.Context, context.CancelFunc) {
assert.Equal(minWait, timeout)
return context.WithTimeout(parent, time.Millisecond)
}
backoff.Wait(context.Background())
}
func TestBackoff_ExponentialRealSleep(t *testing.T) {
assert := assert.New(t)
minWait := 100 * time.Millisecond
backoff, err := NewExponentialBackoff(minWait, 500*time.Millisecond)
require.NoError(t, err)
a := time.Now()
backoff.Wait(context.Background())
b := time.Now()