205d71f405fc8d8f4a2b898bad9c1b8607898f7d
[govpp.git] / vendor / github.com / onsi / gomega / matchers / be_sent_matcher_test.go
1 package matchers_test
2
3 import (
4         . "github.com/onsi/gomega/matchers"
5         "time"
6
7         . "github.com/onsi/ginkgo"
8         . "github.com/onsi/gomega"
9 )
10
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)
17                                 go func() {
18                                         val := <-c
19                                         d <- val
20                                 }()
21
22                                 time.Sleep(10 * time.Millisecond)
23
24                                 Ω(c).Should(BeSent("foo"))
25                                 Eventually(d).Should(Receive(Equal("foo")))
26                         })
27
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"))
32                         })
33                 })
34
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())
40                         })
41                 })
42
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)
47                                 go func() {
48                                         time.Sleep(30 * time.Millisecond)
49                                         val := <-c
50                                         d <- val
51                                 }()
52
53                                 Eventually(c).Should(BeSent("foo"))
54                                 Eventually(d).Should(Receive(Equal("foo")))
55                         })
56                 })
57
58                 Context("when the channel is closed", func() {
59                         It("should error", func() {
60                                 c := make(chan string)
61                                 close(c)
62                                 success, err := (&BeSentMatcher{Arg: "foo"}).Match(c)
63                                 Ω(success).Should(BeFalse())
64                                 Ω(err).Should(HaveOccurred())
65                         })
66
67                         It("should short-circuit Eventually", func() {
68                                 c := make(chan string)
69                                 close(c)
70
71                                 t := time.Now()
72                                 failures := InterceptGomegaFailures(func() {
73                                         Eventually(c, 10.0).Should(BeSent("foo"))
74                                 })
75                                 Ω(failures).Should(HaveLen(1))
76                                 Ω(time.Since(t)).Should(BeNumerically("<", time.Second))
77                         })
78                 })
79         })
80
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())
86                 })
87         })
88
89         Context("when passed a receive-only channel", func() {
90                 It("should error", func() {
91                         var c <-chan string
92                         c = make(chan string, 1)
93                         success, err := (&BeSentMatcher{Arg: "foo"}).Match(c)
94                         Ω(success).Should(BeFalse())
95                         Ω(err).Should(HaveOccurred())
96                 })
97         })
98
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())
104                 })
105         })
106 })