1
// Copyright (c) 2017 Uber Technologies, Inc.
2
//
3
// Permission is hereby granted, free of charge, to any person obtaining a copy
4
// of this software and associated documentation files (the "Software"), to deal
5
// in the Software without restriction, including without limitation the rights
6
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
// copies of the Software, and to permit persons to whom the Software is
8
// furnished to do so, subject to the following conditions:
9
//
10
// The above copyright notice and this permission notice shall be included in
11
// all copies or substantial portions of the Software.
12
//
13
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
// THE SOFTWARE.
20

21
package goleak
22

23
import (
24
	"fmt"
25
	"io"
26
	"os"
27
)
28

29
// Variables for stubbing in unit tests.
30
var (
31
	_osExit             = os.Exit
32
	_osStderr io.Writer = os.Stderr
33
)
34

35
// TestingM is the minimal subset of testing.M that we use.
36
type TestingM interface {
37
	Run() int
38
}
39

40
// VerifyTestMain can be used in a TestMain function for package tests to
41
// verify that there were no goroutine leaks.
42
// To use it, your TestMain function should look like:
43
//
44
//  func TestMain(m *testing.M) {
45
//    goleak.VerifyTestMain(m)
46
//  }
47
//
48
// See https://golang.org/pkg/testing/#hdr-Main for more details.
49
//
50
// This will run all tests as per normal, and if they were successful, look
51
// for any goroutine leaks and fail the tests if any leaks were found.
52
func VerifyTestMain(m TestingM, options ...Option) {
53 2
	exitCode := m.Run()
54

55 2
	if exitCode == 0 {
56 2
		if err := Find(options...); err != nil {
57 2
			fmt.Fprintf(_osStderr, "goleak: Errors on successful test run: %v\n", err)
58 2
			exitCode = 1
59
		}
60
	}
61

62 2
	_osExit(exitCode)
63
}

Read our documentation on viewing source code .

Loading