Browse code

Merge pull request #21196 from LK4D4/detect_leaks_util

integration-cli: move goroutines info helpers to separate funcs

Sebastiaan van Stijn authored on 2016/03/15 20:42:47
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"
... ...
@@ -4219,18 +4218,8 @@ func (s *DockerSuite) TestRunNamedVolumesFromNotRemoved(c *check.C) {
4219 4219
 }
4220 4220
 
4221 4221
 func (s *DockerSuite) TestRunAttachFailedNoLeak(c *check.C) {
4222
-	type info struct {
4223
-		NGoroutines int
4224
-	}
4225
-	getNGoroutines := func() int {
4226
-		var i info
4227
-		status, b, err := sockRequest("GET", "/info", nil)
4228
-		c.Assert(err, checker.IsNil)
4229
-		c.Assert(status, checker.Equals, 200)
4230
-		c.Assert(json.Unmarshal(b, &i), checker.IsNil)
4231
-		return i.NGoroutines
4232
-	}
4233
-	nroutines := getNGoroutines()
4222
+	nroutines, err := getGoroutineNumber()
4223
+	c.Assert(err, checker.IsNil)
4234 4224
 
4235 4225
 	runSleepingContainer(c, "--name=test", "-p", "8000:8000")
4236 4226
 
... ...
@@ -4241,18 +4230,5 @@ func (s *DockerSuite) TestRunAttachFailedNoLeak(c *check.C) {
4241 4241
 	dockerCmd(c, "rm", "-f", "test")
4242 4242
 
4243 4243
 	// NGoroutines is not updated right away, so we need to wait before failing
4244
-	t := time.After(30 * time.Second)
4245
-	for {
4246
-		select {
4247
-		case <-t:
4248
-			n := getNGoroutines()
4249
-			c.Assert(n <= nroutines, checker.Equals, true, check.Commentf("leaked goroutines: expected less than or equal to %d, got: %d", nroutines, n))
4250
-
4251
-		default:
4252
-			if n := getNGoroutines(); n <= nroutines {
4253
-				return
4254
-			}
4255
-			time.Sleep(200 * time.Millisecond)
4256
-		}
4257
-	}
4244
+	c.Assert(waitForGoroutines(nroutines), checker.IsNil)
4258 4245
 }
... ...
@@ -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
+}