3 * Copyright 2017 gRPC authors.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
28 type testErrorfer struct {
33 func (e *testErrorfer) Errorf(format string, args ...interface{}) {
34 e.errors = append(e.errors, fmt.Sprintf(format, args...))
38 func TestCheck(t *testing.T) {
40 for i := 0; i < leakCount; i++ {
41 go func() { time.Sleep(2 * time.Second) }()
43 if ig := interestingGoroutines(); len(ig) == 0 {
48 if e.errorCount != leakCount {
49 t.Errorf("check found %v leaks, want %v leaks", e.errorCount, leakCount)
50 t.Logf("leaked goroutines:\n%v", strings.Join(e.errors, "\n"))
52 check(t, 3*time.Second)
55 func ignoredTestingLeak(d time.Duration) {
59 func TestCheckRegisterIgnore(t *testing.T) {
60 RegisterIgnoreGoroutine("ignoredTestingLeak")
62 for i := 0; i < leakCount; i++ {
63 go func() { time.Sleep(2 * time.Second) }()
65 go func() { ignoredTestingLeak(3 * time.Second) }()
66 if ig := interestingGoroutines(); len(ig) == 0 {
71 if e.errorCount != leakCount {
72 t.Errorf("check found %v leaks, want %v leaks", e.errorCount, leakCount)
73 t.Logf("leaked goroutines:\n%v", strings.Join(e.errors, "\n"))
75 check(t, 3*time.Second)