forked from cloudfoundry/consuladapter
/
watch_test.go
117 lines (85 loc) · 2.75 KB
/
watch_test.go
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
108
109
110
111
112
113
114
115
116
117
package consuladapter_test
import (
"time"
"github.com/cloudfoundry-incubator/consuladapter"
"github.com/hashicorp/consul/api"
"github.com/pivotal-golang/lager/lagertest"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Watching", func() {
var client *api.Client
var logger *lagertest.TestLogger
var disappearChan <-chan []string
BeforeEach(func() {
logger = lagertest.NewTestLogger("test")
startClusterAndSession()
})
AfterEach(stopClusterAndSession)
Context("when the watch starts first", func() {
BeforeEach(func() {
var err error
client = clusterRunner.NewClient()
disappearChan = session.WatchForDisappearancesUnder(logger, "under")
Expect(err).NotTo(HaveOccurred())
})
Context("when there are keys", func() {
var bsession *consuladapter.Session
BeforeEach(func() {
bsession = clusterRunner.NewSession("bsession")
})
AfterEach(func() {
bsession.Destroy()
})
It("detects removals of keys", func() {
_, err := bsession.SetPresence("under/here", []byte("value"))
Expect(err).NotTo(HaveOccurred())
bsession.Destroy()
Eventually(disappearChan).Should(Receive(Equal([]string{"under/here"})))
})
Context("with other prefixes", func() {
BeforeEach(func() {
_, err := bsession.SetPresence("other", []byte("value"))
Expect(err).NotTo(HaveOccurred())
})
It("does not detect removal of keys under other prefixes", func() {
bsession.Destroy()
Consistently(disappearChan).ShouldNot(Receive())
})
})
Context("when destroying the session", func() {
It("closes the disappearance channel", func() {
session.Destroy()
Eventually(disappearChan, 15).Should(BeClosed())
})
})
Context("when an error occurs", func() {
It("retries", func() {
stopCluster()
Consistently(disappearChan).ShouldNot(Receive())
startCluster()
time.Sleep(1 * time.Second) // allow the watch to retry
bsession = clusterRunner.NewSession("bession")
_, err := bsession.SetPresence("under/here", []byte("value"))
Expect(err).NotTo(HaveOccurred())
bsession.Destroy()
Eventually(disappearChan).Should(Receive(Equal([]string{"under/here"})))
})
})
})
})
Context("when the watch starts later", func() {
var bsession *consuladapter.Session
BeforeEach(func() {
bsession = clusterRunner.NewSession("bsession")
_, err := bsession.SetPresence("under/here", []byte("value"))
Expect(err).NotTo(HaveOccurred())
})
It("detects removals of keys", func() {
disappearChan := session.WatchForDisappearancesUnder(logger, "under")
time.Sleep(1 * time.Second) // allow the watch to retry
bsession.Destroy()
Eventually(disappearChan).Should(Receive(Equal([]string{"under/here"})))
})
})
})