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

Improve select for daemon restart tests

This improves the select logic for the restart tests or starting the
daemon in general.  With the way the ticker and select was setup, it was
possible for only the timeout to be displayed and not the wait errors.

Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
Michael Crosby 2019-07-02 16:16:33 -04:00
parent a43a2ed746
commit 402433a5e4

View file

@ -279,7 +279,7 @@ func (d *Daemon) StartWithLogFile(out *os.File, providedArgs ...string) error {
return errors.Errorf("[%s] could not start daemon container: %v", d.id, err) return errors.Errorf("[%s] could not start daemon container: %v", d.id, err)
} }
wait := make(chan error) wait := make(chan error, 1)
go func() { go func() {
ret := d.cmd.Wait() ret := d.cmd.Wait()
@ -307,26 +307,27 @@ func (d *Daemon) StartWithLogFile(out *os.File, providedArgs ...string) error {
req.URL.Host = clientConfig.addr req.URL.Host = clientConfig.addr
req.URL.Scheme = clientConfig.scheme req.URL.Scheme = clientConfig.scheme
ticker := time.NewTicker(500 * time.Millisecond) ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer ticker.Stop() defer cancel()
tick := ticker.C
timeout := time.NewTimer(60 * time.Second) // timeout for the whole loop
defer timeout.Stop()
// make sure daemon is ready to receive requests // make sure daemon is ready to receive requests
for { for {
d.log.Logf("[%s] waiting for daemon to start", d.id) d.log.Logf("[%s] waiting for daemon to start", d.id)
select { select {
case <-timeout.C: case <-ctx.Done():
return errors.Errorf("[%s] Daemon exited and never started", d.id) return errors.Errorf("[%s] Daemon exited and never started: %s", d.id, ctx.Err())
case <-tick: case err := <-d.Wait:
ctx, cancel := context.WithTimeout(context.TODO(), 2*time.Second) return errors.Errorf("[%s] Daemon exited during startup: %v", d.id, err)
resp, err := client.Do(req.WithContext(ctx)) default:
cancel() rctx, rcancel := context.WithTimeout(context.TODO(), 2*time.Second)
defer rcancel()
resp, err := client.Do(req.WithContext(rctx))
if err != nil { if err != nil {
d.log.Logf("[%s] error pinging daemon on start: %v", d.id, err) d.log.Logf("[%s] error pinging daemon on start: %v", d.id, err)
time.Sleep(500 * time.Millisecond)
continue continue
} }
@ -340,8 +341,6 @@ func (d *Daemon) StartWithLogFile(out *os.File, providedArgs ...string) error {
return errors.Errorf("[%s] error querying daemon for root directory: %v", d.id, err) return errors.Errorf("[%s] error querying daemon for root directory: %v", d.id, err)
} }
return nil return nil
case err := <-d.Wait:
return errors.Errorf("[%s] Daemon exited during startup: %v", d.id, err)
} }
} }
} }