Skip to content

Commit

Permalink
ref: have run take a channel instead of a Ticker
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Linkhorst committed Jul 30, 2018
1 parent aa65d8b commit 89936cd
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 10 deletions.
8 changes: 4 additions & 4 deletions chaoskube/chaoskube.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,17 @@ 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")
}

c.Logger.Debug("sleeping...")
select {
case <-ticker.C:
case <-next:
case <-ctx.Done():
return
}
Expand Down
7 changes: 2 additions & 5 deletions chaoskube/chaoskube_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 89936cd

Please sign in to comment.