]> Cypherpunks repositories - gostls13.git/commitdiff
net: add test for runtime.PollDesc leak
authorDave Cheney <dave@cheney.net>
Tue, 9 Apr 2013 01:14:22 +0000 (11:14 +1000)
committerDave Cheney <dave@cheney.net>
Tue, 9 Apr 2013 01:14:22 +0000 (11:14 +1000)
See 8318044

R=bradfitz
CC=golang-dev
https://golang.org/cl/8547043

src/pkg/net/dial_test.go

index 098df738b627dae4330b8f45c79a17fe7227c66a..62b9a6843eb44761604cedd5375745e8c4970c44 100644 (file)
@@ -330,3 +330,45 @@ func numFD() int {
        // All tests using this should be skipped anyway, but:
        panic("numFDs not implemented on " + runtime.GOOS)
 }
+
+// Assert that a failed Dial attempt does not leak
+// runtime.PollDesc structures
+func TestDialPollDescLeak(t *testing.T) {
+       // remove once CL 8318044 is submitted
+       t.Skip("Test skipped pending submission of CL 8318044")
+
+       if testing.Short() {
+               t.Skip("skipping PollDesc leak test in -short mode")
+       }
+
+       const loops = 10
+       const count = 20000
+       var old runtime.MemStats // used by sysdelta
+       runtime.ReadMemStats(&old)
+       sysdelta := func() uint64 {
+               var new runtime.MemStats
+               runtime.ReadMemStats(&new)
+               delta := old.Sys - new.Sys
+               old = new
+               return delta
+       }
+       failcount := 0
+       for i := 0; i < loops; i++ {
+               for i := 0; i < count; i++ {
+                       conn, err := Dial("tcp", "127.0.0.1:1")
+                       if err == nil {
+                               t.Error("dial should not succeed")
+                               conn.Close()
+                               t.FailNow()
+                       }
+               }
+               if delta := sysdelta(); delta > 0 {
+                       failcount++
+               }
+               // there are always some allocations on the first loop
+               if failcount > 3 {
+                       t.Error("net.Dial leaked memory")
+                       t.FailNow()
+               }
+       }
+}