From 58eec62419006a97b324ee675905530c830f45c1 Mon Sep 17 00:00:00 2001 From: Evstigneev Denis Date: Tue, 17 Dec 2024 15:35:04 +0300 Subject: [PATCH] correcting hooks calling --- broker/memory/memory.go | 2 +- broker/noop.go | 2 +- client/noop.go | 2 +- config/default.go | 2 +- server/noop.go | 2 +- server/noop_test.go | 38 ++++++++++++++++++++++++++++++++++++++ store/memory/memory.go | 2 +- store/noop.go | 2 +- 8 files changed, 45 insertions(+), 7 deletions(-) diff --git a/broker/memory/memory.go b/broker/memory/memory.go index 56c08eaa..a6abb960 100644 --- a/broker/memory/memory.go +++ b/broker/memory/memory.go @@ -109,7 +109,7 @@ func (m *memoryBroker) Init(opts ...broker.Option) error { m.funcSubscribe = m.fnSubscribe m.funcBatchSubscribe = m.fnBatchSubscribe - m.opts.Hooks.EachNext(func(hook options.Hook) { + m.opts.Hooks.EachPrev(func(hook options.Hook) { switch h := hook.(type) { case broker.HookPublish: m.funcPublish = h(m.funcPublish) diff --git a/broker/noop.go b/broker/noop.go index de98421e..c64e6b99 100644 --- a/broker/noop.go +++ b/broker/noop.go @@ -59,7 +59,7 @@ func (b *NoopBroker) Init(opts ...Option) error { b.funcSubscribe = b.fnSubscribe b.funcBatchSubscribe = b.fnBatchSubscribe - b.opts.Hooks.EachNext(func(hook options.Hook) { + b.opts.Hooks.EachPrev(func(hook options.Hook) { switch h := hook.(type) { case HookPublish: b.funcPublish = h(b.funcPublish) diff --git a/client/noop.go b/client/noop.go index db960e62..f8f56265 100644 --- a/client/noop.go +++ b/client/noop.go @@ -194,7 +194,7 @@ func (n *noopClient) Init(opts ...Option) error { n.funcPublish = n.fnPublish n.funcBatchPublish = n.fnBatchPublish - n.opts.Hooks.EachNext(func(hook options.Hook) { + n.opts.Hooks.EachPrev(func(hook options.Hook) { switch h := hook.(type) { case HookCall: n.funcCall = h(n.funcCall) diff --git a/config/default.go b/config/default.go index 62cef02c..9bbd0af9 100644 --- a/config/default.go +++ b/config/default.go @@ -37,7 +37,7 @@ func (c *defaultConfig) Init(opts ...Option) error { c.funcLoad = c.fnLoad c.funcSave = c.fnSave - c.opts.Hooks.EachNext(func(hook options.Hook) { + c.opts.Hooks.EachPrev(func(hook options.Hook) { switch h := hook.(type) { case HookLoad: c.funcLoad = h(c.funcLoad) 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..d6706e61 100644 --- a/server/noop_test.go +++ b/server/noop_test.go @@ -9,6 +9,7 @@ import ( "go.unistack.org/micro/v3/client" "go.unistack.org/micro/v3/codec" "go.unistack.org/micro/v3/logger" + "go.unistack.org/micro/v3/options" "go.unistack.org/micro/v3/server" ) @@ -84,3 +85,40 @@ 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) + } + }) + + if err := fn(nil, nil); err != nil { + t.Fatal(err) + } + + if n != 0 { + t.Fatalf("uncorrected hooks call") + } +} diff --git a/store/memory/memory.go b/store/memory/memory.go index c91f38d9..47c88ce0 100644 --- a/store/memory/memory.go +++ b/store/memory/memory.go @@ -123,7 +123,7 @@ func (m *memoryStore) Init(opts ...store.Option) error { m.funcList = m.fnList m.funcDelete = m.fnDelete - m.opts.Hooks.EachNext(func(hook options.Hook) { + m.opts.Hooks.EachPrev(func(hook options.Hook) { switch h := hook.(type) { case store.HookRead: m.funcRead = h(m.funcRead) diff --git a/store/noop.go b/store/noop.go index 322e8f00..a4e07baf 100644 --- a/store/noop.go +++ b/store/noop.go @@ -54,7 +54,7 @@ func (n *noopStore) Init(opts ...Option) error { n.funcList = n.fnList n.funcDelete = n.fnDelete - n.opts.Hooks.EachNext(func(hook options.Hook) { + n.opts.Hooks.EachPrev(func(hook options.Hook) { switch h := hook.(type) { case HookRead: n.funcRead = h(n.funcRead) -- 2.45.2