correcting hooks calling
Some checks failed
lint / lint (pull_request) Failing after 2m7s
test / test (pull_request) Successful in 4m29s

This commit is contained in:
Денис Евстигнеев 2024-12-17 15:35:04 +03:00
parent 19b04fe070
commit fc11b1dcc5
2 changed files with 37 additions and 1 deletions

View File

@ -723,7 +723,7 @@ func (n *noopServer) createSubHandler(sb *subscriber, opts Options) broker.Handl
return nil return nil
} }
opts.Hooks.EachNext(func(hook options.Hook) { opts.Hooks.EachPrev(func(hook options.Hook) {
if h, ok := hook.(HookSubHandler); ok { if h, ok := hook.(HookSubHandler); ok {
fn = h(fn) fn = h(fn)
} }

View File

@ -3,6 +3,7 @@ package server_test
import ( import (
"context" "context"
"fmt" "fmt"
"go.unistack.org/micro/v3/options"
"testing" "testing"
"go.unistack.org/micro/v3/broker" "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")
}
}