Browse code

integration-cli: move goroutines info helpers to separate funcs

Signed-off-by: Alexander Morozov <lk4d4@docker.com>

Alexander Morozov authored on 2016/03/15 07:52:50
Showing 3 changed files
... ...
@@ -1,7 +1,6 @@
1 1
 package main
2 2
 
3 3
 import (
4
-	"encoding/json"
5 4
 	"fmt"
6 5
 	"io"
7 6
 	"os/exec"
... ...
@@ -265,20 +264,8 @@ func (s *DockerSuite) TestLogsFollowGoroutinesWithStdout(c *check.C) {
265 265
 	id := strings.TrimSpace(out)
266 266
 	c.Assert(waitRun(id), checker.IsNil)
267 267
 
268
-	type info struct {
269
-		NGoroutines int
270
-	}
271
-	getNGoroutines := func() int {
272
-		var i info
273
-		status, b, err := sockRequest("GET", "/info", nil)
274
-		c.Assert(err, checker.IsNil)
275
-		c.Assert(status, checker.Equals, 200)
276
-		c.Assert(json.Unmarshal(b, &i), checker.IsNil)
277
-		return i.NGoroutines
278
-	}
279
-
280
-	nroutines := getNGoroutines()
281
-
268
+	nroutines, err := getGoroutineNumber()
269
+	c.Assert(err, checker.IsNil)
282 270
 	cmd := exec.Command(dockerBinary, "logs", "-f", id)
283 271
 	r, w := io.Pipe()
284 272
 	cmd.Stdout = w
... ...
@@ -295,20 +282,7 @@ func (s *DockerSuite) TestLogsFollowGoroutinesWithStdout(c *check.C) {
295 295
 	c.Assert(cmd.Process.Kill(), checker.IsNil)
296 296
 
297 297
 	// NGoroutines is not updated right away, so we need to wait before failing
298
-	t := time.After(30 * time.Second)
299
-	for {
300
-		select {
301
-		case <-t:
302
-			n := getNGoroutines()
303
-			c.Assert(n <= nroutines, checker.Equals, true, check.Commentf("leaked goroutines: expected less than or equal to %d, got: %d", nroutines, n))
304
-
305
-		default:
306
-			if n := getNGoroutines(); n <= nroutines {
307
-				return
308
-			}
309
-			time.Sleep(200 * time.Millisecond)
310
-		}
311
-	}
298
+	c.Assert(waitForGoroutines(nroutines), checker.IsNil)
312 299
 }
313 300
 
314 301
 func (s *DockerSuite) TestLogsFollowGoroutinesNoOutput(c *check.C) {
... ...
@@ -316,40 +290,15 @@ func (s *DockerSuite) TestLogsFollowGoroutinesNoOutput(c *check.C) {
316 316
 	id := strings.TrimSpace(out)
317 317
 	c.Assert(waitRun(id), checker.IsNil)
318 318
 
319
-	type info struct {
320
-		NGoroutines int
321
-	}
322
-	getNGoroutines := func() int {
323
-		var i info
324
-		status, b, err := sockRequest("GET", "/info", nil)
325
-		c.Assert(err, checker.IsNil)
326
-		c.Assert(status, checker.Equals, 200)
327
-		c.Assert(json.Unmarshal(b, &i), checker.IsNil)
328
-		return i.NGoroutines
329
-	}
330
-
331
-	nroutines := getNGoroutines()
332
-
319
+	nroutines, err := getGoroutineNumber()
320
+	c.Assert(err, checker.IsNil)
333 321
 	cmd := exec.Command(dockerBinary, "logs", "-f", id)
334 322
 	c.Assert(cmd.Start(), checker.IsNil)
335 323
 	time.Sleep(200 * time.Millisecond)
336 324
 	c.Assert(cmd.Process.Kill(), checker.IsNil)
337 325
 
338 326
 	// NGoroutines is not updated right away, so we need to wait before failing
339
-	t := time.After(30 * time.Second)
340
-	for {
341
-		select {
342
-		case <-t:
343
-			n := getNGoroutines()
344
-			c.Assert(n <= nroutines, checker.Equals, true, check.Commentf("leaked goroutines: expected less than or equal to %d, got: %d", nroutines, n))
345
-
346
-		default:
347
-			if n := getNGoroutines(); n <= nroutines {
348
-				return
349
-			}
350
-			time.Sleep(200 * time.Millisecond)
351
-		}
352
-	}
327
+	c.Assert(waitForGoroutines(nroutines), checker.IsNil)
353 328
 }
354 329
 
355 330
 func (s *DockerSuite) TestLogsCLIContainerNotFound(c *check.C) {
... ...
@@ -3,7 +3,6 @@ package main
3 3
 import (
4 4
 	"bufio"
5 5
 	"bytes"
6
-	"encoding/json"
7 6
 	"fmt"
8 7
 	"io/ioutil"
9 8
 	"net"
... ...
@@ -4223,18 +4222,8 @@ func (s *DockerSuite) TestRunNamedVolumesFromNotRemoved(c *check.C) {
4223 4223
 }
4224 4224
 
4225 4225
 func (s *DockerSuite) TestRunAttachFailedNoLeak(c *check.C) {
4226
-	type info struct {
4227
-		NGoroutines int
4228
-	}
4229
-	getNGoroutines := func() int {
4230
-		var i info
4231
-		status, b, err := sockRequest("GET", "/info", nil)
4232
-		c.Assert(err, checker.IsNil)
4233
-		c.Assert(status, checker.Equals, 200)
4234
-		c.Assert(json.Unmarshal(b, &i), checker.IsNil)
4235
-		return i.NGoroutines
4236
-	}
4237
-	nroutines := getNGoroutines()
4226
+	nroutines, err := getGoroutineNumber()
4227
+	c.Assert(err, checker.IsNil)
4238 4228
 
4239 4229
 	runSleepingContainer(c, "--name=test", "-p", "8000:8000")
4240 4230
 
... ...
@@ -4245,18 +4234,5 @@ func (s *DockerSuite) TestRunAttachFailedNoLeak(c *check.C) {
4245 4245
 	dockerCmd(c, "rm", "-f", "test")
4246 4246
 
4247 4247
 	// NGoroutines is not updated right away, so we need to wait before failing
4248
-	t := time.After(30 * time.Second)
4249
-	for {
4250
-		select {
4251
-		case <-t:
4252
-			n := getNGoroutines()
4253
-			c.Assert(n <= nroutines, checker.Equals, true, check.Commentf("leaked goroutines: expected less than or equal to %d, got: %d", nroutines, n))
4254
-
4255
-		default:
4256
-			if n := getNGoroutines(); n <= nroutines {
4257
-				return
4258
-			}
4259
-			time.Sleep(200 * time.Millisecond)
4260
-		}
4261
-	}
4248
+	c.Assert(waitForGoroutines(nroutines), checker.IsNil)
4262 4249
 }
... ...
@@ -1435,3 +1435,45 @@ func minimalBaseImage() string {
1435 1435
 	}
1436 1436
 	return "scratch"
1437 1437
 }
1438
+
1439
+func getGoroutineNumber() (int, error) {
1440
+	i := struct {
1441
+		NGoroutines int
1442
+	}{}
1443
+	status, b, err := sockRequest("GET", "/info", nil)
1444
+	if err != nil {
1445
+		return 0, err
1446
+	}
1447
+	if status != http.StatusOK {
1448
+		return 0, fmt.Errorf("http status code: %d", status)
1449
+	}
1450
+	if err := json.Unmarshal(b, &i); err != nil {
1451
+		return 0, err
1452
+	}
1453
+	return i.NGoroutines, nil
1454
+}
1455
+
1456
+func waitForGoroutines(expected int) error {
1457
+	t := time.After(30 * time.Second)
1458
+	for {
1459
+		select {
1460
+		case <-t:
1461
+			n, err := getGoroutineNumber()
1462
+			if err != nil {
1463
+				return err
1464
+			}
1465
+			if n > expected {
1466
+				return fmt.Errorf("leaked goroutines: expected less than or equal to %d, got: %d", expected, n)
1467
+			}
1468
+		default:
1469
+			n, err := getGoroutineNumber()
1470
+			if err != nil {
1471
+				return err
1472
+			}
1473
+			if n <= expected {
1474
+				return nil
1475
+			}
1476
+			time.Sleep(200 * time.Millisecond)
1477
+		}
1478
+	}
1479
+}