testhook

package
v3.35.0 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2023 License: Apache-2.0, Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Copyright 2022 Molecula Corp. (DBA FeatureBase). SPDX-License-Identifier: Apache-2.0

Copyright 2022 Molecula Corp. (DBA FeatureBase). SPDX-License-Identifier: Apache-2.0

Copyright 2022 Molecula Corp. (DBA FeatureBase). SPDX-License-Identifier: Apache-2.0

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Cleanup

func Cleanup(tb testing.TB, fn func())

Cleanup in 1.13 logs a message about skipping a cleanup function, but allows things to build. Cleanup in 1.14 uses tb.Cleanup to register a cleanup function to call when a test completes.

func Closed

func Closed(a Auditor, o interface{}, kv KV) error

Closed(a, o, kv) is shorthand for a.Registry(o).Closed(o, kv) plus the error checking inside that.

func Created

func Created(a Auditor, o interface{}, kv KV) error

Created(a, o, kv) is shorthand for a.Registry(o).Created(o, kv) plus the error checking inside that.

func Destroyed

func Destroyed(a Auditor, o interface{}, kv KV) error

Destroyed(a, o, kv) is shorthand for a.Registry(o).Destroyed(o, kv) plus the error checking inside that.

func Opened

func Opened(a Auditor, o interface{}, kv KV) error

Opened(a, o, kv) is shorthand for a.Registry(o).Opened(o, kv) plus the error checking inside that.

func RegisterPostTestHook

func RegisterPostTestHook(fn Callback)

RegisterPostTestHook registers a function to be called after tests are run. It should return a nil error if it's okay, and a non-nil error to cause a non-zero exit status.

func RegisterPreTestHook

func RegisterPreTestHook(fn Callback)

RegisterPreTestHook registers a function to be called after tests are run. It should return a nil error if it's okay, and a non-nil error to cause a non-zero exit status.

func RunTestsWithHooks

func RunTestsWithHooks(m *testing.M)

RunTestsWithHooks is a suitable implementation for TestMain; you can just invoke this from your TestMain, passing in m, and it runs the tests and then runs any registered pre/post hooks. If the hooks themselves try to register hooks, you will deadlock. Don't do that.

func Seen

func Seen(a Auditor, o interface{}, kv KV) error

Seen(a, o, kv) is shorthand for a.Registry(o).Seen(o, kv) plus the error checking inside that.

func TempDir

func TempDir(tb testing.TB, pattern string) (path string, err error)

TempDir creates a temp directory that will be automatically deleted when this test completes, using go1.14's [TB].Cleanup() if available.

func TempDirInDir

func TempDirInDir(tb testing.TB, dir string, pattern string) (path string, err error)

TempDirInDir creates a temp directory that will be automatically deleted when this test completes, using go1.14's [TB].Cleanup(), but with a specified path instead of the default Go TMPDIR. Only some tests use this, which is possibly an error...

func TempFile

func TempFile(tb testing.TB, pattern string) (file *os.File, err error)

TempFile creates a temp file that will be automatically deleted when this test completes, using go1.14's [TB].Cleanup() if available.

func TempFileInDir

func TempFileInDir(tb testing.TB, dir string, pattern string) (file *os.File, err error)

TempFileInDir creates a temp file that will be automatically deleted when this test completes, using go1.14's [TB].Cleanup(), but with a specified path instead of the default Go TMPDIR. Only some tests use this, which is possibly an error...

Types

type Auditor

type Auditor interface {

	// Registry yields a registry for objects of this type.
	// multiple calls with the same object type yield the same registry.
	Registry(interface{}) (Registry, error)
	// Check performs any error-checking that can be done during
	// usage.
	Check() (error, []error)
	// FinalCheck performs any error-checking that makes sense only
	// after all operations are supposed to be complete, such as
	// verifying that opened objects have been closed.
	FinalCheck() (error, []error)
}

Auditor represents a thing which knows how to audit events. For instance, it can check on when things are accessed, or when they were opened, or whether opened objects are later closed.

type Callback

type Callback func() error

Callback denotes a function which can be run on a testing.T, or testing.B, which performs additional functions typically before or after tests.

type KV

type KV map[string]interface{}

KV represents a key/value mapping. You don't need to use the type for anything, it's just to make typing easier.

type LiveHook

type LiveHook func(interface{}, *RegistryEntry) error

type NopAuditor

type NopAuditor struct{}

NopAuditor doesn't do anything.

func NewNopAuditor

func NewNopAuditor() *NopAuditor

func (*NopAuditor) Check

func (*NopAuditor) Check() (error, []error)

func (*NopAuditor) FinalCheck

func (*NopAuditor) FinalCheck() (error, []error)

func (*NopAuditor) Registry

func (*NopAuditor) Registry(interface{}) (Registry, error)

type NopRegistry

type NopRegistry struct{}

NopRegistry doesn't do anything; it exists to fit the interface but not consume resources.

func NewNopRegistry

func NewNopRegistry() *NopRegistry

func (*NopRegistry) Closed

func (*NopRegistry) Closed(interface{}, KV) error

func (*NopRegistry) Created

func (*NopRegistry) Created(interface{}, KV) error

func (*NopRegistry) Destroyed

func (*NopRegistry) Destroyed(interface{}, KV) error

func (*NopRegistry) Live

func (*NopRegistry) Live() (map[interface{}]*RegistryEntry, error)

func (*NopRegistry) Opened

func (*NopRegistry) Opened(interface{}, KV) error

func (*NopRegistry) Seen

func (*NopRegistry) Seen(interface{}, KV) error

type PluggableRegistry

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

PluggableRegistry is a registry which takes dynamically-generated functions, and calls them if they're not nil.

func Compose

func Compose(hooks ...RegistryHook) *PluggableRegistry

Compose takes a list of RegistryHook objects, and combines their hook functions into a unified registry. For Pre hooks and Live hooks, the functions are called in the order they were provided to this function, and the first one to return an error causes the remainder to be skipped; for Post hooks, they are called in the opposite order, and the whole list continues to be called, with each function getting passed the error (or non-error) value returned by the previous one.

func (*PluggableRegistry) Closed

func (p *PluggableRegistry) Closed(i interface{}, kv KV, ent *RegistryEntry) error

func (*PluggableRegistry) Created

func (p *PluggableRegistry) Created(i interface{}, kv KV, ent *RegistryEntry) error

func (*PluggableRegistry) Destroyed

func (p *PluggableRegistry) Destroyed(i interface{}, kv KV, ent *RegistryEntry) error

func (*PluggableRegistry) Live

func (p *PluggableRegistry) Live(i interface{}, ent *RegistryEntry) error

func (*PluggableRegistry) Opened

func (p *PluggableRegistry) Opened(i interface{}, kv KV, ent *RegistryEntry) error

func (*PluggableRegistry) Seen

func (p *PluggableRegistry) Seen(i interface{}, kv KV, ent *RegistryEntry) error

func (*PluggableRegistry) WasClosed

func (p *PluggableRegistry) WasClosed(i interface{}, kv KV, ent *RegistryEntry, err error) error

func (*PluggableRegistry) WasCreated

func (p *PluggableRegistry) WasCreated(i interface{}, kv KV, ent *RegistryEntry, err error) error

func (*PluggableRegistry) WasDestroyed

func (p *PluggableRegistry) WasDestroyed(i interface{}, kv KV, ent *RegistryEntry, err error) error

func (*PluggableRegistry) WasOpened

func (p *PluggableRegistry) WasOpened(i interface{}, kv KV, ent *RegistryEntry, err error) error

func (*PluggableRegistry) WasSeen

func (p *PluggableRegistry) WasSeen(i interface{}, kv KV, ent *RegistryEntry, err error) error

type PostHook

type PostHook func(interface{}, KV, *RegistryEntry, error) error

type PreHook

type PreHook func(interface{}, KV, *RegistryEntry) error

type Registry

type Registry interface {
	Created(interface{}, KV) error                 // Object has been created, and must be destroyed later.
	Opened(interface{}, KV) error                  // Object has been opened, and must be closed later.
	Seen(interface{}, KV) error                    // Object has been interacted with, and should be between an open and a close.
	Closed(interface{}, KV) error                  // Object has been closed, and must have been opened previously.
	Destroyed(interface{}, KV) error               // Object has been destroyed
	Live() (map[interface{}]*RegistryEntry, error) // Currently live objects
}

Registry represents a set of known objects of a given common type. A typical implementation might maintain a map of objects it's seen which haven't been deleted. A minimal implementation just does nothing.

A Registry may implement reference-count semantics, or may treat reopening of a still-open object as an error.

All objects provided to a registry should have the same type.

type RegistryEntry

type RegistryEntry struct {
	Error     error
	Stack     []byte
	Stamp     time.Time
	Data      KV
	OpenCount int
}

RegistryEntry represents the data we might have about an entry. Every entry in it could be zero-valued in some implementations

type RegistryHook

type RegistryHook interface{}

RegistryHook is a generic interface, but any real implementation should implement at least one of RegistryHookPreOpen, RegistryHookPostOpen, etcetera. Pre-hooks are called before any error checks by the registry, post-hooks are called with after those error checks, and are passed the (possibly nil) error that would be returned at that point. If the post-hook overrides the error, the registry continues with operations as though it hadn't occurred, which may be a very bad idea.

type RegistryHookLive

type RegistryHookLive interface {
	Live(interface{}, *RegistryEntry) error
}

RegistryHookLive is an interface implemented by a registry hook.

If a registry's hooks implement RegistryHookLive, Live() should return only those entries for which a non-nil error was returned, with the error inserted in the RegistryEntry.

type RegistryHookPostClose

type RegistryHookPostClose interface {
	WasClosed(interface{}, KV, *RegistryEntry, error) error
}

type RegistryHookPostCreate

type RegistryHookPostCreate interface {
	WasCreated(interface{}, KV, *RegistryEntry, error) error
}

type RegistryHookPostDestroy

type RegistryHookPostDestroy interface {
	WasDestroyed(interface{}, KV, *RegistryEntry, error) error
}

type RegistryHookPostOpen

type RegistryHookPostOpen interface {
	WasOpened(interface{}, KV, *RegistryEntry, error) error
}

type RegistryHookPostSee

type RegistryHookPostSee interface {
	WasSeen(interface{}, KV, *RegistryEntry, error) error
}

type RegistryHookPreClose

type RegistryHookPreClose interface {
	Closed(interface{}, KV, *RegistryEntry) error
}

type RegistryHookPreCreate

type RegistryHookPreCreate interface {
	Created(interface{}, KV, *RegistryEntry) error
}

type RegistryHookPreDestroy

type RegistryHookPreDestroy interface {
	Destroyed(interface{}, KV, *RegistryEntry) error
}

type RegistryHookPreOpen

type RegistryHookPreOpen interface {
	Opened(interface{}, KV, *RegistryEntry) error
}

type RegistryHookPreSee

type RegistryHookPreSee interface {
	Seen(interface{}, KV, *RegistryEntry) error
}

type RegistryHooks

type RegistryHooks map[reflect.Type]RegistryHook

RegistryHooks represents a set of registry hook values to use for registries, corresponding to different types.

type SimpleRegistry

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

SimpleRegistry asserts that objects are created, then opened, then possibly seen, then closed, then destroyed, and that they are not opened more than once at a time, or destroyed while open. As a convenience feature for users whose use cases might rely on this, it will actually accept a new item being opened without being previously created; to prevent this, use a PreOpen hook that checks for a nil *RegistryEntry. The default Live check will report only objects with an open count other than 0, but if you provide a Live hook, you can return errors for objects still existing.

func NewSimpleRegistry

func NewSimpleRegistry(hooks RegistryHook) *SimpleRegistry

func (*SimpleRegistry) Closed

func (s *SimpleRegistry) Closed(o interface{}, kv KV) (err error)

func (*SimpleRegistry) Created

func (s *SimpleRegistry) Created(o interface{}, kv KV) (err error)

func (*SimpleRegistry) Destroyed

func (s *SimpleRegistry) Destroyed(o interface{}, kv KV) (err error)

func (*SimpleRegistry) Live

func (s *SimpleRegistry) Live() (results map[interface{}]*RegistryEntry, err error)

func (*SimpleRegistry) Opened

func (s *SimpleRegistry) Opened(o interface{}, kv KV) (err error)

func (*SimpleRegistry) Seen

func (s *SimpleRegistry) Seen(o interface{}, kv KV) (err error)

type VerifyCloseAuditor

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

VerifyCloseAuditor provides registries which it will check for things being closed.

func NewVerifyCloseAuditor

func NewVerifyCloseAuditor(hooks RegistryHooks) *VerifyCloseAuditor

func (*VerifyCloseAuditor) Check

func (*VerifyCloseAuditor) Check() (error, []error)

func (*VerifyCloseAuditor) FinalCheck

func (v *VerifyCloseAuditor) FinalCheck() (error, []error)

func (*VerifyCloseAuditor) Registry

func (v *VerifyCloseAuditor) Registry(o interface{}) (Registry, error)

Jump to

Keyboard shortcuts

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