From 49c596c5caba570ac5d92a1bc085386265cfe036 Mon Sep 17 00:00:00 2001 From: Sargun Dhillon Date: Mon, 3 Aug 2020 10:08:41 -0700 Subject: [PATCH] Split waitableInt into its own test file This is merely a rearranging of the deck chairs and moving waitable int into its own file since we intend to use it across multiple tests. --- node/mock_test.go | 45 ---------------------------------- node/waitable_int_test.go | 51 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 45 deletions(-) create mode 100644 node/waitable_int_test.go diff --git a/node/mock_test.go b/node/mock_test.go index 297ad487a..5e679331b 100644 --- a/node/mock_test.go +++ b/node/mock_test.go @@ -20,51 +20,6 @@ var ( _ PodLifecycleHandler = (*mockProvider)(nil) ) -type waitableInt struct { - cond *sync.Cond - val int -} - -func newWaitableInt() *waitableInt { - return &waitableInt{ - cond: sync.NewCond(&sync.Mutex{}), - } -} - -func (w *waitableInt) read() int { - defer w.cond.L.Unlock() - w.cond.L.Lock() - return w.val -} - -func (w *waitableInt) until(ctx context.Context, f func(int) bool) error { - ctx, cancel := context.WithCancel(ctx) - defer cancel() - - go func() { - <-ctx.Done() - w.cond.Broadcast() - }() - - w.cond.L.Lock() - defer w.cond.L.Unlock() - - for !f(w.val) { - if err := ctx.Err(); err != nil { - return err - } - w.cond.Wait() - } - return nil -} - -func (w *waitableInt) increment() { - w.cond.L.Lock() - defer w.cond.L.Unlock() - w.val++ - w.cond.Broadcast() -} - type mockProvider struct { creates *waitableInt updates *waitableInt diff --git a/node/waitable_int_test.go b/node/waitable_int_test.go new file mode 100644 index 000000000..6757f14ce --- /dev/null +++ b/node/waitable_int_test.go @@ -0,0 +1,51 @@ +package node + +import ( + "context" + "sync" +) + +type waitableInt struct { + cond *sync.Cond + val int +} + +func newWaitableInt() *waitableInt { + return &waitableInt{ + cond: sync.NewCond(&sync.Mutex{}), + } +} + +func (w *waitableInt) read() int { + defer w.cond.L.Unlock() + w.cond.L.Lock() + return w.val +} + +func (w *waitableInt) until(ctx context.Context, f func(int) bool) error { + ctx, cancel := context.WithCancel(ctx) + defer cancel() + + go func() { + <-ctx.Done() + w.cond.Broadcast() + }() + + w.cond.L.Lock() + defer w.cond.L.Unlock() + + for !f(w.val) { + if err := ctx.Err(); err != nil { + return err + } + w.cond.Wait() + } + return nil +} + +func (w *waitableInt) increment() { + w.cond.L.Lock() + defer w.cond.L.Unlock() + w.val++ + w.cond.Broadcast() +}