summaryrefslogtreecommitdiff
path: root/vendor/github.com/onsi/ginkgo/internal/leafnodes/synchronized_before_suite_node_test.go
blob: 46c3e276b6318e3e542516e256f05b5d8db8f803 (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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
package leafnodes_test

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

	"net/http"

	"github.com/onsi/gomega/ghttp"

	"time"

	"github.com/onsi/ginkgo/internal/codelocation"
	Failer "github.com/onsi/ginkgo/internal/failer"
	"github.com/onsi/ginkgo/types"
)

var _ = Describe("SynchronizedBeforeSuiteNode", func() {
	var failer *Failer.Failer
	var node SuiteNode
	var codeLocation types.CodeLocation
	var innerCodeLocation types.CodeLocation
	var outcome bool
	var server *ghttp.Server

	BeforeEach(func() {
		server = ghttp.NewServer()
		codeLocation = codelocation.New(0)
		innerCodeLocation = codelocation.New(0)
		failer = Failer.New()
	})

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

	newNode := func(bodyA interface{}, bodyB interface{}) SuiteNode {
		return NewSynchronizedBeforeSuiteNode(bodyA, bodyB, codeLocation, time.Millisecond, failer)
	}

	Describe("when not running in parallel", func() {
		Context("when all is well", func() {
			var data []byte
			BeforeEach(func() {
				data = nil

				node = newNode(func() []byte {
					return []byte("my data")
				}, func(d []byte) {
					data = d
				})

				outcome = node.Run(1, 1, server.URL())
			})

			It("should run A, then B passing the output from A to B", func() {
				Ω(data).Should(Equal([]byte("my data")))
			})

			It("should report success", func() {
				Ω(outcome).Should(BeTrue())
				Ω(node.Passed()).Should(BeTrue())
				Ω(node.Summary().State).Should(Equal(types.SpecStatePassed))
			})
		})

		Context("when A fails", func() {
			var ranB bool
			BeforeEach(func() {
				ranB = false
				node = newNode(func() []byte {
					failer.Fail("boom", innerCodeLocation)
					return nil
				}, func([]byte) {
					ranB = true
				})

				outcome = node.Run(1, 1, server.URL())
			})

			It("should not run B", func() {
				Ω(ranB).Should(BeFalse())
			})

			It("should report failure", func() {
				Ω(outcome).Should(BeFalse())
				Ω(node.Passed()).Should(BeFalse())
				Ω(node.Summary().State).Should(Equal(types.SpecStateFailed))
			})
		})

		Context("when B fails", func() {
			BeforeEach(func() {
				node = newNode(func() []byte {
					return nil
				}, func([]byte) {
					failer.Fail("boom", innerCodeLocation)
				})

				outcome = node.Run(1, 1, server.URL())
			})

			It("should report failure", func() {
				Ω(outcome).Should(BeFalse())
				Ω(node.Passed()).Should(BeFalse())
				Ω(node.Summary().State).Should(Equal(types.SpecStateFailed))
			})
		})

		Context("when A times out", func() {
			var ranB bool
			BeforeEach(func() {
				ranB = false
				node = newNode(func(Done) []byte {
					time.Sleep(time.Second)
					return nil
				}, func([]byte) {
					ranB = true
				})

				outcome = node.Run(1, 1, server.URL())
			})

			It("should not run B", func() {
				Ω(ranB).Should(BeFalse())
			})

			It("should report failure", func() {
				Ω(outcome).Should(BeFalse())
				Ω(node.Passed()).Should(BeFalse())
				Ω(node.Summary().State).Should(Equal(types.SpecStateTimedOut))
			})
		})

		Context("when B times out", func() {
			BeforeEach(func() {
				node = newNode(func() []byte {
					return nil
				}, func([]byte, Done) {
					time.Sleep(time.Second)
				})

				outcome = node.Run(1, 1, server.URL())
			})

			It("should report failure", func() {
				Ω(outcome).Should(BeFalse())
				Ω(node.Passed()).Should(BeFalse())
				Ω(node.Summary().State).Should(Equal(types.SpecStateTimedOut))
			})
		})
	})

	Describe("when running in parallel", func() {
		var ranB bool
		var parallelNode, parallelTotal int
		BeforeEach(func() {
			ranB = false
			parallelNode, parallelTotal = 1, 3
		})

		Context("as the first node, it runs A", func() {
			var expectedState types.RemoteBeforeSuiteData

			BeforeEach(func() {
				parallelNode, parallelTotal = 1, 3
			})

			JustBeforeEach(func() {
				server.AppendHandlers(ghttp.CombineHandlers(
					ghttp.VerifyRequest("POST", "/BeforeSuiteState"),
					ghttp.VerifyJSONRepresenting(expectedState),
				))

				outcome = node.Run(parallelNode, parallelTotal, server.URL())
			})

			Context("when A succeeds", func() {
				BeforeEach(func() {
					expectedState = types.RemoteBeforeSuiteData{Data: []byte("my data"), State: types.RemoteBeforeSuiteStatePassed}

					node = newNode(func() []byte {
						return []byte("my data")
					}, func([]byte) {
						ranB = true
					})
				})

				It("should post about A succeeding", func() {
					Ω(server.ReceivedRequests()).Should(HaveLen(1))
				})

				It("should run B", func() {
					Ω(ranB).Should(BeTrue())
				})

				It("should report success", func() {
					Ω(outcome).Should(BeTrue())
				})
			})

			Context("when A fails", func() {
				BeforeEach(func() {
					expectedState = types.RemoteBeforeSuiteData{Data: nil, State: types.RemoteBeforeSuiteStateFailed}

					node = newNode(func() []byte {
						panic("BAM")
					}, func([]byte) {
						ranB = true
					})
				})

				It("should post about A failing", func() {
					Ω(server.ReceivedRequests()).Should(HaveLen(1))
				})

				It("should not run B", func() {
					Ω(ranB).Should(BeFalse())
				})

				It("should report failure", func() {
					Ω(outcome).Should(BeFalse())
				})
			})
		})

		Context("as the Nth node", func() {
			var statusCode int
			var response interface{}
			var ranA bool
			var bData []byte

			BeforeEach(func() {
				ranA = false
				bData = nil

				statusCode = http.StatusOK

				server.AppendHandlers(ghttp.CombineHandlers(
					ghttp.VerifyRequest("GET", "/BeforeSuiteState"),
					ghttp.RespondWith(http.StatusOK, string((types.RemoteBeforeSuiteData{Data: nil, State: types.RemoteBeforeSuiteStatePending}).ToJSON())),
				), ghttp.CombineHandlers(
					ghttp.VerifyRequest("GET", "/BeforeSuiteState"),
					ghttp.RespondWith(http.StatusOK, string((types.RemoteBeforeSuiteData{Data: nil, State: types.RemoteBeforeSuiteStatePending}).ToJSON())),
				), ghttp.CombineHandlers(
					ghttp.VerifyRequest("GET", "/BeforeSuiteState"),
					ghttp.RespondWithJSONEncodedPtr(&statusCode, &response),
				))

				node = newNode(func() []byte {
					ranA = true
					return nil
				}, func(data []byte) {
					bData = data
				})

				parallelNode, parallelTotal = 2, 3
			})

			Context("when A on node1 succeeds", func() {
				BeforeEach(func() {
					response = types.RemoteBeforeSuiteData{Data: []byte("my data"), State: types.RemoteBeforeSuiteStatePassed}
					outcome = node.Run(parallelNode, parallelTotal, server.URL())
				})

				It("should not run A", func() {
					Ω(ranA).Should(BeFalse())
				})

				It("should poll for A", func() {
					Ω(server.ReceivedRequests()).Should(HaveLen(3))
				})

				It("should run B when the polling succeeds", func() {
					Ω(bData).Should(Equal([]byte("my data")))
				})

				It("should succeed", func() {
					Ω(outcome).Should(BeTrue())
					Ω(node.Passed()).Should(BeTrue())
				})
			})

			Context("when A on node1 fails", func() {
				BeforeEach(func() {
					response = types.RemoteBeforeSuiteData{Data: []byte("my data"), State: types.RemoteBeforeSuiteStateFailed}
					outcome = node.Run(parallelNode, parallelTotal, server.URL())
				})

				It("should not run A", func() {
					Ω(ranA).Should(BeFalse())
				})

				It("should poll for A", func() {
					Ω(server.ReceivedRequests()).Should(HaveLen(3))
				})

				It("should not run B", func() {
					Ω(bData).Should(BeNil())
				})

				It("should fail", func() {
					Ω(outcome).Should(BeFalse())
					Ω(node.Passed()).Should(BeFalse())

					summary := node.Summary()
					Ω(summary.State).Should(Equal(types.SpecStateFailed))
					Ω(summary.Failure.Message).Should(Equal("BeforeSuite on Node 1 failed"))
					Ω(summary.Failure.Location).Should(Equal(codeLocation))
					Ω(summary.Failure.ComponentType).Should(Equal(types.SpecComponentTypeBeforeSuite))
					Ω(summary.Failure.ComponentIndex).Should(Equal(0))
					Ω(summary.Failure.ComponentCodeLocation).Should(Equal(codeLocation))
				})
			})

			Context("when node1 disappears", func() {
				BeforeEach(func() {
					response = types.RemoteBeforeSuiteData{Data: []byte("my data"), State: types.RemoteBeforeSuiteStateDisappeared}
					outcome = node.Run(parallelNode, parallelTotal, server.URL())
				})

				It("should not run A", func() {
					Ω(ranA).Should(BeFalse())
				})

				It("should poll for A", func() {
					Ω(server.ReceivedRequests()).Should(HaveLen(3))
				})

				It("should not run B", func() {
					Ω(bData).Should(BeNil())
				})

				It("should fail", func() {
					Ω(outcome).Should(BeFalse())
					Ω(node.Passed()).Should(BeFalse())

					summary := node.Summary()
					Ω(summary.State).Should(Equal(types.SpecStateFailed))
					Ω(summary.Failure.Message).Should(Equal("Node 1 disappeared before completing BeforeSuite"))
					Ω(summary.Failure.Location).Should(Equal(codeLocation))
					Ω(summary.Failure.ComponentType).Should(Equal(types.SpecComponentTypeBeforeSuite))
					Ω(summary.Failure.ComponentIndex).Should(Equal(0))
					Ω(summary.Failure.ComponentCodeLocation).Should(Equal(codeLocation))
				})
			})
		})
	})

	Describe("construction", func() {
		Describe("the first function", func() {
			Context("when the first function returns a byte array", func() {
				Context("and takes nothing", func() {
					It("should be fine", func() {
						Ω(func() {
							newNode(func() []byte { return nil }, func([]byte) {})
						}).ShouldNot(Panic())
					})
				})

				Context("and takes a done function", func() {
					It("should be fine", func() {
						Ω(func() {
							newNode(func(Done) []byte { return nil }, func([]byte) {})
						}).ShouldNot(Panic())
					})
				})

				Context("and takes more than one thing", func() {
					It("should panic", func() {
						Ω(func() {
							newNode(func(Done, Done) []byte { return nil }, func([]byte) {})
						}).Should(Panic())
					})
				})

				Context("and takes something else", func() {
					It("should panic", func() {
						Ω(func() {
							newNode(func(bool) []byte { return nil }, func([]byte) {})
						}).Should(Panic())
					})
				})
			})

			Context("when the first function does not return a byte array", func() {
				It("should panic", func() {
					Ω(func() {
						newNode(func() {}, func([]byte) {})
					}).Should(Panic())

					Ω(func() {
						newNode(func() []int { return nil }, func([]byte) {})
					}).Should(Panic())
				})
			})
		})

		Describe("the second function", func() {
			Context("when the second function takes a byte array", func() {
				It("should be fine", func() {
					Ω(func() {
						newNode(func() []byte { return nil }, func([]byte) {})
					}).ShouldNot(Panic())
				})
			})

			Context("when it also takes a done channel", func() {
				It("should be fine", func() {
					Ω(func() {
						newNode(func() []byte { return nil }, func([]byte, Done) {})
					}).ShouldNot(Panic())
				})
			})

			Context("if it takes anything else", func() {
				It("should panic", func() {
					Ω(func() {
						newNode(func() []byte { return nil }, func([]byte, chan bool) {})
					}).Should(Panic())

					Ω(func() {
						newNode(func() []byte { return nil }, func(string) {})
					}).Should(Panic())
				})
			})

			Context("if it takes nothing at all", func() {
				It("should panic", func() {
					Ω(func() {
						newNode(func() []byte { return nil }, func() {})
					}).Should(Panic())
				})
			})

			Context("if it returns something", func() {
				It("should panic", func() {
					Ω(func() {
						newNode(func() []byte { return nil }, func([]byte) []byte { return nil })
					}).Should(Panic())
				})
			})
		})
	})
})