github.com/serbaut/terraform@v0.6.12-0.20160607213102-ac2d195cc560/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_error(t *testing.T) { 42 t.Parallel() 43 44 expected := fmt.Errorf("nope") 45 f := func() *RetryError { 46 return NonRetryableError(expected) 47 } 48 49 errCh := make(chan error) 50 go func() { 51 errCh <- Retry(1*time.Second, f) 52 }() 53 54 select { 55 case err := <-errCh: 56 if err != expected { 57 t.Fatalf("bad: %#v", err) 58 } 59 case <-time.After(5 * time.Second): 60 t.Fatal("timeout") 61 } 62 }