|
|
|
@ -1,5 +1,5 @@ |
|
|
|
|
/* |
|
|
|
|
* MinIO Cloud Storage, (C) 2016 MinIO, Inc. |
|
|
|
|
* Minio Cloud Storage, (C) 2016-2020 Minio, Inc. |
|
|
|
|
* |
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
@ -17,25 +17,26 @@ |
|
|
|
|
package cmd |
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
|
"context" |
|
|
|
|
"testing" |
|
|
|
|
"time" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
// Tests for retry timer.
|
|
|
|
|
func TestRetryTimerSimple(t *testing.T) { |
|
|
|
|
doneCh := make(chan struct{}) |
|
|
|
|
attemptCh := newRetryTimerSimple(doneCh) |
|
|
|
|
rctx, cancel := context.WithCancel(context.Background()) |
|
|
|
|
attemptCh := newRetryTimerSimple(rctx) |
|
|
|
|
i := <-attemptCh |
|
|
|
|
if i != 0 { |
|
|
|
|
close(doneCh) |
|
|
|
|
cancel() |
|
|
|
|
t.Fatalf("Invalid attempt counter returned should be 0, found %d instead", i) |
|
|
|
|
} |
|
|
|
|
i = <-attemptCh |
|
|
|
|
if i <= 0 { |
|
|
|
|
close(doneCh) |
|
|
|
|
cancel() |
|
|
|
|
t.Fatalf("Invalid attempt counter returned should be greater than 0, found %d instead", i) |
|
|
|
|
} |
|
|
|
|
close(doneCh) |
|
|
|
|
cancel() |
|
|
|
|
_, ok := <-attemptCh |
|
|
|
|
if ok { |
|
|
|
|
t.Fatal("Attempt counter should be closed") |
|
|
|
@ -44,20 +45,23 @@ func TestRetryTimerSimple(t *testing.T) { |
|
|
|
|
|
|
|
|
|
// Test retry time with no jitter.
|
|
|
|
|
func TestRetryTimerWithNoJitter(t *testing.T) { |
|
|
|
|
doneCh := make(chan struct{}) |
|
|
|
|
rctx, cancel := context.WithCancel(context.Background()) |
|
|
|
|
|
|
|
|
|
// No jitter
|
|
|
|
|
attemptCh := newRetryTimerWithJitter(time.Millisecond, 5*time.Millisecond, NoJitter, doneCh) |
|
|
|
|
attemptCh := newRetryTimerWithJitter(rctx, time.Millisecond, 5*time.Millisecond, NoJitter) |
|
|
|
|
i := <-attemptCh |
|
|
|
|
if i != 0 { |
|
|
|
|
close(doneCh) |
|
|
|
|
cancel() |
|
|
|
|
t.Fatalf("Invalid attempt counter returned should be 0, found %d instead", i) |
|
|
|
|
} |
|
|
|
|
// Loop through the maximum possible attempt.
|
|
|
|
|
for i = range attemptCh { |
|
|
|
|
if i == 30 { |
|
|
|
|
close(doneCh) |
|
|
|
|
break |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
cancel() |
|
|
|
|
_, ok := <-attemptCh |
|
|
|
|
if ok { |
|
|
|
|
t.Fatal("Attempt counter should be closed") |
|
|
|
@ -66,15 +70,16 @@ func TestRetryTimerWithNoJitter(t *testing.T) { |
|
|
|
|
|
|
|
|
|
// Test retry time with Jitter greater than MaxJitter.
|
|
|
|
|
func TestRetryTimerWithJitter(t *testing.T) { |
|
|
|
|
doneCh := make(chan struct{}) |
|
|
|
|
rctx, cancel := context.WithCancel(context.Background()) |
|
|
|
|
|
|
|
|
|
// Jitter will be set back to 1.0
|
|
|
|
|
attemptCh := newRetryTimerWithJitter(defaultRetryUnit, defaultRetryCap, 2.0, doneCh) |
|
|
|
|
attemptCh := newRetryTimerWithJitter(rctx, time.Second, 30*time.Second, 2.0) |
|
|
|
|
i := <-attemptCh |
|
|
|
|
if i != 0 { |
|
|
|
|
close(doneCh) |
|
|
|
|
cancel() |
|
|
|
|
t.Fatalf("Invalid attempt counter returned should be 0, found %d instead", i) |
|
|
|
|
} |
|
|
|
|
close(doneCh) |
|
|
|
|
cancel() |
|
|
|
|
_, ok := <-attemptCh |
|
|
|
|
if ok { |
|
|
|
|
t.Fatal("Attempt counter should be closed") |
|
|
|
|