Skip to content

Commit 1395ba8

Browse files
author
Renato Costa
committed
testutils: fix determinism in random predecessor history tests
Previously, the `rng` instance used by the tests was initialized once in the `var` block. That meant that every assertion that relied on that rng was order-dependent: running a single test in that file in isolation would lead to failed assertions. This commit ensures that every test resets the rng before making any assertions, guaranteeing we get the same values regardless of execution order. Epic: none Release note: None
1 parent e9add29 commit 1395ba8

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

pkg/testutils/release/releases_test.go

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,17 @@ var (
4040
},
4141
}
4242

43-
// Results rely on this constant seed
44-
rng = rand.New(rand.NewSource(1))
43+
// Results rely on this constant seed.
44+
seed = int64(1)
45+
46+
// Tests must call `resetRNG` if they use this global rng.
47+
rng *rand.Rand
4548
)
4649

50+
func resetRNG() {
51+
rng = rand.New(rand.NewSource(seed))
52+
}
53+
4754
func TestLatestAndRandomPredecessor(t *testing.T) {
4855
testCases := []struct {
4956
name string
@@ -72,16 +79,17 @@ func TestLatestAndRandomPredecessor(t *testing.T) {
7279
name: "latest is withdrawn",
7380
v: "v22.2.3",
7481
expectedLatest: "22.1.12",
75-
expectedRandom: "22.1.8",
82+
expectedRandom: "22.1.10",
7683
},
7784
}
7885

79-
for _, tc := range testCases {
80-
oldReleaseData := releaseData
81-
releaseData = testReleaseData
82-
defer func() { releaseData = oldReleaseData }()
86+
oldReleaseData := releaseData
87+
releaseData = testReleaseData
88+
defer func() { releaseData = oldReleaseData }()
8389

90+
for _, tc := range testCases {
8491
t.Run(tc.name, func(t *testing.T) {
92+
resetRNG() // deterministic results
8593
latestPred, latestErr := LatestPredecessor(version.MustParse(tc.v))
8694
randomPred, randomErr := RandomPredecessor(rng, version.MustParse(tc.v))
8795
if tc.expectedErr == "" {
@@ -119,23 +127,24 @@ func TestLatestPredecessorHistory(t *testing.T) {
119127
v: "v23.1.1",
120128
k: 3,
121129
expectedLatest: []string{"19.2.0", "22.1.12", "22.2.8"},
122-
expectedRandom: []string{"19.2.0", "22.1.12", "22.2.1"},
130+
expectedRandom: []string{"19.2.0", "22.1.8", "22.2.8"},
123131
},
124132
{
125133
name: "with pre-release",
126134
v: "v23.1.1-beta.1",
127135
k: 2,
128136
expectedLatest: []string{"22.1.12", "22.2.8"},
129-
expectedRandom: []string{"22.1.0", "22.2.5"},
137+
expectedRandom: []string{"22.1.8", "22.2.8"},
130138
},
131139
}
132140

133-
for _, tc := range testCases {
134-
oldReleaseData := releaseData
135-
releaseData = testReleaseData
136-
defer func() { releaseData = oldReleaseData }()
141+
oldReleaseData := releaseData
142+
releaseData = testReleaseData
143+
defer func() { releaseData = oldReleaseData }()
137144

145+
for _, tc := range testCases {
138146
t.Run(tc.name, func(t *testing.T) {
147+
resetRNG() // deterministic results
139148
latestHistory, latestErr := LatestPredecessorHistory(version.MustParse(tc.v), tc.k)
140149
randomHistory, randomErr := RandomPredecessorHistory(rng, version.MustParse(tc.v), tc.k)
141150
if tc.expectedErr == "" {

0 commit comments

Comments
 (0)