usercode

package
v0.0.0-...-b5d9cbe Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2024 License: BSD-3-Clause Imports: 4 Imported by: 0

Documentation

Overview

Package usercode provides utilities to interact with user-defined code.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ForceErrorForTesting

func ForceErrorForTesting(err error)

ForceErrorForTesting always panics. If the current function is called by SafeCall, it forces SafeCall to return an error. This function is to be used by unit tests which want to simulate SafeCall errors reliably.

func SafeCall

func SafeCall(ctx context.Context, name string, timeout, gracePeriod time.Duration, ph PanicHandler, f func(ctx context.Context)) error

SafeCall runs a function f on a goroutine to protect callers from its possible bad behavior.

SafeCall calls f with a context having a specified timeout. If f does not return before the timeout, SafeCall further waits for gracePeriod to allow some clean up. If f does not return after timeout + gracePeriod or ctx is canceled before f finishes, SafeCall abandons the goroutine and immediately returns an error. name is included in an error message to explain which user code did not return.

If f panics, SafeCall calls a panic handler ph to handle it. SafeCall will not call ph if it decides to abandon f, even if f panics later.

If f calls runtime.Goexit, it is handled just like the function returns normally.

SafeCall returns an error only if execution of f was abandoned for some reasons (e.g. f ignored the timeout, ctx was canceled). In other cases, it returns nil.

Types

type ErrorReporter

type ErrorReporter interface {
	Error(args ...interface{})
}

ErrorReporter is the interface for reporting errors. It is implemented by testing.State and its sibling types.

type PanicHandler

type PanicHandler func(val interface{})

PanicHandler specifies how to handle panics in SafeCall.

func ErrorOnPanic

func ErrorOnPanic(e ErrorReporter) PanicHandler

ErrorOnPanic returns a PanicHandler that reports a panic via e.

Jump to

Keyboard shortcuts

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