2015-11-12 14:55:17 -05:00
|
|
|
package container
|
2014-06-06 07:28:12 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"sync/atomic"
|
|
|
|
"testing"
|
|
|
|
"time"
|
2014-10-08 13:03:57 -04:00
|
|
|
|
|
|
|
"github.com/docker/docker/daemon/execdriver"
|
2014-06-06 07:28:12 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestStateRunStop(t *testing.T) {
|
|
|
|
s := NewState()
|
|
|
|
for i := 1; i < 3; i++ { // full lifecycle two times
|
|
|
|
started := make(chan struct{})
|
|
|
|
var pid int64
|
|
|
|
go func() {
|
2016-01-04 10:58:20 -05:00
|
|
|
runPid, _ := s.WaitRunning(-1 * time.Second)
|
2014-06-06 07:28:12 -04:00
|
|
|
atomic.StoreInt64(&pid, int64(runPid))
|
|
|
|
close(started)
|
|
|
|
}()
|
2015-09-28 16:48:12 -04:00
|
|
|
s.Lock()
|
2015-11-12 14:55:17 -05:00
|
|
|
s.SetRunning(i + 100)
|
2015-09-28 16:48:12 -04:00
|
|
|
s.Unlock()
|
2015-07-30 17:01:53 -04:00
|
|
|
|
2014-06-06 07:28:12 -04:00
|
|
|
if !s.IsRunning() {
|
|
|
|
t.Fatal("State not running")
|
|
|
|
}
|
|
|
|
if s.Pid != i+100 {
|
|
|
|
t.Fatalf("Pid %v, expected %v", s.Pid, i+100)
|
|
|
|
}
|
|
|
|
if s.ExitCode != 0 {
|
|
|
|
t.Fatalf("ExitCode %v, expected 0", s.ExitCode)
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-time.After(100 * time.Millisecond):
|
|
|
|
t.Fatal("Start callback doesn't fire in 100 milliseconds")
|
|
|
|
case <-started:
|
|
|
|
t.Log("Start callback fired")
|
|
|
|
}
|
|
|
|
runPid := int(atomic.LoadInt64(&pid))
|
|
|
|
if runPid != i+100 {
|
|
|
|
t.Fatalf("Pid %v, expected %v", runPid, i+100)
|
|
|
|
}
|
2016-01-04 10:58:20 -05:00
|
|
|
if pid, err := s.WaitRunning(-1 * time.Second); err != nil || pid != i+100 {
|
|
|
|
t.Fatalf("WaitRunning returned pid: %v, err: %v, expected pid: %v, err: %v", pid, err, i+100, nil)
|
2014-06-06 07:28:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
stopped := make(chan struct{})
|
|
|
|
var exit int64
|
|
|
|
go func() {
|
|
|
|
exitCode, _ := s.WaitStop(-1 * time.Second)
|
|
|
|
atomic.StoreInt64(&exit, int64(exitCode))
|
|
|
|
close(stopped)
|
|
|
|
}()
|
2015-11-12 14:55:17 -05:00
|
|
|
s.SetStoppedLocking(&execdriver.ExitStatus{ExitCode: i})
|
2014-06-06 07:28:12 -04:00
|
|
|
if s.IsRunning() {
|
|
|
|
t.Fatal("State is running")
|
|
|
|
}
|
|
|
|
if s.ExitCode != i {
|
|
|
|
t.Fatalf("ExitCode %v, expected %v", s.ExitCode, i)
|
|
|
|
}
|
|
|
|
if s.Pid != 0 {
|
|
|
|
t.Fatalf("Pid %v, expected 0", s.Pid)
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-time.After(100 * time.Millisecond):
|
|
|
|
t.Fatal("Stop callback doesn't fire in 100 milliseconds")
|
|
|
|
case <-stopped:
|
|
|
|
t.Log("Stop callback fired")
|
|
|
|
}
|
|
|
|
exitCode := int(atomic.LoadInt64(&exit))
|
|
|
|
if exitCode != i {
|
|
|
|
t.Fatalf("ExitCode %v, expected %v", exitCode, i)
|
|
|
|
}
|
|
|
|
if exitCode, err := s.WaitStop(-1 * time.Second); err != nil || exitCode != i {
|
2014-08-13 03:37:30 -04:00
|
|
|
t.Fatalf("WaitStop returned exitCode: %v, err: %v, expected exitCode: %v, err: %v", exitCode, err, i, nil)
|
2014-06-06 07:28:12 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStateTimeoutWait(t *testing.T) {
|
|
|
|
s := NewState()
|
|
|
|
started := make(chan struct{})
|
|
|
|
go func() {
|
2016-01-04 10:58:20 -05:00
|
|
|
s.WaitRunning(100 * time.Millisecond)
|
2014-06-06 07:28:12 -04:00
|
|
|
close(started)
|
|
|
|
}()
|
|
|
|
select {
|
|
|
|
case <-time.After(200 * time.Millisecond):
|
|
|
|
t.Fatal("Start callback doesn't fire in 100 milliseconds")
|
|
|
|
case <-started:
|
|
|
|
t.Log("Start callback fired")
|
|
|
|
}
|
2015-07-30 17:01:53 -04:00
|
|
|
|
2015-09-28 16:48:12 -04:00
|
|
|
s.Lock()
|
2015-11-12 14:55:17 -05:00
|
|
|
s.SetRunning(49)
|
2015-09-28 16:48:12 -04:00
|
|
|
s.Unlock()
|
2015-07-30 17:01:53 -04:00
|
|
|
|
2014-06-06 07:28:12 -04:00
|
|
|
stopped := make(chan struct{})
|
|
|
|
go func() {
|
2016-01-04 10:58:20 -05:00
|
|
|
s.WaitRunning(100 * time.Millisecond)
|
2014-06-06 07:28:12 -04:00
|
|
|
close(stopped)
|
|
|
|
}()
|
|
|
|
select {
|
|
|
|
case <-time.After(200 * time.Millisecond):
|
|
|
|
t.Fatal("Start callback doesn't fire in 100 milliseconds")
|
|
|
|
case <-stopped:
|
|
|
|
t.Log("Start callback fired")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|