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.
This commit is contained in:
Sargun Dhillon
2020-08-03 10:08:41 -07:00
parent 3fc79dc677
commit 49c596c5ca
2 changed files with 51 additions and 45 deletions

View File

@@ -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

51
node/waitable_int_test.go Normal file
View File

@@ -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()
}