Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AssertSocketSameNetNS ¶ added in v24.0.2
AssertSocketSameNetNS makes a best-effort attempt to assert that conn is in the same network namespace as the current goroutine's thread.
func SetupTestOSContext ¶
SetupTestOSContext joins the current goroutine to a new network namespace, and returns its associated teardown function.
Example usage:
defer SetupTestOSContext(t)()
Types ¶
type OSContext ¶
type OSContext struct {
// contains filtered or unexported fields
}
OSContext is a handle to a test OS context.
func SetupTestOSContextEx ¶
SetupTestOSContextEx joins the current goroutine to a new network namespace.
Compared to SetupTestOSContext, this function allows goroutines to be spawned which are associated with the same OS context via the returned OSContext value.
Example usage:
c := SetupTestOSContext(t) defer c.Cleanup(t)
func (*OSContext) Cleanup ¶
Cleanup tears down the OS context. It must be called from the same goroutine as the SetupTestOSContextEx call which returned c.
Explicit cleanup is required as (*testing.T).Cleanup() makes no guarantees about which goroutine the cleanup functions are invoked on.
func (*OSContext) Set ¶
Set sets the OS context of the calling goroutine to c and returns a teardown function to restore the calling goroutine's OS context and release resources. The teardown function accepts an optional Logger argument.
This is a lower-level interface which is less ergonomic than c.Go() but more composable with other goroutine-spawning utilities such as sync.WaitGroup or golang.org/x/sync/errgroup.Group.
Example usage:
func TestFoo(t *testing.T) { osctx := testutils.SetupTestOSContextEx(t) defer osctx.Cleanup(t) var eg errgroup.Group eg.Go(func() error { teardown, err := osctx.Set() if err != nil { return err } defer teardown(t) // ... }) if err := eg.Wait(); err != nil { t.Fatalf("%+v", err) } }