diff --git a/server/noop.go b/server/noop.go index d742706c..30a1d07b 100644 --- a/server/noop.go +++ b/server/noop.go @@ -723,7 +723,7 @@ func (n *noopServer) createSubHandler(sb *subscriber, opts Options) broker.Handl return nil } - opts.Hooks.EachNext(func(hook options.Hook) { + opts.Hooks.EachPrev(func(hook options.Hook) { if h, ok := hook.(HookSubHandler); ok { fn = h(fn) } diff --git a/server/noop_test.go b/server/noop_test.go index 11476501..f6ecbc8d 100644 --- a/server/noop_test.go +++ b/server/noop_test.go @@ -3,6 +3,7 @@ package server_test import ( "context" "fmt" + "go.unistack.org/micro/v3/options" "testing" "go.unistack.org/micro/v3/broker" @@ -84,3 +85,38 @@ func TestNoopSub(t *testing.T) { } }() } + +func TestHooks_Wrap(t *testing.T) { + n := 5 + fn1 := func(next server.FuncSubHandler) server.FuncSubHandler { + return func(ctx context.Context, msg server.Message) (err error) { + n *= 2 + return next(ctx, msg) + } + } + fn2 := func(next server.FuncSubHandler) server.FuncSubHandler { + return func(ctx context.Context, msg server.Message) (err error) { + n -= 10 + return next(ctx, msg) + } + } + + hs := &options.Hooks{} + hs.Append(server.HookSubHandler(fn1), server.HookSubHandler(fn2)) + + var fn = func(ctx context.Context, msg server.Message) error { + return nil + } + + hs.EachPrev(func(hook options.Hook) { + if h, ok := hook.(server.HookSubHandler); ok { + fn = h(fn) + } + }) + + fn(nil, nil) + + if n != 0 { + t.Fatalf("uncorrected hooks call") + } +}