github.com/haraldrudell/parl@v0.4.176/cyclic-awaitable_test.go (about) 1 /* 2 © 2023-present Harald Rudell <haraldrudell@proton.me> (https://haraldrudell.github.io/haraldrudell/) 3 All rights reserved 4 */ 5 6 package parl 7 8 import ( 9 "testing" 10 ) 11 12 func TestCyclicAwaitable(t *testing.T) { 13 14 var ch, ch2 AwaitableCh 15 var isClosed, didClose, didOpen bool 16 17 // Ch() Close() IsClosed() Open() 18 var cyclicAwaitable CyclicAwaitable 19 var reset = func() { 20 cyclicAwaitable = CyclicAwaitable{} 21 } 22 23 // Ch should return open channel 24 reset() 25 ch = cyclicAwaitable.Ch() 26 if ch == nil { 27 t.Errorf("ch nil") 28 } 29 select { 30 case <-ch: 31 isClosed = true 32 default: 33 isClosed = false 34 } 35 if isClosed { 36 t.Errorf("ch closed") 37 } 38 39 // IsClosed should be false 40 reset() 41 isClosed = cyclicAwaitable.IsClosed() 42 if isClosed { 43 t.Errorf("IsClosed() true") 44 } 45 46 // Open should open 47 reset() 48 didClose = cyclicAwaitable.Close() 49 _ = didClose 50 didOpen, ch = cyclicAwaitable.Open() 51 if !didOpen { 52 t.Errorf("didOpen false") 53 } 54 if ch == nil { 55 t.Errorf("ch nil") 56 } 57 select { 58 case <-ch: 59 isClosed = true 60 default: 61 isClosed = false 62 } 63 if isClosed { 64 t.Errorf("ch not open") 65 } 66 isClosed = cyclicAwaitable.IsClosed() 67 if isClosed { 68 t.Errorf("IsClosed() true") 69 } 70 didOpen, ch2 = cyclicAwaitable.Open() 71 if didOpen { 72 t.Errorf("didOpen true") 73 } 74 if ch2 != ch { 75 t.Errorf("not same channel") 76 } 77 78 // Close should close 79 reset() 80 didClose = cyclicAwaitable.Close() 81 if !didClose { 82 t.Errorf("didClose false") 83 } 84 isClosed = cyclicAwaitable.IsClosed() 85 if !isClosed { 86 t.Errorf("IsClosed() false") 87 } 88 ch = cyclicAwaitable.Ch() 89 select { 90 case <-ch: 91 isClosed = true 92 default: 93 isClosed = false 94 } 95 if !isClosed { 96 t.Errorf("ch not closed") 97 } 98 didClose = cyclicAwaitable.Close() 99 if didClose { 100 t.Errorf("didClose true") 101 } 102 }