File: server_test.go

package info (click to toggle)
golang-ginkgo 1.2.0+git20161006.acfa16a-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 1,324 kB
  • ctags: 1,210
  • sloc: makefile: 12
file content (269 lines) | stat: -rw-r--r-- 7,545 bytes parent folder | download | duplicates (2)
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package remote_test

import (
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/ginkgo/internal/remote"
	. "github.com/onsi/gomega"

	"github.com/onsi/ginkgo/config"
	"github.com/onsi/ginkgo/reporters"
	"github.com/onsi/ginkgo/types"

	"bytes"
	"encoding/json"
	"net/http"
)

var _ = Describe("Server", func() {
	var (
		server *Server
	)

	BeforeEach(func() {
		var err error
		server, err = NewServer(3)
		Ω(err).ShouldNot(HaveOccurred())

		server.Start()
	})

	AfterEach(func() {
		server.Close()
	})

	Describe("Streaming endpoints", func() {
		var (
			reporterA, reporterB *reporters.FakeReporter
			forwardingReporter   *ForwardingReporter

			suiteSummary *types.SuiteSummary
			setupSummary *types.SetupSummary
			specSummary  *types.SpecSummary
		)

		BeforeEach(func() {
			reporterA = reporters.NewFakeReporter()
			reporterB = reporters.NewFakeReporter()

			server.RegisterReporters(reporterA, reporterB)

			forwardingReporter = NewForwardingReporter(server.Address(), &http.Client{}, &fakeOutputInterceptor{})

			suiteSummary = &types.SuiteSummary{
				SuiteDescription: "My Test Suite",
			}

			setupSummary = &types.SetupSummary{
				State: types.SpecStatePassed,
			}

			specSummary = &types.SpecSummary{
				ComponentTexts: []string{"My", "Spec"},
				State:          types.SpecStatePassed,
			}
		})

		It("should make its address available", func() {
			Ω(server.Address()).Should(MatchRegexp(`http://127.0.0.1:\d{2,}`))
		})

		Describe("/SpecSuiteWillBegin", func() {
			It("should decode and forward the Ginkgo config and suite summary", func(done Done) {
				forwardingReporter.SpecSuiteWillBegin(config.GinkgoConfig, suiteSummary)
				Ω(reporterA.Config).Should(Equal(config.GinkgoConfig))
				Ω(reporterB.Config).Should(Equal(config.GinkgoConfig))
				Ω(reporterA.BeginSummary).Should(Equal(suiteSummary))
				Ω(reporterB.BeginSummary).Should(Equal(suiteSummary))
				close(done)
			})
		})

		Describe("/BeforeSuiteDidRun", func() {
			It("should decode and forward the setup summary", func() {
				forwardingReporter.BeforeSuiteDidRun(setupSummary)
				Ω(reporterA.BeforeSuiteSummary).Should(Equal(setupSummary))
				Ω(reporterB.BeforeSuiteSummary).Should(Equal(setupSummary))
			})
		})

		Describe("/AfterSuiteDidRun", func() {
			It("should decode and forward the setup summary", func() {
				forwardingReporter.AfterSuiteDidRun(setupSummary)
				Ω(reporterA.AfterSuiteSummary).Should(Equal(setupSummary))
				Ω(reporterB.AfterSuiteSummary).Should(Equal(setupSummary))
			})
		})

		Describe("/SpecWillRun", func() {
			It("should decode and forward the spec summary", func(done Done) {
				forwardingReporter.SpecWillRun(specSummary)
				Ω(reporterA.SpecWillRunSummaries[0]).Should(Equal(specSummary))
				Ω(reporterB.SpecWillRunSummaries[0]).Should(Equal(specSummary))
				close(done)
			})
		})

		Describe("/SpecDidComplete", func() {
			It("should decode and forward the spec summary", func(done Done) {
				forwardingReporter.SpecDidComplete(specSummary)
				Ω(reporterA.SpecSummaries[0]).Should(Equal(specSummary))
				Ω(reporterB.SpecSummaries[0]).Should(Equal(specSummary))
				close(done)
			})
		})

		Describe("/SpecSuiteDidEnd", func() {
			It("should decode and forward the suite summary", func(done Done) {
				forwardingReporter.SpecSuiteDidEnd(suiteSummary)
				Ω(reporterA.EndSummary).Should(Equal(suiteSummary))
				Ω(reporterB.EndSummary).Should(Equal(suiteSummary))
				close(done)
			})
		})
	})

	Describe("Synchronization endpoints", func() {
		Describe("GETting and POSTing BeforeSuiteState", func() {
			getBeforeSuite := func() types.RemoteBeforeSuiteData {
				resp, err := http.Get(server.Address() + "/BeforeSuiteState")
				Ω(err).ShouldNot(HaveOccurred())
				Ω(resp.StatusCode).Should(Equal(http.StatusOK))

				r := types.RemoteBeforeSuiteData{}
				decoder := json.NewDecoder(resp.Body)
				err = decoder.Decode(&r)
				Ω(err).ShouldNot(HaveOccurred())

				return r
			}

			postBeforeSuite := func(r types.RemoteBeforeSuiteData) {
				resp, err := http.Post(server.Address()+"/BeforeSuiteState", "application/json", bytes.NewReader(r.ToJSON()))
				Ω(err).ShouldNot(HaveOccurred())
				Ω(resp.StatusCode).Should(Equal(http.StatusOK))
			}

			Context("when the first node's Alive has not been registered yet", func() {
				It("should return pending", func() {
					state := getBeforeSuite()
					Ω(state).Should(Equal(types.RemoteBeforeSuiteData{nil, types.RemoteBeforeSuiteStatePending}))

					state = getBeforeSuite()
					Ω(state).Should(Equal(types.RemoteBeforeSuiteData{nil, types.RemoteBeforeSuiteStatePending}))
				})
			})

			Context("when the first node is Alive but has not responded yet", func() {
				BeforeEach(func() {
					server.RegisterAlive(1, func() bool {
						return true
					})
				})

				It("should return pending", func() {
					state := getBeforeSuite()
					Ω(state).Should(Equal(types.RemoteBeforeSuiteData{nil, types.RemoteBeforeSuiteStatePending}))

					state = getBeforeSuite()
					Ω(state).Should(Equal(types.RemoteBeforeSuiteData{nil, types.RemoteBeforeSuiteStatePending}))
				})
			})

			Context("when the first node has responded", func() {
				var state types.RemoteBeforeSuiteData
				BeforeEach(func() {
					server.RegisterAlive(1, func() bool {
						return false
					})

					state = types.RemoteBeforeSuiteData{
						Data:  []byte("my data"),
						State: types.RemoteBeforeSuiteStatePassed,
					}
					postBeforeSuite(state)
				})

				It("should return the passed in state", func() {
					returnedState := getBeforeSuite()
					Ω(returnedState).Should(Equal(state))
				})
			})

			Context("when the first node is no longer Alive and has not responded yet", func() {
				BeforeEach(func() {
					server.RegisterAlive(1, func() bool {
						return false
					})
				})

				It("should return disappeared", func() {
					state := getBeforeSuite()
					Ω(state).Should(Equal(types.RemoteBeforeSuiteData{nil, types.RemoteBeforeSuiteStateDisappeared}))

					state = getBeforeSuite()
					Ω(state).Should(Equal(types.RemoteBeforeSuiteData{nil, types.RemoteBeforeSuiteStateDisappeared}))
				})
			})
		})

		Describe("GETting RemoteAfterSuiteData", func() {
			getRemoteAfterSuiteData := func() bool {
				resp, err := http.Get(server.Address() + "/RemoteAfterSuiteData")
				Ω(err).ShouldNot(HaveOccurred())
				Ω(resp.StatusCode).Should(Equal(http.StatusOK))

				a := types.RemoteAfterSuiteData{}
				decoder := json.NewDecoder(resp.Body)
				err = decoder.Decode(&a)
				Ω(err).ShouldNot(HaveOccurred())

				return a.CanRun
			}

			Context("when there are unregistered nodes", func() {
				BeforeEach(func() {
					server.RegisterAlive(2, func() bool {
						return false
					})
				})

				It("should return false", func() {
					Ω(getRemoteAfterSuiteData()).Should(BeFalse())
				})
			})

			Context("when all none-node-1 nodes are still running", func() {
				BeforeEach(func() {
					server.RegisterAlive(2, func() bool {
						return true
					})

					server.RegisterAlive(3, func() bool {
						return false
					})
				})

				It("should return false", func() {
					Ω(getRemoteAfterSuiteData()).Should(BeFalse())
				})
			})

			Context("when all none-1 nodes are done", func() {
				BeforeEach(func() {
					server.RegisterAlive(2, func() bool {
						return false
					})

					server.RegisterAlive(3, func() bool {
						return false
					})
				})

				It("should return true", func() {
					Ω(getRemoteAfterSuiteData()).Should(BeTrue())
				})

			})
		})
	})
})