2015-11-05 19:12:31 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
2015-12-18 20:17:13 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2015-11-05 19:12:31 +00:00
|
|
|
)
|
|
|
|
|
2015-12-18 20:17:13 +00:00
|
|
|
func testPolicy(success bool, mode string) *structs.RestartPolicy {
|
|
|
|
return &structs.RestartPolicy{
|
|
|
|
Interval: 2 * time.Minute,
|
|
|
|
Delay: 1 * time.Second,
|
|
|
|
Attempts: 3,
|
|
|
|
Mode: mode,
|
|
|
|
RestartOnSuccess: success,
|
|
|
|
}
|
|
|
|
}
|
2015-11-06 01:13:25 +00:00
|
|
|
|
2015-12-18 20:11:12 +00:00
|
|
|
// withinJitter is a helper that returns whether the returned delay is within
|
|
|
|
// the jitter.
|
|
|
|
func withinJitter(expected, actual time.Duration) bool {
|
|
|
|
return float64((actual.Nanoseconds()-expected.Nanoseconds())/
|
|
|
|
expected.Nanoseconds()) <= jitter
|
|
|
|
}
|
|
|
|
|
2015-12-18 20:17:13 +00:00
|
|
|
func TestClient_RestartTracker_ModeDelay(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
p := testPolicy(true, structs.RestartPolicyModeDelay)
|
2015-12-16 22:39:37 +00:00
|
|
|
rt := newRestartTracker(p)
|
2015-12-18 20:17:13 +00:00
|
|
|
for i := 0; i < p.Attempts; i++ {
|
|
|
|
actual, when := rt.NextRestart(127)
|
2015-11-06 01:13:25 +00:00
|
|
|
if !actual {
|
2015-12-18 20:17:13 +00:00
|
|
|
t.Fatalf("NextRestart() returned %v, want %v", actual, true)
|
2015-11-06 01:13:25 +00:00
|
|
|
}
|
2015-12-18 20:11:12 +00:00
|
|
|
if !withinJitter(p.Delay, when) {
|
|
|
|
t.Fatalf("NextRestart() returned %v; want %v+jitter", when, p.Delay)
|
2015-11-06 01:13:25 +00:00
|
|
|
}
|
2015-11-05 19:12:31 +00:00
|
|
|
}
|
2015-11-06 01:13:25 +00:00
|
|
|
|
2015-12-18 20:17:13 +00:00
|
|
|
// Follow up restarts should cause delay.
|
2015-11-06 01:13:25 +00:00
|
|
|
for i := 0; i < 3; i++ {
|
2015-12-18 20:17:13 +00:00
|
|
|
actual, when := rt.NextRestart(127)
|
2015-11-06 01:13:25 +00:00
|
|
|
if !actual {
|
|
|
|
t.Fail()
|
|
|
|
}
|
2015-12-21 15:43:45 +00:00
|
|
|
if !(when > p.Delay && when <= p.Interval) {
|
|
|
|
t.Fatalf("NextRestart() returned %v; want > %v and <= %v", when, p.Delay, p.Interval)
|
2015-11-06 01:13:25 +00:00
|
|
|
}
|
|
|
|
}
|
2015-11-05 19:12:31 +00:00
|
|
|
}
|
|
|
|
|
2015-12-18 20:17:13 +00:00
|
|
|
func TestClient_RestartTracker_ModeFail(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
p := testPolicy(true, structs.RestartPolicyModeFail)
|
2015-12-16 22:39:37 +00:00
|
|
|
rt := newRestartTracker(p)
|
2015-12-18 20:17:13 +00:00
|
|
|
for i := 0; i < p.Attempts; i++ {
|
|
|
|
actual, when := rt.NextRestart(127)
|
|
|
|
if !actual {
|
|
|
|
t.Fatalf("NextRestart() returned %v, want %v", actual, true)
|
2015-11-06 01:30:41 +00:00
|
|
|
}
|
2015-12-18 20:11:12 +00:00
|
|
|
if !withinJitter(p.Delay, when) {
|
|
|
|
t.Fatalf("NextRestart() returned %v; want %v+jitter", when, p.Delay)
|
2015-11-06 01:30:41 +00:00
|
|
|
}
|
2015-11-05 19:12:31 +00:00
|
|
|
}
|
2015-12-18 20:17:13 +00:00
|
|
|
|
|
|
|
// Next restart should cause fail
|
|
|
|
if actual, _ := rt.NextRestart(127); actual {
|
|
|
|
t.Fail()
|
2015-11-05 19:12:31 +00:00
|
|
|
}
|
|
|
|
}
|
2015-11-23 18:56:38 +00:00
|
|
|
|
2015-12-18 20:17:13 +00:00
|
|
|
func TestClient_RestartTracker_NoRestartOnSuccess(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
p := testPolicy(false, structs.RestartPolicyModeDelay)
|
2015-12-16 22:39:37 +00:00
|
|
|
rt := newRestartTracker(p)
|
2015-12-18 20:17:13 +00:00
|
|
|
if shouldRestart, _ := rt.NextRestart(0); shouldRestart {
|
|
|
|
t.Fatalf("NextRestart() returned %v, expected: %v", shouldRestart, false)
|
2015-11-23 18:56:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|