2015-11-05 19:12:31 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestTaskRunner_ServiceRestartCounter(t *testing.T) {
|
2015-11-06 01:13:25 +00:00
|
|
|
interval := 2 * time.Minute
|
|
|
|
delay := 1 * time.Second
|
|
|
|
attempts := 3
|
|
|
|
rt := newRestartTracker(structs.JobTypeService, &structs.RestartPolicy{Attempts: attempts, Interval: interval, Delay: delay})
|
|
|
|
|
|
|
|
for i := 0; i < attempts; i++ {
|
|
|
|
actual, when := rt.nextRestart()
|
|
|
|
if !actual {
|
|
|
|
t.Fatalf("should restart returned %v, actual %v", actual, true)
|
|
|
|
}
|
|
|
|
if when != delay {
|
|
|
|
t.Fatalf("nextRestart() returned %v; want %v", when, delay)
|
|
|
|
}
|
2015-11-05 19:12:31 +00:00
|
|
|
}
|
2015-11-06 01:13:25 +00:00
|
|
|
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
actual, when := rt.nextRestart()
|
|
|
|
if !actual {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
if !(when > delay && when < interval) {
|
|
|
|
t.Fatalf("nextRestart() returned %v; want less than %v and more than %v", when, interval, delay)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-05 19:12:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestTaskRunner_BatchRestartCounter(t *testing.T) {
|
2015-11-06 01:30:41 +00:00
|
|
|
attempts := 2
|
|
|
|
interval := 1 * time.Second
|
|
|
|
delay := 1 * time.Second
|
2015-11-06 01:33:03 +00:00
|
|
|
rt := newRestartTracker(structs.JobTypeBatch,
|
|
|
|
&structs.RestartPolicy{Attempts: attempts,
|
|
|
|
Interval: interval,
|
|
|
|
Delay: delay,
|
|
|
|
},
|
|
|
|
)
|
2015-11-06 01:30:41 +00:00
|
|
|
for i := 0; i < attempts; i++ {
|
|
|
|
shouldRestart, when := rt.nextRestart()
|
|
|
|
if !shouldRestart {
|
|
|
|
t.Fatalf("should restart returned %v, actual %v", shouldRestart, true)
|
|
|
|
}
|
|
|
|
if when != delay {
|
|
|
|
t.Fatalf("Delay should be %v, actual: %v", delay, when)
|
|
|
|
}
|
2015-11-05 19:12:31 +00:00
|
|
|
}
|
2015-11-06 01:30:41 +00:00
|
|
|
actual, _ := rt.nextRestart()
|
2015-11-05 19:12:31 +00:00
|
|
|
if actual {
|
|
|
|
t.Fatalf("Expect %v, Actual: %v", false, actual)
|
|
|
|
}
|
|
|
|
}
|