google.golang.org/grpc@v1.72.2/internal/leakcheck/leakcheck_test.go (about)

     1  /*
     2   *
     3   * Copyright 2017 gRPC authors.
     4   *
     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
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    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.
    16   *
    17   */
    18  
    19  package leakcheck
    20  
    21  import (
    22  	"context"
    23  	"fmt"
    24  	"strings"
    25  	"sync"
    26  	"testing"
    27  	"time"
    28  
    29  	"google.golang.org/grpc/internal"
    30  )
    31  
    32  type testLogger struct {
    33  	errorCount int
    34  	errors     []string
    35  }
    36  
    37  func (e *testLogger) Logf(string, ...any) {
    38  }
    39  
    40  func (e *testLogger) Errorf(format string, args ...any) {
    41  	e.errors = append(e.errors, fmt.Sprintf(format, args...))
    42  	e.errorCount++
    43  }
    44  
    45  func TestCheck(t *testing.T) {
    46  	const leakCount = 3
    47  	for i := 0; i < leakCount; i++ {
    48  		go func() { time.Sleep(2 * time.Second) }()
    49  	}
    50  	if ig := interestingGoroutines(); len(ig) == 0 {
    51  		t.Error("blah")
    52  	}
    53  	e := &testLogger{}
    54  	ctx, cancel := context.WithTimeout(context.Background(), time.Second)
    55  	defer cancel()
    56  	CheckGoroutines(ctx, e)
    57  	if e.errorCount != leakCount {
    58  		t.Errorf("CheckGoroutines found %v leaks, want %v leaks", e.errorCount, leakCount)
    59  		t.Logf("leaked goroutines:\n%v", strings.Join(e.errors, "\n"))
    60  	}
    61  	ctx, cancel = context.WithTimeout(context.Background(), 3*time.Second)
    62  	defer cancel()
    63  	CheckGoroutines(ctx, t)
    64  }
    65  
    66  func ignoredTestingLeak(d time.Duration) {
    67  	time.Sleep(d)
    68  }
    69  
    70  func TestCheckRegisterIgnore(t *testing.T) {
    71  	RegisterIgnoreGoroutine("ignoredTestingLeak")
    72  	const leakCount = 3
    73  	for i := 0; i < leakCount; i++ {
    74  		go func() { time.Sleep(2 * time.Second) }()
    75  	}
    76  	go func() { ignoredTestingLeak(3 * time.Second) }()
    77  	if ig := interestingGoroutines(); len(ig) == 0 {
    78  		t.Error("blah")
    79  	}
    80  	e := &testLogger{}
    81  	ctx, cancel := context.WithTimeout(context.Background(), time.Second)
    82  	defer cancel()
    83  	CheckGoroutines(ctx, e)
    84  	if e.errorCount != leakCount {
    85  		t.Errorf("CheckGoroutines found %v leaks, want %v leaks", e.errorCount, leakCount)
    86  		t.Logf("leaked goroutines:\n%v", strings.Join(e.errors, "\n"))
    87  	}
    88  	ctx, cancel = context.WithTimeout(context.Background(), 3*time.Second)
    89  	defer cancel()
    90  	CheckGoroutines(ctx, t)
    91  }
    92  
    93  // TestTrackTimers verifies that only leaked timers are reported and expired,
    94  // stopped timers are ignored.
    95  func TestTrackTimers(t *testing.T) {
    96  	TrackTimers()
    97  	const leakCount = 3
    98  	for i := 0; i < leakCount; i++ {
    99  		internal.TimeAfterFunc(2*time.Second, func() {
   100  			t.Logf("Timer %d fired.", i)
   101  		})
   102  	}
   103  	wg := sync.WaitGroup{}
   104  	// Let a couple of timers expire.
   105  	for i := 0; i < 2; i++ {
   106  		wg.Add(1)
   107  		internal.TimeAfterFunc(time.Millisecond, func() {
   108  			wg.Done()
   109  		})
   110  	}
   111  	wg.Wait()
   112  
   113  	// Stop a couple of timers.
   114  	for i := 0; i < leakCount; i++ {
   115  		t := internal.TimeAfterFunc(time.Hour, func() {
   116  			t.Error("Timer fired before test ended.")
   117  		})
   118  		t.Stop()
   119  	}
   120  
   121  	ctx, cancel := context.WithTimeout(context.Background(), time.Second)
   122  	defer cancel()
   123  	e := &testLogger{}
   124  	CheckTimers(ctx, e)
   125  	if e.errorCount != leakCount {
   126  		t.Errorf("CheckTimers found %v leaks, want %v leaks", e.errorCount, leakCount)
   127  		t.Logf("leaked timers:\n%v", strings.Join(e.errors, "\n"))
   128  	}
   129  	ctx, cancel = context.WithTimeout(context.Background(), 3*time.Second)
   130  	defer cancel()
   131  	CheckTimers(ctx, t)
   132  }