github.com/jpedro/terraform@v0.11.12-beta1/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 // make sure a slow StateRefreshFunc is allowed to complete after timeout 29 func TestRetry_grace(t *testing.T) { 30 t.Parallel() 31 32 f := func() *RetryError { 33 time.Sleep(1 * time.Second) 34 return nil 35 } 36 37 err := Retry(10*time.Millisecond, f) 38 if err != nil { 39 t.Fatalf("err: %s", err) 40 } 41 } 42 43 func TestRetry_timeout(t *testing.T) { 44 t.Parallel() 45 46 f := func() *RetryError { 47 return RetryableError(fmt.Errorf("always")) 48 } 49 50 err := Retry(1*time.Second, f) 51 if err == nil { 52 t.Fatal("should error") 53 } 54 } 55 56 func TestRetry_hang(t *testing.T) { 57 old := refreshGracePeriod 58 refreshGracePeriod = 50 * time.Millisecond 59 defer func() { 60 refreshGracePeriod = old 61 }() 62 63 f := func() *RetryError { 64 time.Sleep(2 * time.Second) 65 return nil 66 } 67 68 err := Retry(50*time.Millisecond, f) 69 if err == nil { 70 t.Fatal("should error") 71 } 72 } 73 74 func TestRetry_error(t *testing.T) { 75 t.Parallel() 76 77 expected := fmt.Errorf("nope") 78 f := func() *RetryError { 79 return NonRetryableError(expected) 80 } 81 82 errCh := make(chan error) 83 go func() { 84 errCh <- Retry(1*time.Second, f) 85 }() 86 87 select { 88 case err := <-errCh: 89 if err != expected { 90 t.Fatalf("bad: %#v", err) 91 } 92 case <-time.After(5 * time.Second): 93 t.Fatal("timeout") 94 } 95 }