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:
@@ -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
51
node/waitable_int_test.go
Normal 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()
|
||||
}
|
||||
Reference in New Issue
Block a user