github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/helper/resource/wait_test.go (about)

     1  package resource
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  	"time"
     7  )
     8  
     9  func TestRetry(t *testing.T) {
    10  	t.Parallel()
    11  
    12  	tries := 0
    13  	f := func() *RetryError {
    14  		tries++
    15  		if tries == 3 {
    16  			return nil
    17  		}
    18  
    19  		return RetryableError(fmt.Errorf("error"))
    20  	}
    21  
    22  	err := Retry(10*time.Second, f)
    23  	if err != nil {
    24  		t.Fatalf("err: %s", err)
    25  	}
    26  }
    27  
    28  func TestRetry_timeout(t *testing.T) {
    29  	t.Parallel()
    30  
    31  	f := func() *RetryError {
    32  		return RetryableError(fmt.Errorf("always"))
    33  	}
    34  
    35  	err := Retry(1*time.Second, f)
    36  	if err == nil {
    37  		t.Fatal("should error")
    38  	}
    39  }
    40  
    41  func TestRetry_hang(t *testing.T) {
    42  	t.Parallel()
    43  
    44  	f := func() *RetryError {
    45  		time.Sleep(2 * time.Second)
    46  		return nil
    47  	}
    48  
    49  	err := Retry(1*time.Second, f)
    50  	if err == nil {
    51  		t.Fatal("should error")
    52  	}
    53  }
    54  
    55  func TestRetry_error(t *testing.T) {
    56  	t.Parallel()
    57  
    58  	expected := fmt.Errorf("nope")
    59  	f := func() *RetryError {
    60  		return NonRetryableError(expected)
    61  	}
    62  
    63  	errCh := make(chan error)
    64  	go func() {
    65  		errCh <- Retry(1*time.Second, f)
    66  	}()
    67  
    68  	select {
    69  	case err := <-errCh:
    70  		if err != expected {
    71  			t.Fatalf("bad: %#v", err)
    72  		}
    73  	case <-time.After(5 * time.Second):
    74  		t.Fatal("timeout")
    75  	}
    76  }