github.com/bigkraig/terraform@v0.6.4-0.20151219155159-c90d1b074e31/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() error { 14 tries++ 15 if tries == 1 { 16 return nil 17 } 18 19 return fmt.Errorf("error") 20 } 21 22 err := Retry(2*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() error { 32 return 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() error { 46 return RetryError{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 }