Skip to content

Commit 22df817

Browse files
committed
Fix termination tests randomly timing out
Unit tests are run in random order. Termination tests also verify the polling logic of the respective code, by atomically incrementing a counter in each call of the HTTP handler. The issues fixed with these changes are: 1. counter var not being reset after each termination test using polling 2. using the polling iterations loop in termination tests that were not using the HTTP handler that increments the counter 3. polling iterations loop not using sleep between iterations, which spikes CPU usage The first two issues led to the termination tests sometimes passing, depending on which termination test run first. If it was a test that incremented the polling counter, then all other test would be successful as well. If it was a test that didn't increment the polling counter, then the termination tests would be stuck in an endless loop, until the unit tests reached a timeout. Signed-off-by: Michail Resvanis <[email protected]>
1 parent 97ea122 commit 22df817

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

pkg/termination/termination_test.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ var _ = Describe("Handler Suite", func() {
124124
var counter int32
125125

126126
BeforeEach(func() {
127+
counter = 0
128+
127129
// Ensure the polling logic is excercised in tests
128130
httpHandler = newMockHTTPHandler(func(rw http.ResponseWriter, req *http.Request) {
129131
if atomic.LoadInt32(&counter) == 4 {
@@ -135,15 +137,15 @@ var _ = Describe("Handler Suite", func() {
135137
})
136138
})
137139

138-
JustBeforeEach(func() {
139-
// Ensure the polling logic is excercised in tests
140-
for atomic.LoadInt32(&counter) < 4 {
141-
continue
142-
}
143-
})
144-
145140
Context("and the handler is stopped", func() {
146141
JustBeforeEach(func() {
142+
// Ensure the polling logic is tested as well
143+
for atomic.LoadInt32(&counter) < 4 {
144+
// use 50ms since polling is set to 100ms
145+
time.Sleep(50 * time.Millisecond)
146+
continue
147+
}
148+
147149
close(stop)
148150
})
149151

@@ -157,6 +159,15 @@ var _ = Describe("Handler Suite", func() {
157159
})
158160

159161
Context("and the instance termination notice is fulfilled", func() {
162+
JustBeforeEach(func() {
163+
// Ensure the polling logic is tested as well
164+
for atomic.LoadInt32(&counter) < 4 {
165+
// use 50ms since polling is set to 100ms
166+
time.Sleep(50 * time.Millisecond)
167+
continue
168+
}
169+
})
170+
160171
It("should mark the node for deletion", func() {
161172
Eventually(nodeMarkedForDeletion(testNode.Name)).Should(BeTrue())
162173
})

0 commit comments

Comments
 (0)