Remove usage of atomics

It turns out that running atomic.Read(...) in a tight loop breaks
Golang. The goroutine would never yield control over the scheduler,
so we ended up getting into a situation where the test would get
stuck forever. This moves to a different model, in which
there is a condition var, instead of atomics in loops.
This commit is contained in:
Sargun Dhillon
2019-08-13 08:43:20 -07:00
parent 75a399f6f4
commit fbed4ca702
3 changed files with 72 additions and 38 deletions

View File

@@ -4,7 +4,6 @@ import (
"context"
"fmt"
"strconv"
"sync/atomic"
"testing"
"time"
@@ -141,17 +140,7 @@ func TestPodLifecycle(t *testing.T) {
t.Run("createStartDeleteScenarioWithDeletionRandomError", func(t *testing.T) {
mp := newMockProvider()
deletionFunc := func(ctx context.Context, watcher watch.Interface) error {
select {
case <-mp.attemptedDeletes:
case <-ctx.Done():
return ctx.Err()
}
select {
case <-mp.attemptedDeletes:
case <-ctx.Done():
return ctx.Err()
}
return nil
return mp.attemptedDeletes.until(ctx, func(v int) bool { return v >= 2 })
}
mp.errorOnDelete = errors.New("random error")
assert.NilError(t, wireUpSystem(ctx, mp, func(ctx context.Context, s *system) {
@@ -362,7 +351,7 @@ func testDanglingPodScenario(ctx context.Context, t *testing.T, s *system, m *mo
// Start the pod controller
s.start(ctx)
assert.Assert(t, m.deletes == 1)
assert.Assert(t, is.Equal(m.deletes.read(), 1))
}
@@ -536,8 +525,7 @@ func testUpdatePodWhileRunningScenario(ctx context.Context, t *testing.T, s *sys
log.G(ctx).WithField("pod", p).Info("Updating pod")
_, err = s.client.CoreV1().Pods(p.Namespace).Update(p)
assert.NilError(t, err)
for atomic.LoadUint64(&m.updates) == 0 {
}
assert.NilError(t, m.updates.until(ctx, func(v int) bool { return v > 0 }))
}
func BenchmarkCreatePods(b *testing.B) {