leakcheck

package
v1.69.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 18, 2024 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package leakcheck contains functions to check leaked goroutines and buffers.

Call the following at the beginning of test:

defer leakcheck.NewLeakChecker(t).Check()

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckGoroutines added in v1.66.0

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 added in v1.66.0

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 added in v1.66.0

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 added in v1.66.0

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 added in v1.66.0

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 added in v1.66.0

func (lc *LeakChecker) Check()

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

type Logger added in v1.66.0

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL