github.com/akaros/go-akaros@v0.0.0-20181004170632-85005d477eab/src/net/http/main_test.go (about)

     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  	"fmt"
     9  	"net/http"
    10  	"os"
    11  	"runtime"
    12  	"sort"
    13  	"strings"
    14  	"testing"
    15  	"time"
    16  )
    17  
    18  func TestMain(m *testing.M) {
    19  	v := m.Run()
    20  	if v == 0 && goroutineLeaked() {
    21  		os.Exit(1)
    22  	}
    23  	os.Exit(v)
    24  }
    25  
    26  func interestingGoroutines() (gs []string) {
    27  	buf := make([]byte, 2<<20)
    28  	buf = buf[:runtime.Stack(buf, true)]
    29  	for _, g := range strings.Split(string(buf), "\n\n") {
    30  		sl := strings.SplitN(g, "\n", 2)
    31  		if len(sl) != 2 {
    32  			continue
    33  		}
    34  		stack := strings.TrimSpace(sl[1])
    35  		if stack == "" ||
    36  			strings.Contains(stack, "created by net.startServer") ||
    37  			strings.Contains(stack, "created by testing.RunTests") ||
    38  			strings.Contains(stack, "closeWriteAndWait") ||
    39  			strings.Contains(stack, "testing.Main(") ||
    40  			strings.Contains(stack, "runtime/parlib.process_signals()") ||
    41  			// These only show up with GOTRACEBACK=2; Issue 5005 (comment 28)
    42  			strings.Contains(stack, "runtime.goexit") ||
    43  			strings.Contains(stack, "created by runtime.gc") ||
    44  			strings.Contains(stack, "net/http_test.interestingGoroutines") ||
    45  			strings.Contains(stack, "runtime.MHeap_Scavenger") {
    46  			continue
    47  		}
    48  		gs = append(gs, stack)
    49  	}
    50  	sort.Strings(gs)
    51  	return
    52  }
    53  
    54  // Verify the other tests didn't leave any goroutines running.
    55  func goroutineLeaked() bool {
    56  	if testing.Short() {
    57  		// not counting goroutines for leakage in -short mode
    58  		return false
    59  	}
    60  	gs := interestingGoroutines()
    61  
    62  	n := 0
    63  	stackCount := make(map[string]int)
    64  	for _, g := range gs {
    65  		stackCount[g]++
    66  		n++
    67  	}
    68  
    69  	if n == 0 {
    70  		return false
    71  	}
    72  	fmt.Fprintf(os.Stderr, "Too many goroutines running after net/http test(s).\n")
    73  	for stack, count := range stackCount {
    74  		fmt.Fprintf(os.Stderr, "%d instances of:\n%s\n", count, stack)
    75  	}
    76  	return true
    77  }
    78  
    79  func afterTest(t *testing.T) {
    80  	http.DefaultTransport.(*http.Transport).CloseIdleConnections()
    81  	if testing.Short() {
    82  		return
    83  	}
    84  	var bad string
    85  	badSubstring := map[string]string{
    86  		").readLoop(":                                  "a Transport",
    87  		").writeLoop(":                                 "a Transport",
    88  		"created by net/http/httptest.(*Server).Start": "an httptest.Server",
    89  		"timeoutHandler":                               "a TimeoutHandler",
    90  		"net.(*netFD).connect(":                        "a timing out dial",
    91  		").noteClientGone(":                            "a closenotifier sender",
    92  	}
    93  	var stacks string
    94  	for i := 0; i < 4; i++ {
    95  		bad = ""
    96  		stacks = strings.Join(interestingGoroutines(), "\n\n")
    97  		for substr, what := range badSubstring {
    98  			if strings.Contains(stacks, substr) {
    99  				bad = what
   100  			}
   101  		}
   102  		if bad == "" {
   103  			return
   104  		}
   105  		// Bad stuff found, but goroutines might just still be
   106  		// shutting down, so give it some time.
   107  		time.Sleep(250 * time.Millisecond)
   108  	}
   109  	t.Errorf("Test appears to have leaked %s:\n%s", bad, stacks)
   110  }