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++ {
|
2015-11-23 18:56:38 +00:00
|
|
|
actual, when := rt.nextRestart(127)
|
2015-11-06 01:13:25 +00:00
|
|
|
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++ {
|
2015-11-23 18:56:38 +00:00
|
|
|
actual, when := rt.nextRestart(127)
|
2015-11-06 01:13:25 +00:00
|
|
|
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++ {
|
2015-11-23 18:56:38 +00:00
|
|
|
shouldRestart, when := rt.nextRestart(127)
|
2015-11-06 01:30:41 +00:00
|
|
|
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-23 18:56:38 +00:00
|
|
|
actual, _ := rt.nextRestart(1)
|
2015-11-05 19:12:31 +00:00
|
|
|
if actual {
|
|
|
|
t.Fatalf("Expect %v, Actual: %v", false, actual)
|
|
|
|
}
|
|
|
|
}
|
2015-11-23 18:56:38 +00:00
|
|
|
|
|
|
|
func TestTaskRunner_BatchRestartOnSuccess(t *testing.T) {
|
|
|
|
attempts := 2
|
|
|
|
interval := 1 * time.Second
|
|
|
|
delay := 1 * time.Second
|
|
|
|
rt := newRestartTracker(structs.JobTypeBatch,
|
|
|
|
&structs.RestartPolicy{Attempts: attempts,
|
|
|
|
Interval: interval,
|
|
|
|
Delay: delay,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
shouldRestart, _ := rt.nextRestart(0)
|
|
|
|
if shouldRestart {
|
|
|
|
t.Fatalf("should restart returned %v, expected: %v", shouldRestart, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|