fix broken retry tests

master
Harshavardhana 5 years ago
parent b768645fde
commit 6c62b1a2ea
  1. 33
      cmd/retry_test.go
  2. 4
      pkg/dsync/retry_test.go

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

@ -57,9 +57,11 @@ func TestRetryTimerWithNoJitter(t *testing.T) {
// Loop through the maximum possible attempt. // Loop through the maximum possible attempt.
for i = range attemptCh { for i = range attemptCh {
if i == 30 { if i == 30 {
cancel() break
} }
} }
cancel()
_, ok := <-attemptCh _, ok := <-attemptCh
if ok { if ok {
t.Fatal("Attempt counter should be closed") t.Fatal("Attempt counter should be closed")

Loading…
Cancel
Save