File: ordering_test.go

package info (click to toggle)
golang-github-onsi-ginkgo-v2 2.15.0-1~bpo12%2B1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-backports
  • size: 4,112 kB
  • sloc: javascript: 59; sh: 14; makefile: 7
file content (365 lines) | stat: -rw-r--r-- 13,332 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
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
package internal_test

import (
	"strings"
	"time"

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

	"github.com/onsi/ginkgo/v2/internal"
	"github.com/onsi/ginkgo/v2/types"
)

type SpecTexts []string

func getTexts(specs Specs, groupedSpecIndices internal.GroupedSpecIndices) SpecTexts {
	out := []string{}
	for _, specIndices := range groupedSpecIndices {
		for _, idx := range specIndices {
			out = append(out, specs[idx].Text())
		}
	}
	return out
}

func (tt SpecTexts) Join() string {
	return strings.Join(tt, "")
}

var _ = Describe("OrderSpecs", func() {
	var conf types.SuiteConfig
	var specs Specs

	BeforeEach(func() {
		conf = types.SuiteConfig{}
		conf.RandomSeed = 1
		conf.ParallelTotal = 1

		con1 := N(ntCon)
		con2 := N(ntCon)
		specs = Specs{
			S(N("A", ntIt)),
			S(N("B", ntIt)),
			S(con1, N("C", ntIt)),
			S(con1, N("D", ntIt)),
			S(con1, N(ntCon), N("E", ntIt)),
			S(N("F", ntIt)),
			S(con2, N("G", ntIt)),
			S(con2, N("H", ntIt)),
		}
	})

	Context("when configured to only randomize top-level specs", func() {
		It("shuffles top level specs only", func() {
			for conf.RandomSeed = 1; conf.RandomSeed < 10; conf.RandomSeed += 1 {
				groupedSpecIndices, serialSpecIndices := internal.OrderSpecs(specs, conf)
				Ω(serialSpecIndices).Should(BeEmpty())

				Ω(getTexts(specs, groupedSpecIndices).Join()).Should(ContainSubstring("CDE"))
				Ω(getTexts(specs, groupedSpecIndices).Join()).Should(ContainSubstring("GH"))
			}

			conf.RandomSeed = 1
			groupedSpecIndices1, _ := internal.OrderSpecs(specs, conf)
			conf.RandomSeed = 2
			groupedSpecIndices2, _ := internal.OrderSpecs(specs, conf)
			Ω(getTexts(specs, groupedSpecIndices1)).ShouldNot(Equal(getTexts(specs, groupedSpecIndices2)))
		})
	})

	Context("when configured to randomize all specs", func() {
		BeforeEach(func() {
			conf.RandomizeAllSpecs = true
		})

		It("shuffles all specs", func() {
			hasCDE := true
			hasGH := true
			for conf.RandomSeed = 1; conf.RandomSeed < 10; conf.RandomSeed += 1 {
				groupedSpecIndices, serialSpecIndices := internal.OrderSpecs(specs, conf)
				Ω(serialSpecIndices).Should(BeEmpty())

				hasCDE, _ = ContainSubstring("CDE").Match(getTexts(specs, groupedSpecIndices).Join())
				hasGH, _ = ContainSubstring("GH").Match(getTexts(specs, groupedSpecIndices).Join())
				if !hasCDE && !hasGH {
					break
				}
			}

			Ω(hasCDE || hasGH).Should(BeFalse(), "after 10 randomizations, we really shouldn't have gotten CDE and GH in order as all specs should be shuffled, not just top-level containers and specs")

			conf.RandomSeed = 1
			groupedSpecIndices1, _ := internal.OrderSpecs(specs, conf)
			conf.RandomSeed = 2
			groupedSpecIndices2, _ := internal.OrderSpecs(specs, conf)
			Ω(getTexts(specs, groupedSpecIndices1)).ShouldNot(Equal(getTexts(specs, groupedSpecIndices2)))
		})
	})

	Context("when passed the same seed", func() {
		It("always generates the same order", func() {
			for _, conf.RandomizeAllSpecs = range []bool{true, false} {
				for conf.RandomSeed = 1; conf.RandomSeed < 10; conf.RandomSeed += 1 {
					groupedSpecIndices, serialSpecIndices := internal.OrderSpecs(specs, conf)
					Ω(serialSpecIndices).Should(BeEmpty())
					for i := 0; i < 10; i++ {
						reshuffledGroupedSpecIndices, serialSpecIndices := internal.OrderSpecs(specs, conf)
						Ω(serialSpecIndices).Should(BeEmpty())

						Ω(getTexts(specs, groupedSpecIndices)).Should(Equal(getTexts(specs, reshuffledGroupedSpecIndices)))
					}
				}
			}
		})
	})

	Context("when specs are in different files and the files are loaded in an undefined order", func() {
		var specsInFileA, specsInFileB Specs
		BeforeEach(func() {
			con1 := N(ntCon, CL("file_A", 10))
			specsInFileA = Specs{
				S(N("A", ntIt, CL("file_A", 1))),
				S(N("B", ntIt, CL("file_A", 5))),
				S(con1, N("C", ntIt, CL("file_A", 15))),
				S(con1, N("D", ntIt, CL("file_A", 20))),
				S(con1, N(ntCon, CL("file_A", 25)), N("E", ntIt, CL("file_A", 30))),
			}

			con2 := N(ntCon, CL("file_B", 10))
			specsInFileB = Specs{
				S(N("F", ntIt, CL("file_B", 1))),
				S(con2, N("G", ntIt, CL("file_B", 15))),
				S(con2, N("H", ntIt, CL("file_B", 20))),
			}
		})

		It("always generates a consistent randomization when given the same seed", func() {
			for _, conf.RandomizeAllSpecs = range []bool{true, false} {
				for conf.RandomSeed = 1; conf.RandomSeed < 10; conf.RandomSeed += 1 {
					specsOrderAB := Specs{}
					specsOrderAB = append(specsOrderAB, specsInFileA...)
					specsOrderAB = append(specsOrderAB, specsInFileB...)

					specsOrderBA := Specs{}
					specsOrderBA = append(specsOrderBA, specsInFileB...)
					specsOrderBA = append(specsOrderBA, specsInFileA...)

					groupedSpecIndicesAB, serialSpecIndices := internal.OrderSpecs(specsOrderAB, conf)
					Ω(serialSpecIndices).Should(BeEmpty())

					groupedSpecIndicesBA, serialSpecIndices := internal.OrderSpecs(specsOrderBA, conf)
					Ω(serialSpecIndices).Should(BeEmpty())

					Ω(getTexts(specsOrderAB, groupedSpecIndicesAB)).Should(Equal(getTexts(specsOrderBA, groupedSpecIndicesBA)))
				}
			}
		})
	})

	Context("when there are ordered specs and randomize-all is true", func() {
		BeforeEach(func() {
			con1 := N(ntCon, Ordered)
			con2 := N(ntCon)
			specs = Specs{
				S(N("A", ntIt)),
				S(N("B", ntIt)),
				S(con1, N("C", ntIt)),
				S(con1, N("D", ntIt)),
				S(con1, N(ntCon), N("E", ntIt)),
				S(N("F", ntIt)),
				S(con2, N("G", ntIt)),
				S(con2, N("H", ntIt)),
			}

			conf.RandomizeAllSpecs = true
		})

		It("never shuffles the specs in ordered specs", func() {
			for conf.RandomSeed = 1; conf.RandomSeed < 10; conf.RandomSeed += 1 {
				groupedSpecIndices, serialSpecIndices := internal.OrderSpecs(specs, conf)
				Ω(serialSpecIndices).Should(BeEmpty())

				Ω(getTexts(specs, groupedSpecIndices).Join()).Should(ContainSubstring("CDE"))
			}
		})
	})

	Context("when there are ordered specs and randomize-all is false and everything is in an enclosing container", func() {
		BeforeEach(func() {
			con0 := N(ntCon, CL(1))
			con1 := N(ntCon, Ordered, CL(4))
			con2 := N(ntCon, CL(10))
			specs = Specs{
				S(con0, N("A", ntIt, CL(2))),
				S(con0, N("B", ntIt, CL(3))),
				S(con0, con1, N("C", ntIt, CL(5))),
				S(con0, con1, N("D", ntIt, CL(6))),
				S(con0, con1, N(ntCon, CL(7)), N("E", ntIt, CL(8))),
				S(con0, N("F", ntIt, CL(9))),
				S(con0, con2, N("G", ntIt, CL(11))),
				S(con0, con2, N("H", ntIt, CL(12))),
			}

			conf.RandomizeAllSpecs = false
		})

		It("runs all the specs in order", func() {
			for conf.RandomSeed = 1; conf.RandomSeed < 10; conf.RandomSeed += 1 {
				groupedSpecIndices, serialSpecIndices := internal.OrderSpecs(specs, conf)
				Ω(serialSpecIndices).Should(BeEmpty())

				Ω(getTexts(specs, groupedSpecIndices).Join()).Should(Equal("ABCDEFGH"))
			}
		})
	})

	Context("when there are serial specs", func() {
		BeforeEach(func() {
			con1 := N(ntCon, Ordered, Serial)
			con2 := N(ntCon)
			specs = Specs{
				S(N("A", Serial, ntIt)),
				S(N("B", ntIt)),
				S(con1, N("C", ntIt)),
				S(con1, N("D", ntIt)),
				S(con1, N(ntCon), N("E", ntIt)),
				S(N("F", ntIt)),
				S(con2, N("G", ntIt)),
				S(con2, N("H", ntIt, Serial)),
			}
			conf.RandomizeAllSpecs = true
		})

		Context("and the tests are not running in parallel", func() {
			BeforeEach(func() {
				conf.ParallelTotal = 1
			})

			It("puts all the tests in the parallelizable group and returns an empty serial group", func() {
				for conf.RandomSeed = 1; conf.RandomSeed < 10; conf.RandomSeed += 1 {
					groupedSpecIndices, serialSpecIndices := internal.OrderSpecs(specs, conf)
					Ω(serialSpecIndices).Should(BeEmpty())

					Ω(getTexts(specs, groupedSpecIndices).Join()).Should(ContainSubstring("CDE"))
					Ω(getTexts(specs, groupedSpecIndices)).Should(ConsistOf("A", "B", "C", "D", "E", "F", "G", "H"))
				}

				conf.RandomSeed = 1
				groupedSpecIndices1, _ := internal.OrderSpecs(specs, conf)
				conf.RandomSeed = 2
				groupedSpecIndices2, _ := internal.OrderSpecs(specs, conf)
				Ω(getTexts(specs, groupedSpecIndices1)).ShouldNot(Equal(getTexts(specs, groupedSpecIndices2)))
			})
		})

		Context("and the tests are running in parallel", func() {
			BeforeEach(func() {
				conf.ParallelTotal = 2
			})

			It("puts all parallelizable tests in the parallelizable group and all serial tests in the serial group, preserving ordered test order", func() {
				for conf.RandomSeed = 1; conf.RandomSeed < 10; conf.RandomSeed += 1 {
					groupedSpecIndices, serialSpecIndices := internal.OrderSpecs(specs, conf)

					Ω(getTexts(specs, groupedSpecIndices)).Should(ConsistOf("B", "F", "G"))
					Ω(getTexts(specs, serialSpecIndices).Join()).Should(ContainSubstring("CDE"))
					Ω(getTexts(specs, serialSpecIndices)).Should(ConsistOf("A", "C", "D", "E", "H"))
				}

				conf.RandomSeed = 1
				groupedSpecIndices1, serialSpecIndices1 := internal.OrderSpecs(specs, conf)
				conf.RandomSeed = 2
				groupedSpecIndices2, serialSpecIndices2 := internal.OrderSpecs(specs, conf)
				Ω(getTexts(specs, groupedSpecIndices1)).ShouldNot(Equal(getTexts(specs, groupedSpecIndices2)))
				Ω(getTexts(specs, serialSpecIndices1)).ShouldNot(Equal(getTexts(specs, serialSpecIndices2)))
			})
		})

		Describe("presorting-specs", func() {
			BeforeEach(func() {
				conA0 := N(ntCon, CL("file-A", 1))
				conA1 := N(ntCon, Ordered, CL("file-A", 4))
				conA2 := N(ntCon, CL("file-A", 10))
				conB0 := N(ntCon, CL("file-B", 1))
				conC0 := N(ntCon, CL("file-C", 1))
				specs = Specs{
					S(conA0, N("A", ntIt, CL("file-A", 2))),
					S(conA0, N("B", ntIt, CL("file-A", 3))),
					// C and D are generated by a helper function in a different file.  if we aren't careful they would sort after E.  But conA1 is an Ordered container so its important things run in the correct order
					S(conA0, conA1, N("C", ntIt, CL("file-Z", 100))),
					S(conA0, conA1, N("D", ntIt, CL("file-Z", 99))),
					S(conA0, conA1, N(ntCon, CL("file-A", 7)), N("E", ntIt, CL("file-A", 8))),
					S(conA0, N("F", ntIt, CL("file-A", 9))),
					S(conA0, conA2, N("G", ntIt, CL("file-A", 11))),
					S(conA0, conA2, N("H", ntIt, CL("file-A", 12))),
					S(conB0, N("B-Z", ntIt, CL("file-B", 2))),
					S(conB0, N("B-Y", ntIt, CL("file-B", 3))),
					S(conB0, N("B-D", ntIt, CL("file-B", 4))),
					S(conB0, N("B-C", ntIt, CL("file-B", 4))),
					S(conB0, N("B-B", ntIt, CL("file-B", 4))),
					S(conB0, N("B-A", ntIt, CL("file-B", 5))),
				}

				for key := range map[string]bool{"C-A": true, "C-B": true, "C-C": true, "C-D": true, "C-E": true, "C-F": true} {
					specs = append(specs, S(conC0, N(key, ntIt, CL("file-C", 2)))) // normally this would be totally non-deterministic
				}
				conf.RandomizeAllSpecs = false
			})

			It("ensures a deterministic order for specs that are defined at the same line without messing with the natural order of specs and containers; it also ensures ordered containers run in the correct order - even if specs are generated in a helper function at a different line", func() {
				conf.RandomSeed = 1 // this happens to sort conA0 ahead of conB0 - other than that, though, we are actually testing SortableSpecs
				groupedSpecIndices, serialSpecIndices := internal.OrderSpecs(specs, conf)
				Ω(serialSpecIndices).Should(BeEmpty())

				Ω(getTexts(specs, groupedSpecIndices).Join()).Should(Equal("ABCDEFGHB-ZB-YB-BB-CB-DB-AC-AC-BC-CC-DC-EC-F"))
			})
		})

		Describe("presorting-specs with randomize-all enabled", func() {
			generateSpecs := func() Specs {
				conA0 := N(ntCon, CL("file-A", 1))
				conA1 := N(ntCon, CL("file-A", 4))
				conA2 := N(ntCon, CL("file-A", 10))
				conB0 := N(ntCon, CL("file-B", 1))
				conC0 := N(ntCon, CL("file-C", 1))
				specs := Specs{
					S(conA0, N("A", ntIt, CL("file-A", 2))),
					S(conA0, N("B", ntIt, CL("file-A", 3))),
					S(conA0, conA1, N("C", ntIt, CL("file-A", 5))),
					S(conA0, conA1, N("D", ntIt, CL("file-A", 6))),
					S(conA0, conA1, N(ntCon, CL("file-A", 7)), N("E", ntIt, CL("file-A", 8))),
					S(conA0, N("F", ntIt, CL("file-A", 9))),
					S(conA0, conA2, N("G", ntIt, CL("file-A", 11))),
					S(conA0, conA2, N("H", ntIt, CL("file-A", 12))),
					S(conB0, N("B-Z", ntIt, CL("file-B", 2))),
					S(conB0, N("B-Y", ntIt, CL("file-B", 3))),
					S(conB0, N("B-D", ntIt, CL("file-B", 4))),
					S(conB0, N("B-C", ntIt, CL("file-B", 4))),
					S(conB0, N("B-B", ntIt, CL("file-B", 4))),
					S(conB0, N("B-A", ntIt, CL("file-B", 5))),
				}

				for key := range map[string]bool{"C-A": true, "C-B": true, "C-C": true, "C-D": true, "C-E": true, "C-F": true} {
					specs = append(specs, S(conC0, N(key, ntIt, CL("file-C", 2)))) // normally this would be totally non-deterministic
				}
				return specs
			}

			It("ensures a deterministic order for specs that are defined at the same line", func() {
				conf.RandomSeed = time.Now().Unix()
				conf.RandomizeAllSpecs = true

				specsA := generateSpecs()
				specsB := generateSpecs()
				groupedSpecIndicesA, serialSpecIndices := internal.OrderSpecs(specsA, conf)
				Ω(serialSpecIndices).Should(BeEmpty())
				groupedSpecIndicesB, serialSpecIndices := internal.OrderSpecs(specsB, conf)
				Ω(serialSpecIndices).Should(BeEmpty())

				Ω(getTexts(specsA, groupedSpecIndicesA).Join()).Should(Equal(getTexts(specsB, groupedSpecIndicesB).Join()))

			}, MustPassRepeatedly(5))
		})
	})
})