OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 The Go Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style |
| 3 // license that can be found in the LICENSE file. |
| 4 |
| 5 package http_test |
| 6 |
| 7 import ( |
| 8 "net/http" |
| 9 "runtime" |
| 10 "strings" |
| 11 "testing" |
| 12 "time" |
| 13 ) |
| 14 |
| 15 // Verify the other tests didn't leave any goroutines running. |
| 16 // This is in a file named z_last_test.go so it sorts at the end. |
| 17 func TestGoroutinesRunning(t *testing.T) { |
| 18 n := runtime.NumGoroutine() |
| 19 t.Logf("num goroutines = %d", n) |
| 20 if n > 20 { |
| 21 // Currently 14 on Linux (blocked in epoll_wait, |
| 22 // waiting for on fds that are closed?), but give some |
| 23 // slop for now. |
| 24 buf := make([]byte, 1<<20) |
| 25 buf = buf[:runtime.Stack(buf, true)] |
| 26 t.Errorf("Too many goroutines:\n%s", buf) |
| 27 } |
| 28 } |
| 29 |
| 30 func checkLeakedTransports(t *testing.T) { |
| 31 http.DefaultTransport.(*http.Transport).CloseIdleConnections() |
| 32 if testing.Short() { |
| 33 return |
| 34 } |
| 35 buf := make([]byte, 1<<20) |
| 36 var stacks string |
| 37 var bad string |
| 38 badSubstring := map[string]string{ |
| 39 ").readLoop(": "a Transport", |
| 40 ").writeLoop(": "a Transport", |
| 41 "created by net/http/httptest.(*Server).Start": "an httptest.Ser
ver", |
| 42 "timeoutHandler": "a TimeoutHandle
r", |
| 43 } |
| 44 for i := 0; i < 4; i++ { |
| 45 bad = "" |
| 46 stacks = string(buf[:runtime.Stack(buf, true)]) |
| 47 for substr, what := range badSubstring { |
| 48 if strings.Contains(stacks, substr) { |
| 49 bad = what |
| 50 } |
| 51 } |
| 52 if bad == "" { |
| 53 return |
| 54 } |
| 55 // Bad stuff found, but goroutines might just still be |
| 56 // shutting down, so give it some time. |
| 57 time.Sleep(250 * time.Millisecond) |
| 58 } |
| 59 t.Errorf("Test appears to have leaked %s:\n%s", bad, stacks) |
| 60 } |
OLD | NEW |