Skip to content

Commit

Permalink
Simplify testing middleware (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
thenorthnate authored Jun 2, 2024
1 parent 54201dc commit 262ff73
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
9 changes: 9 additions & 0 deletions buzz.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
11 changes: 11 additions & 0 deletions external_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package buzz_test
import (
"context"
"log"
"testing"

"github.com/thenorthnate/buzz"
)
Expand Down Expand Up @@ -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)
}
}

0 comments on commit 262ff73

Please sign in to comment.