github.com/wikibal01/hashicorp-terraform@v0.11.12-beta1/helper/signalwrapper/wrapper_test.go (about) 1 package signalwrapper 2 3 import ( 4 "errors" 5 "testing" 6 "time" 7 ) 8 9 func TestWrapped_goodCh(t *testing.T) { 10 errVal := errors.New("hi") 11 f := func(<-chan struct{}) error { return errVal } 12 err := <-Run(f).ErrCh 13 if err != errVal { 14 t.Fatalf("bad: %#v", err) 15 } 16 } 17 18 func TestWrapped_goodWait(t *testing.T) { 19 errVal := errors.New("hi") 20 f := func(<-chan struct{}) error { 21 time.Sleep(10 * time.Millisecond) 22 return errVal 23 } 24 25 wrapped := Run(f) 26 27 // Once 28 { 29 err := wrapped.Wait() 30 if err != errVal { 31 t.Fatalf("bad: %#v", err) 32 } 33 } 34 35 // Again 36 { 37 err := wrapped.Wait() 38 if err != errVal { 39 t.Fatalf("bad: %#v", err) 40 } 41 } 42 } 43 44 func TestWrapped_cancel(t *testing.T) { 45 errVal := errors.New("hi") 46 f := func(ch <-chan struct{}) error { 47 <-ch 48 return errVal 49 } 50 51 wrapped := Run(f) 52 53 // Once 54 { 55 err := wrapped.Cancel() 56 if err != errVal { 57 t.Fatalf("bad: %#v", err) 58 } 59 } 60 61 // Again 62 { 63 err := wrapped.Cancel() 64 if err != errVal { 65 t.Fatalf("bad: %#v", err) 66 } 67 } 68 } 69 70 func TestWrapped_waitAndCancel(t *testing.T) { 71 errVal := errors.New("hi") 72 readyCh := make(chan struct{}) 73 f := func(ch <-chan struct{}) error { 74 <-ch 75 <-readyCh 76 return errVal 77 } 78 79 wrapped := Run(f) 80 81 // Run both cancel and wait and wait some time to hope they're 82 // scheduled. We could _ensure_ both are scheduled by using some 83 // more lines of code but this is probably just good enough. 84 go wrapped.Cancel() 85 go wrapped.Wait() 86 close(readyCh) 87 time.Sleep(10 * time.Millisecond) 88 89 // Check it by calling Cancel again 90 err := wrapped.Cancel() 91 if err != errVal { 92 t.Fatalf("bad: %#v", err) 93 } 94 }