1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Merge pull request #28792 from yuexiao-wang/restart-mgr

Optimize the unit test for restartmanager
This commit is contained in:
Vincent Demeester 2016-11-28 20:27:44 +01:00 committed by GitHub
commit 3ccc4bc370
2 changed files with 10 additions and 8 deletions

View file

@ -35,7 +35,7 @@ type restartManager struct {
canceled bool canceled bool
} }
// New returns a new restartmanager based on a policy. // New returns a new restartManager based on a policy.
func New(policy container.RestartPolicy, restartCount int) RestartManager { func New(policy container.RestartPolicy, restartCount int) RestartManager {
return &restartManager{policy: policy, restartCount: restartCount, cancel: make(chan struct{})} return &restartManager{policy: policy, restartCount: restartCount, cancel: make(chan struct{})}
} }
@ -63,7 +63,7 @@ func (rm *restartManager) ShouldRestart(exitCode uint32, hasBeenManuallyStopped
} }
if rm.active { if rm.active {
return false, nil, fmt.Errorf("invalid call on active restartmanager") return false, nil, fmt.Errorf("invalid call on an active restart manager")
} }
// if the container ran for more than 10s, regardless of status and policy reset the // if the container ran for more than 10s, regardless of status and policy reset the
// the timeout back to the default. // the timeout back to the default.

View file

@ -9,14 +9,15 @@ import (
func TestRestartManagerTimeout(t *testing.T) { func TestRestartManagerTimeout(t *testing.T) {
rm := New(container.RestartPolicy{Name: "always"}, 0).(*restartManager) rm := New(container.RestartPolicy{Name: "always"}, 0).(*restartManager)
should, _, err := rm.ShouldRestart(0, false, 1*time.Second) var duration = time.Duration(1 * time.Second)
should, _, err := rm.ShouldRestart(0, false, duration)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if !should { if !should {
t.Fatal("container should be restarted") t.Fatal("container should be restarted")
} }
if rm.timeout != 100*time.Millisecond { if rm.timeout != defaultTimeout {
t.Fatalf("restart manager should have a timeout of 100 ms but has %s", rm.timeout) t.Fatalf("restart manager should have a timeout of 100 ms but has %s", rm.timeout)
} }
} }
@ -24,11 +25,12 @@ func TestRestartManagerTimeout(t *testing.T) {
func TestRestartManagerTimeoutReset(t *testing.T) { func TestRestartManagerTimeoutReset(t *testing.T) {
rm := New(container.RestartPolicy{Name: "always"}, 0).(*restartManager) rm := New(container.RestartPolicy{Name: "always"}, 0).(*restartManager)
rm.timeout = 5 * time.Second rm.timeout = 5 * time.Second
_, _, err := rm.ShouldRestart(0, false, 10*time.Second) var duration = time.Duration(10 * time.Second)
_, _, err := rm.ShouldRestart(0, false, duration)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if rm.timeout != 100*time.Millisecond { if rm.timeout != defaultTimeout {
t.Fatalf("restart manager should have a timeout of 100 ms but has %s", rm.timeout) t.Fatalf("restart manager should have a timeout of 100 ms but has %s", rm.timeout)
} }
} }