package leakcheck

import "google.golang.org/grpc/internal/leakcheck"

Package leakcheck contains functions to check leaked goroutines and buffers.

Call the following at the beginning of test:

defer leakcheck.NewLeakChecker(t).Check()

Index

Functions

func CheckGoroutines

func CheckGoroutines(ctx context.Context, logger Logger)

CheckGoroutines looks at the currently-running goroutines and checks if there are any interesting (created by gRPC) goroutines leaked. It waits up to 10 seconds in the error cases.

func CheckTimers

func CheckTimers(ctx context.Context, logger Logger)

CheckTimers undoes the effects of TrackTimers, and fails unit tests if not all timers were cancelled or executed. It is invalid to invoke this function without previously having invoked TrackTimers.

func CheckTrackingBufferPool

func CheckTrackingBufferPool()

CheckTrackingBufferPool undoes the effects of SetTrackingBufferPool, and fails unit tests if not all buffers were returned. It is invalid to invoke this function without previously having invoked SetTrackingBufferPool.

func RegisterIgnoreGoroutine

func RegisterIgnoreGoroutine(s string)

RegisterIgnoreGoroutine appends s into the ignore goroutine list. The goroutines whose stack trace contains s will not be identified as leaked goroutines. Not thread-safe, only call this function in init().

func SetTrackingBufferPool

func SetTrackingBufferPool(logger Logger)

SetTrackingBufferPool replaces the default buffer pool in the mem package to one that tracks where buffers are allocated. CheckTrackingBufferPool should then be invoked at the end of the test to validate that all buffers pulled from the pool were returned.

func TrackTimers

func TrackTimers()

TrackTimers replaces internal.TimerAfterFunc with one that tracks timer creations, stoppages and expirations. CheckTimers should then be invoked at the end of the test to validate that all timers created have either executed or are cancelled.

Types

type LeakChecker

type LeakChecker struct {
	// contains filtered or unexported fields
}

LeakChecker captures a Logger and is returned by NewLeakChecker as a convenient method to set up leak check tests in a unit test.

func NewLeakChecker

func NewLeakChecker(logger Logger) *LeakChecker

NewLeakChecker offers a convenient way to set up the leak checks for a specific unit test. It can be used as follows, at the beginning of tests:

defer leakcheck.NewLeakChecker(t).Check()

It initially invokes SetTrackingBufferPool to set up buffer tracking, then the deferred LeakChecker.Check call will invoke CheckTrackingBufferPool and CheckGoroutines with a default timeout of 10 seconds.

type Logger

type Logger interface {
	Logf(format string, args ...any)
	Errorf(format string, args ...any)
}

Logger is the interface that wraps the Logf and Errorf method. It's a subset of testing.TB to make it easy to use this package.

Source Files

leakcheck.go

Version
v1.72.0 (latest)
Published
Apr 21, 2025
Platform
linux/amd64
Imports
13 packages
Last checked
1 minute ago

Tools for package owners.