diff --git a/buzz.go b/buzz.go index 9d11d15..d30d4a7 100644 --- a/buzz.go +++ b/buzz.go @@ -26,3 +26,12 @@ func (chain *CallChain) Next(ctx context.Context) error { // MiddleFunc defines the type of any middleware that can be used in the hive. type MiddleFunc func(ctx context.Context, chain *CallChain) error + +// NewTestCallChain creates a new [CallChain] that simply executes the given [MiddleFunc]. +// The provided [MiddleFunc] will recieve a nil [CallChain]. This function is a utility to +// make it easy to test your own middleware. +func NewTestCallChain(exec MiddleFunc) *CallChain { + return &CallChain{ + exec: exec, + } +} diff --git a/external_test.go b/external_test.go index 6c99564..e61fd4c 100644 --- a/external_test.go +++ b/external_test.go @@ -3,6 +3,7 @@ package buzz_test import ( "context" "log" + "testing" "github.com/thenorthnate/buzz" ) @@ -31,3 +32,13 @@ func Example() { // Some time later... during shutdown hive.StopAll() } + +func TestNewTestCallChain(t *testing.T) { + middleware := func(ctx context.Context, chain *buzz.CallChain) error { + return nil + } + chain := buzz.NewTestCallChain(middleware) + if err := chain.Next(context.Background()); err != nil { + t.Fatal("got unexpected error ", err) + } +}