Skip to content
This repository was archived by the owner on Oct 13, 2023. It is now read-only.

Commit 5fd4461

Browse files
crosbymichaelthaJeztah
authored andcommitted
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> (cherry picked from commit 402433a5e4b8a74cab404658f34e6520e71df00a) Signed-off-by: Sebastiaan van Stijn <github@gone.nl> Upstream-commit: 78f4d6b84fdcacb3e359c491ada11ef2dce645c1 Component: engine
1 parent c7ae679 commit 5fd4461

1 file changed

Lines changed: 14 additions & 15 deletions

File tree

  • components/engine/internal/test/daemon

components/engine/internal/test/daemon/daemon.go

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ func (d *Daemon) StartWithLogFile(out *os.File, providedArgs ...string) error {
281281
return errors.Errorf("[%s] could not start daemon container: %v", d.id, err)
282282
}
283283

284-
wait := make(chan error)
284+
wait := make(chan error, 1)
285285

286286
go func() {
287287
ret := d.cmd.Wait()
@@ -309,26 +309,27 @@ func (d *Daemon) StartWithLogFile(out *os.File, providedArgs ...string) error {
309309
req.URL.Host = clientConfig.addr
310310
req.URL.Scheme = clientConfig.scheme
311311

312-
ticker := time.NewTicker(500 * time.Millisecond)
313-
defer ticker.Stop()
314-
tick := ticker.C
315-
316-
timeout := time.NewTimer(60 * time.Second) // timeout for the whole loop
317-
defer timeout.Stop()
312+
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
313+
defer cancel()
318314

319315
// make sure daemon is ready to receive requests
320316
for {
321317
d.log.Logf("[%s] waiting for daemon to start", d.id)
322318

323319
select {
324-
case <-timeout.C:
325-
return errors.Errorf("[%s] Daemon exited and never started", d.id)
326-
case <-tick:
327-
ctx, cancel := context.WithTimeout(context.TODO(), 2*time.Second)
328-
resp, err := client.Do(req.WithContext(ctx))
329-
cancel()
320+
case <-ctx.Done():
321+
return errors.Errorf("[%s] Daemon exited and never started: %s", d.id, ctx.Err())
322+
case err := <-d.Wait:
323+
return errors.Errorf("[%s] Daemon exited during startup: %v", d.id, err)
324+
default:
325+
rctx, rcancel := context.WithTimeout(context.TODO(), 2*time.Second)
326+
defer rcancel()
327+
328+
resp, err := client.Do(req.WithContext(rctx))
330329
if err != nil {
331330
d.log.Logf("[%s] error pinging daemon on start: %v", d.id, err)
331+
332+
time.Sleep(500 * time.Millisecond)
332333
continue
333334
}
334335

@@ -342,8 +343,6 @@ func (d *Daemon) StartWithLogFile(out *os.File, providedArgs ...string) error {
342343
return errors.Errorf("[%s] error querying daemon for root directory: %v", d.id, err)
343344
}
344345
return nil
345-
case err := <-d.Wait:
346-
return errors.Errorf("[%s] Daemon exited during startup: %v", d.id, err)
347346
}
348347
}
349348
}

0 commit comments

Comments
 (0)