...

Source file src/cloud.google.com/go/internal/testutil/retry_test.go

Documentation: cloud.google.com/go/internal/testutil

     1  // Copyright 2019 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     https://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package testutil
    16  
    17  import (
    18  	"testing"
    19  	"time"
    20  )
    21  
    22  func TestRetry(t *testing.T) {
    23  	Retry(t, 5, time.Millisecond, func(r *R) {
    24  		if r.Attempt == 2 {
    25  			return
    26  		}
    27  		r.Fail()
    28  	})
    29  }
    30  
    31  func TestRetryAttempts(t *testing.T) {
    32  	var attempts int
    33  	Retry(t, 10, time.Millisecond, func(r *R) {
    34  		r.Logf("This line should appear only once.")
    35  		r.Logf("attempt=%d", r.Attempt)
    36  		attempts = r.Attempt
    37  
    38  		// Retry 5 times.
    39  		if r.Attempt == 5 {
    40  			return
    41  		}
    42  		r.Fail()
    43  	})
    44  
    45  	if attempts != 5 {
    46  		t.Errorf("attempts=%d; want %d", attempts, 5)
    47  	}
    48  }
    49  
    50  func TestRetryWithoutTest(t *testing.T) {
    51  	RetryWithoutTest(5, time.Millisecond, func(r *R) {
    52  		if r.Attempt == 2 {
    53  			return
    54  		}
    55  		r.Fail()
    56  	})
    57  }
    58  
    59  func TestRetryWithoutTestAttempts(t *testing.T) {
    60  	var attempts int
    61  	RetryWithoutTest(10, time.Millisecond, func(r *R) {
    62  		r.Logf("This line should appear only once.")
    63  		r.Logf("attempt=%d", r.Attempt)
    64  		attempts = r.Attempt
    65  
    66  		// Retry 5 times.
    67  		if r.Attempt == 5 {
    68  			return
    69  		}
    70  		r.Fail()
    71  	})
    72  
    73  	if attempts != 5 {
    74  		t.Errorf("attempts=%d; want %d", attempts, 5)
    75  	}
    76  }
    77  

View as plain text