4 . "github.com/onsi/gomega/matchers"
7 . "github.com/onsi/ginkgo"
8 . "github.com/onsi/gomega"
11 var _ = Describe("BeSent", func() {
12 Context("when passed a channel and a matching type", func() {
13 Context("when the channel is ready to receive", func() {
14 It("should succeed and send the value down the channel", func() {
15 c := make(chan string)
16 d := make(chan string)
22 time.Sleep(10 * time.Millisecond)
24 Ω(c).Should(BeSent("foo"))
25 Eventually(d).Should(Receive(Equal("foo")))
28 It("should succeed (with a buffered channel)", func() {
29 c := make(chan string, 1)
30 Ω(c).Should(BeSent("foo"))
31 Ω(<-c).Should(Equal("foo"))
35 Context("when the channel is not ready to receive", func() {
36 It("should fail and not send down the channel", func() {
37 c := make(chan string)
38 Ω(c).ShouldNot(BeSent("foo"))
39 Consistently(c).ShouldNot(Receive())
43 Context("when the channel is eventually ready to receive", func() {
44 It("should succeed", func() {
45 c := make(chan string)
46 d := make(chan string)
48 time.Sleep(30 * time.Millisecond)
53 Eventually(c).Should(BeSent("foo"))
54 Eventually(d).Should(Receive(Equal("foo")))
58 Context("when the channel is closed", func() {
59 It("should error", func() {
60 c := make(chan string)
62 success, err := (&BeSentMatcher{Arg: "foo"}).Match(c)
63 Ω(success).Should(BeFalse())
64 Ω(err).Should(HaveOccurred())
67 It("should short-circuit Eventually", func() {
68 c := make(chan string)
72 failures := InterceptGomegaFailures(func() {
73 Eventually(c, 10.0).Should(BeSent("foo"))
75 Ω(failures).Should(HaveLen(1))
76 Ω(time.Since(t)).Should(BeNumerically("<", time.Second))
81 Context("when passed a channel and a non-matching type", func() {
82 It("should error", func() {
83 success, err := (&BeSentMatcher{Arg: "foo"}).Match(make(chan int, 1))
84 Ω(success).Should(BeFalse())
85 Ω(err).Should(HaveOccurred())
89 Context("when passed a receive-only channel", func() {
90 It("should error", func() {
92 c = make(chan string, 1)
93 success, err := (&BeSentMatcher{Arg: "foo"}).Match(c)
94 Ω(success).Should(BeFalse())
95 Ω(err).Should(HaveOccurred())
99 Context("when passed a nonchannel", func() {
100 It("should error", func() {
101 success, err := (&BeSentMatcher{Arg: "foo"}).Match("bar")
102 Ω(success).Should(BeFalse())
103 Ω(err).Should(HaveOccurred())