summaryrefslogtreecommitdiff
path: root/vendor/github.com/onsi/gomega/matchers/be_sent_matcher_test.go
blob: 68ec72879d5672ba707de2172bae4066072ac2d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package matchers_test

import (
	"time"

	. "github.com/onsi/gomega/matchers"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ = Describe("BeSent", func() {
	Context("when passed a channel and a matching type", func() {
		Context("when the channel is ready to receive", func() {
			It("should succeed and send the value down the channel", func() {
				c := make(chan string)
				d := make(chan string)
				go func() {
					val := <-c
					d <- val
				}()

				time.Sleep(10 * time.Millisecond)

				Expect(c).Should(BeSent("foo"))
				Eventually(d).Should(Receive(Equal("foo")))
			})

			It("should succeed (with a buffered channel)", func() {
				c := make(chan string, 1)
				Expect(c).Should(BeSent("foo"))
				Expect(<-c).Should(Equal("foo"))
			})
		})

		Context("when the channel is not ready to receive", func() {
			It("should fail and not send down the channel", func() {
				c := make(chan string)
				Expect(c).ShouldNot(BeSent("foo"))
				Consistently(c).ShouldNot(Receive())
			})
		})

		Context("when the channel is eventually ready to receive", func() {
			It("should succeed", func() {
				c := make(chan string)
				d := make(chan string)
				go func() {
					time.Sleep(30 * time.Millisecond)
					val := <-c
					d <- val
				}()

				Eventually(c).Should(BeSent("foo"))
				Eventually(d).Should(Receive(Equal("foo")))
			})
		})

		Context("when the channel is closed", func() {
			It("should error", func() {
				c := make(chan string)
				close(c)
				success, err := (&BeSentMatcher{Arg: "foo"}).Match(c)
				Expect(success).Should(BeFalse())
				Expect(err).Should(HaveOccurred())
			})

			It("should short-circuit Eventually", func() {
				c := make(chan string)
				close(c)

				t := time.Now()
				failures := InterceptGomegaFailures(func() {
					Eventually(c, 10.0).Should(BeSent("foo"))
				})
				Expect(failures).Should(HaveLen(1))
				Expect(time.Since(t)).Should(BeNumerically("<", time.Second))
			})
		})
	})

	Context("when passed a channel and a non-matching type", func() {
		It("should error", func() {
			success, err := (&BeSentMatcher{Arg: "foo"}).Match(make(chan int, 1))
			Expect(success).Should(BeFalse())
			Expect(err).Should(HaveOccurred())
		})
	})

	Context("when passed a receive-only channel", func() {
		It("should error", func() {
			var c <-chan string
			c = make(chan string, 1)
			success, err := (&BeSentMatcher{Arg: "foo"}).Match(c)
			Expect(success).Should(BeFalse())
			Expect(err).Should(HaveOccurred())
		})
	})

	Context("when passed a nonchannel", func() {
		It("should error", func() {
			success, err := (&BeSentMatcher{Arg: "foo"}).Match("bar")
			Expect(success).Should(BeFalse())
			Expect(err).Should(HaveOccurred())
		})
	})
})