github.com/rkt/rkt@v1.30.1-0.20200224141603-171c416fac02/pkg/pod/wait_test.go (about)

     1  // Copyright 2016 The rkt Authors
     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  //     http://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 pod
    16  
    17  import (
    18  	"testing"
    19  	"time"
    20  
    21  	"golang.org/x/net/context"
    22  )
    23  
    24  func TestRetryFailure(t *testing.T) {
    25  	i := 0
    26  	f := func() bool { i++; return false } // never succeed
    27  
    28  	if err := retry(newTimeout(100*time.Millisecond), f, 10*time.Millisecond); err == nil {
    29  		t.Error("expected failure, but got none")
    30  		return
    31  	}
    32  
    33  	if i == 0 {
    34  		t.Error("expected some retries, but got none")
    35  		return
    36  	}
    37  }
    38  
    39  func TestRetrySuccess(t *testing.T) {
    40  	f := func() bool { return true } // always succeed
    41  
    42  	if err := retry(newTimeout(5*time.Second), f, 10*time.Millisecond); err != nil {
    43  		t.Errorf("expected success, but got %v", err)
    44  	}
    45  
    46  	i := 0
    47  	f = func() bool { i++; return i > 5 } // succeed after 5 times
    48  
    49  	if err := retry(newTimeout(5*time.Second), f, 10*time.Millisecond); err != nil {
    50  		t.Errorf("expected success, but got %v", err)
    51  	}
    52  }
    53  
    54  func newTimeout(t time.Duration) context.Context {
    55  	ctx, _ := context.WithTimeout(context.Background(), t) // should never be reached
    56  	return ctx
    57  }