Browse code

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>

Michael Crosby authored on 2019/07/03 05:16:33
Showing 1 changed files
... ...
@@ -279,7 +279,7 @@ func (d *Daemon) StartWithLogFile(out *os.File, providedArgs ...string) error {
279 279
 		return errors.Errorf("[%s] could not start daemon container: %v", d.id, err)
280 280
 	}
281 281
 
282
-	wait := make(chan error)
282
+	wait := make(chan error, 1)
283 283
 
284 284
 	go func() {
285 285
 		ret := d.cmd.Wait()
... ...
@@ -307,26 +307,27 @@ func (d *Daemon) StartWithLogFile(out *os.File, providedArgs ...string) error {
307 307
 	req.URL.Host = clientConfig.addr
308 308
 	req.URL.Scheme = clientConfig.scheme
309 309
 
310
-	ticker := time.NewTicker(500 * time.Millisecond)
311
-	defer ticker.Stop()
312
-	tick := ticker.C
313
-
314
-	timeout := time.NewTimer(60 * time.Second) // timeout for the whole loop
315
-	defer timeout.Stop()
310
+	ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
311
+	defer cancel()
316 312
 
317 313
 	// make sure daemon is ready to receive requests
318 314
 	for {
319 315
 		d.log.Logf("[%s] waiting for daemon to start", d.id)
320 316
 
321 317
 		select {
322
-		case <-timeout.C:
323
-			return errors.Errorf("[%s] Daemon exited and never started", d.id)
324
-		case <-tick:
325
-			ctx, cancel := context.WithTimeout(context.TODO(), 2*time.Second)
326
-			resp, err := client.Do(req.WithContext(ctx))
327
-			cancel()
318
+		case <-ctx.Done():
319
+			return errors.Errorf("[%s] Daemon exited and never started: %s", d.id, ctx.Err())
320
+		case err := <-d.Wait:
321
+			return errors.Errorf("[%s] Daemon exited during startup: %v", d.id, err)
322
+		default:
323
+			rctx, rcancel := context.WithTimeout(context.TODO(), 2*time.Second)
324
+			defer rcancel()
325
+
326
+			resp, err := client.Do(req.WithContext(rctx))
328 327
 			if err != nil {
329 328
 				d.log.Logf("[%s] error pinging daemon on start: %v", d.id, err)
329
+
330
+				time.Sleep(500 * time.Millisecond)
330 331
 				continue
331 332
 			}
332 333
 
... ...
@@ -340,8 +341,6 @@ func (d *Daemon) StartWithLogFile(out *os.File, providedArgs ...string) error {
340 340
 				return errors.Errorf("[%s] error querying daemon for root directory: %v", d.id, err)
341 341
 			}
342 342
 			return nil
343
-		case err := <-d.Wait:
344
-			return errors.Errorf("[%s] Daemon exited during startup: %v", d.id, err)
345 343
 		}
346 344
 	}
347 345
 }