Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a randomized start before running CheckMonitors. #546

Merged
merged 4 commits into from
Dec 18, 2014
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 %ds before first invocation of %s", int(initialPauseTime.Seconds()), c.Script)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you just print a time.Duration directly here as a string, it will get formatted in a nice, human-friendly way by its String() method. That way we don't have to explicitly cast to an int, and we get a more accurate value in the log.

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: 5 * time.Second,
Logger: log.New(os.Stderr, "", log.LstdFlags),
}
check.Start()
defer check.Stop()

time.Sleep(6 * time.Second)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's lower these timing values significantly. I think we can safely use a low value like 10ms for the Interval, and then use testutil.WaitForResult() to wrap checking the map indexes below. This way we can likely complete the test in ~20ms or so, rather than adding 6s to the overall test time. If you grep through the test code for WaitForResult, you will see how we generally use it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, WaitForResult is probably not necessary here, but let's definitely use sub-second values for the interval and sleep time.


// Should have at least 1 update
if mock.updates["foo"] < 1 {
t.Fatalf("should have 1 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