correcting hooks calling
This commit is contained in:
parent
19b04fe070
commit
fc11b1dcc5
@ -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)
|
||||
}
|
||||
|
@ -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")
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user