use WaitGroup and move test in api.bats

Signed-off-by: Xian Chaobo <xianchaobo@huawei.com>
This commit is contained in:
Xian Chaobo 2015-04-16 22:10:39 -04:00
parent 820804b108
commit 59c5b6b317
3 changed files with 32 additions and 41 deletions

View File

@ -241,17 +241,22 @@ func (c *Cluster) Pull(name string, callback func(what, status string)) {
// Load image
func (c *Cluster) Load(imageReader io.Reader, callback func(what, status string)) {
size := len(c.engines)
done := make(chan bool, size)
var wg sync.WaitGroup
c.RLock()
pipeWriters := []*io.PipeWriter{}
pipeReaders := []*io.PipeReader{}
for _, n := range c.engines {
wg.Add(1)
pipeReader, pipeWriter := io.Pipe()
pipeReaders = append(pipeReaders, pipeReader)
pipeWriters = append(pipeWriters, pipeWriter)
go func(reader *io.PipeReader, nn *cluster.Engine) {
defer wg.Done()
defer reader.Close()
// call engine load image
err := nn.Load(reader)
if callback != nil {
@ -259,10 +264,6 @@ func (c *Cluster) Load(imageReader io.Reader, callback func(what, status string)
callback(nn.Name, err.Error())
}
}
// clean up
defer reader.Close()
done <- true
}(pipeReader, n)
}
@ -284,10 +285,9 @@ func (c *Cluster) Load(imageReader io.Reader, callback func(what, status string)
pipeW.Close()
}
// wait all host done
for i := 0; i < size; i++ {
<-done
}
c.RUnlock()
wg.Wait()
}
// Containers returns all the containers in the cluster.

View File

@ -98,7 +98,28 @@ function teardown() {
# FIXME
@test "docker load" {
skip
# temp file for saving image
IMAGE_FILE=$(mktemp)
# create a tar file
docker pull busybox:latest
docker save -o $IMAGE_FILE busybox:latest
start_docker 2
swarm_manage
run docker_swarm load -i $IMAGE_FILE
[ "$status" -eq 0 ]
run docker -H ${HOSTS[0]} images
[ "${#lines[@]}" -eq 2 ]
run docker -H ${HOSTS[1]} images
[ "${#lines[@]}" -eq 2 ]
rm -f $IMAGE_FILE
}
}
# FIXME

View File

@ -1,30 +0,0 @@
#!/usr/bin/env bats
load helpers
# temp file for saving image
IMAGE_FILE=$(mktemp)
function teardown() {
stop_docker
swarm_manage_cleanup
rm -f $IMAGE_FILE
}
@test "docker load should return success,every node should load the image" {
# create a tar file
docker pull busybox:latest
docker save -o $IMAGE_FILE busybox:latest
start_docker 2
swarm_manage
run docker_swarm load -i $IMAGE_FILE
[ "$status" -eq 0 ]
run docker -H ${HOSTS[0]} images
[ "${#lines[@]}" -eq 2 ]
run docker -H ${HOSTS[1]} images
[ "${#lines[@]}" -eq 2 ]
}