Skip to content

Commit

Permalink
Merge pull request #546 from ryanbreen/master
Browse files Browse the repository at this point in the history
Add a randomized start before running CheckMonitors.
  • Loading branch information
ryanuber committed Dec 18, 2014
2 parents c728cb7 + f2bd641 commit b74af61
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
5 changes: 4 additions & 1 deletion command/agent/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,10 @@ func (c *CheckMonitor) Stop() {

// run is invoked by a goroutine to run until Stop() is called
func (c *CheckMonitor) run() {
next := time.After(0)
// Get the randomized initial pause time
initialPauseTime := randomStagger(c.Interval)
c.Logger.Printf("[DEBUG] agent: pausing %v before first invocation of %s", initialPauseTime, c.Script)
next := time.After(initialPauseTime)
for {
select {
case <-next:
Expand Down
28 changes: 28 additions & 0 deletions command/agent/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,34 @@ func TestCheckMonitor_BadCmd(t *testing.T) {
expectStatus(t, "foobarbaz", structs.HealthCritical)
}

func TestCheckMonitor_RandomStagger(t *testing.T) {
mock := &MockNotify{
state: make(map[string]string),
updates: make(map[string]int),
output: make(map[string]string),
}
check := &CheckMonitor{
Notify: mock,
CheckID: "foo",
Script: "exit 0",
Interval: 25 * time.Millisecond,
Logger: log.New(os.Stderr, "", log.LstdFlags),
}
check.Start()
defer check.Stop()

time.Sleep(50 * time.Millisecond)

// Should have at least 1 update
if mock.updates["foo"] < 1 {
t.Fatalf("should have 1 or more updates %v", mock.updates)
}

if mock.state["foo"] != structs.HealthPassing {
t.Fatalf("should be %v %v", structs.HealthPassing, mock.state)
}
}

func TestCheckMonitor_LimitOutput(t *testing.T) {
mock := &MockNotify{
state: make(map[string]string),
Expand Down

0 comments on commit b74af61

Please sign in to comment.