1package diodes_test 2 3import ( 4 "context" 5 "time" 6 7 "code.cloudfoundry.org/go-diodes" 8 9 . "github.com/onsi/ginkgo" 10 . "github.com/onsi/gomega" 11) 12 13var _ = Describe("Waiter", func() { 14 var ( 15 spy *spyDiode 16 w *diodes.Waiter 17 ) 18 19 BeforeEach(func() { 20 spy = &spyDiode{} 21 w = diodes.NewWaiter(spy) 22 }) 23 24 It("returns the available result", func() { 25 spy.dataList = [][]byte{[]byte("a"), []byte("b")} 26 27 Expect(*(*[]byte)(w.Next())).To(Equal([]byte("a"))) 28 Expect(*(*[]byte)(w.Next())).To(Equal([]byte("b"))) 29 }) 30 31 It("waits until data is available", func() { 32 go func() { 33 time.Sleep(250 * time.Millisecond) 34 data := []byte("a") 35 w.Set(diodes.GenericDataType(&data)) 36 }() 37 38 Expect(*(*[]byte)(w.Next())).To(Equal([]byte("a"))) 39 }) 40 41 It("cancels Next() with context", func() { 42 ctx, cancel := context.WithCancel(context.Background()) 43 w = diodes.NewWaiter(spy, diodes.WithWaiterContext(ctx)) 44 cancel() 45 done := make(chan struct{}) 46 go func() { 47 defer close(done) 48 w.Next() 49 }() 50 51 Eventually(done).Should(BeClosed()) 52 }) 53 54 It("cancels current Next() with context", func() { 55 ctx, cancel := context.WithCancel(context.Background()) 56 w = diodes.NewWaiter(spy, diodes.WithWaiterContext(ctx)) 57 go func() { 58 time.Sleep(100 * time.Millisecond) 59 cancel() 60 }() 61 62 Expect(w.Next() == nil).To(BeTrue()) 63 }) 64}) 65