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(logger Logger, timeout time.Duration)

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 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 method 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.

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.

func (*LeakChecker) Check

func (lc *LeakChecker) Check()

Check executes the leak check tests, failing the unit test if any buffer or goroutine leaks are detected.

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.70.0 (latest)
Published
Jan 23, 2025
Platform
linux/amd64
Imports
11 packages
Last checked
4 minutes ago

Tools for package owners.