Better crash simulation in TestRestore ; force state lock creation when loading a container from disk

This commit is contained in:
shin- 2013-04-02 10:06:49 -07:00
parent 02c211a0dc
commit 791ca6fde4
3 changed files with 12 additions and 4 deletions

View File

@ -127,6 +127,7 @@ func (container *Container) FromDisk() error {
if err := json.Unmarshal(data, container); err != nil { if err := json.Unmarshal(data, container); err != nil {
return err return err
} }
container.State.resetLock()
return nil return nil
} }

View File

@ -7,6 +7,7 @@ import (
"os/exec" "os/exec"
"os/user" "os/user"
"testing" "testing"
"time"
) )
// FIXME: this is no longer needed // FIXME: this is no longer needed
@ -302,11 +303,12 @@ func TestRestore(t *testing.T) {
} }
// Simulate a crash/manual quit of dockerd: process dies, states stays 'Running' // Simulate a crash/manual quit of dockerd: process dies, states stays 'Running'
if err := container2.Stop(); err != nil { cStdin, _ := container2.StdinPipe()
t.Fatalf("Could not stop container: %v", err) cStdin.Close()
} container2.State.setStopped(-1)
time.Sleep(time.Second)
container2.State.Running = true container2.State.Running = true
container2.ToDisk()
if len(runtime1.List()) != 2 { if len(runtime1.List()) != 2 {
t.Errorf("Expected 2 container, %v found", len(runtime1.List())) t.Errorf("Expected 2 container, %v found", len(runtime1.List()))

View File

@ -39,6 +39,11 @@ func (s *State) setStopped(exitCode int) {
s.broadcast() s.broadcast()
} }
func (s *State) resetLock() {
s.stateChangeLock = &sync.Mutex{}
s.stateChangeCond = sync.NewCond(s.stateChangeLock)
}
func (s *State) broadcast() { func (s *State) broadcast() {
s.stateChangeLock.Lock() s.stateChangeLock.Lock()
s.stateChangeCond.Broadcast() s.stateChangeCond.Broadcast()