github.com/onsi/gomega@v1.32.0/matchers/be_sent_matcher_test.go (about) 1 package matchers_test 2 3 import ( 4 "time" 5 6 . "github.com/onsi/gomega/matchers" 7 8 . "github.com/onsi/ginkgo/v2" 9 . "github.com/onsi/gomega" 10 ) 11 12 var _ = Describe("BeSent", func() { 13 When("passed a channel and a matching type", func() { 14 When("the channel is ready to receive", func() { 15 It("should succeed and send the value down the channel", func() { 16 c := make(chan string) 17 d := make(chan string) 18 go func() { 19 val := <-c 20 d <- val 21 }() 22 23 time.Sleep(10 * time.Millisecond) 24 25 Expect(c).Should(BeSent("foo")) 26 Eventually(d).Should(Receive(Equal("foo"))) 27 }) 28 29 It("should succeed (with a buffered channel)", func() { 30 c := make(chan string, 1) 31 Expect(c).Should(BeSent("foo")) 32 Expect(<-c).Should(Equal("foo")) 33 }) 34 }) 35 36 When("the channel is not ready to receive", func() { 37 It("should fail and not send down the channel", func() { 38 c := make(chan string) 39 Expect(c).ShouldNot(BeSent("foo")) 40 Consistently(c).ShouldNot(Receive()) 41 }) 42 }) 43 44 When("the channel is eventually ready to receive", func() { 45 It("should succeed", func() { 46 c := make(chan string) 47 d := make(chan string) 48 go func() { 49 time.Sleep(30 * time.Millisecond) 50 val := <-c 51 d <- val 52 }() 53 54 Eventually(c).Should(BeSent("foo")) 55 Eventually(d).Should(Receive(Equal("foo"))) 56 }) 57 }) 58 59 When("the channel is closed", func() { 60 It("should error", func() { 61 c := make(chan string) 62 close(c) 63 success, err := (&BeSentMatcher{Arg: "foo"}).Match(c) 64 Expect(success).Should(BeFalse()) 65 Expect(err).Should(HaveOccurred()) 66 }) 67 68 It("should short-circuit Eventually", func() { 69 c := make(chan string) 70 close(c) 71 72 t := time.Now() 73 failures := InterceptGomegaFailures(func() { 74 Eventually(c, 10.0).Should(BeSent("foo")) 75 }) 76 Expect(failures).Should(HaveLen(1)) 77 Expect(time.Since(t)).Should(BeNumerically("<", time.Second)) 78 }) 79 }) 80 }) 81 82 When("passed a channel and a non-matching type", func() { 83 It("should error", func() { 84 success, err := (&BeSentMatcher{Arg: "foo"}).Match(make(chan int, 1)) 85 Expect(success).Should(BeFalse()) 86 Expect(err).Should(HaveOccurred()) 87 }) 88 }) 89 90 When("passed a receive-only channel", func() { 91 It("should error", func() { 92 var c <-chan string 93 c = make(chan string, 1) 94 success, err := (&BeSentMatcher{Arg: "foo"}).Match(c) 95 Expect(success).Should(BeFalse()) 96 Expect(err).Should(HaveOccurred()) 97 }) 98 }) 99 100 When("passed a nonchannel", func() { 101 It("should error", func() { 102 success, err := (&BeSentMatcher{Arg: "foo"}).Match("bar") 103 Expect(success).Should(BeFalse()) 104 Expect(err).Should(HaveOccurred()) 105 }) 106 }) 107 })