From 89936cdf342eadf0f25efad77c94985ed2b46ee1 Mon Sep 17 00:00:00 2001 From: Martin Linkhorst Date: Sat, 28 Jul 2018 15:36:26 +0200 Subject: [PATCH] ref: have run take a channel instead of a Ticker --- chaoskube/chaoskube.go | 8 ++++---- chaoskube/chaoskube_test.go | 7 ++----- main.go | 2 +- 3 files changed, 7 insertions(+), 10 deletions(-) diff --git a/chaoskube/chaoskube.go b/chaoskube/chaoskube.go index 9c4f7457..d771a765 100644 --- a/chaoskube/chaoskube.go +++ b/chaoskube/chaoskube.go @@ -83,9 +83,9 @@ func New(client kubernetes.Interface, labels, annotations, namespaces labels.Sel } } -// Run continuously picks and terminates a victim pod at a given interval described by ticker. -// It returns when the given context is cancelled. -func (c *Chaoskube) Run(ctx context.Context, ticker *time.Ticker) { +// Run continuously picks and terminates a victim pod at a given interval +// described by channel next. It returns when the given context is canceled. +func (c *Chaoskube) Run(ctx context.Context, next <-chan time.Time) { for { if err := c.TerminateVictim(); err != nil { c.Logger.WithField("err", err).Error("failed to terminate victim") @@ -93,7 +93,7 @@ func (c *Chaoskube) Run(ctx context.Context, ticker *time.Ticker) { c.Logger.Debug("sleeping...") select { - case <-ticker.C: + case <-next: case <-ctx.Done(): return } diff --git a/chaoskube/chaoskube_test.go b/chaoskube/chaoskube_test.go index b714c4ac..39a10581 100644 --- a/chaoskube/chaoskube_test.go +++ b/chaoskube/chaoskube_test.go @@ -74,7 +74,7 @@ func (suite *Suite) TestNew() { } // TestRun todo -func (suite *Suite) TestRunContextCancelled() { +func (suite *Suite) TestRunContextCanceled() { chaoskube := suite.setup( labels.Everything(), labels.Everything(), @@ -89,10 +89,7 @@ func (suite *Suite) TestRunContextCancelled() { ctx, cancel := context.WithCancel(context.Background()) cancel() - ticker := time.NewTicker(1) - defer ticker.Stop() - - chaoskube.Run(ctx, ticker) + chaoskube.Run(ctx, nil) } func (suite *Suite) TestCandidates() { diff --git a/main.go b/main.go index 433e2aaa..2e1e350e 100644 --- a/main.go +++ b/main.go @@ -170,7 +170,7 @@ func main() { ticker := time.NewTicker(interval) defer ticker.Stop() - chaoskube.Run(ctx, ticker) + chaoskube.Run(ctx, ticker.C) } func newClient() (*kubernetes.Clientset, error) {