github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/client/allocrunner/tasklifecycle/testing.go (about) 1 package tasklifecycle 2 3 import ( 4 "time" 5 6 "github.com/hashicorp/nomad/nomad/structs" 7 "github.com/hashicorp/nomad/testutil" 8 testing "github.com/mitchellh/go-testing-interface" 9 ) 10 11 func RequireTaskBlocked(t testing.T, c *Coordinator, task *structs.Task) { 12 ch := c.StartConditionForTask(task) 13 requireChannelBlocking(t, ch, task.Name) 14 } 15 16 func RequireTaskAllowed(t testing.T, c *Coordinator, task *structs.Task) { 17 ch := c.StartConditionForTask(task) 18 requireChannelPassing(t, ch, task.Name) 19 } 20 21 func WaitNotInitUntil(c *Coordinator, until time.Duration, errorFunc func()) { 22 testutil.WaitForResultUntil(until, 23 func() (bool, error) { 24 c.currentStateLock.RLock() 25 defer c.currentStateLock.RUnlock() 26 return c.currentState != coordinatorStateInit, nil 27 }, 28 func(_ error) { 29 errorFunc() 30 }) 31 } 32 33 func requireChannelPassing(t testing.T, ch <-chan struct{}, name string) { 34 testutil.WaitForResult(func() (bool, error) { 35 return !isChannelBlocking(ch), nil 36 }, func(_ error) { 37 t.Fatalf("%s channel was blocking, should be passing", name) 38 }) 39 } 40 41 func requireChannelBlocking(t testing.T, ch <-chan struct{}, name string) { 42 testutil.WaitForResult(func() (bool, error) { 43 return isChannelBlocking(ch), nil 44 }, func(_ error) { 45 t.Fatalf("%s channel was passing, should be blocking", name) 46 }) 47 } 48 49 func isChannelBlocking(ch <-chan struct{}) bool { 50 select { 51 case <-ch: 52 return false 53 default: 54 return true 55 } 56 }