From 77558d7ec79d0e2854bdd8cf291ce6718eca6ab7 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Thu, 30 Jan 2025 12:40:44 +0300 Subject: [PATCH] fixup Signed-off-by: Vasiliy Tolstov --- broker/memory/memory.go | 11 +++++++++-- metadata/metadata.go | 21 ++++++++++++--------- metadata/metadata_test.go | 27 ++++++++++++++++----------- register/memory/memory_test.go | 2 +- 4 files changed, 38 insertions(+), 23 deletions(-) diff --git a/broker/memory/memory.go b/broker/memory/memory.go index 86430a4c..c95c1b0c 100644 --- a/broker/memory/memory.go +++ b/broker/memory/memory.go @@ -210,8 +210,15 @@ func (b *Broker) publish(ctx context.Context, topic string, messages ...broker.M } case func(broker.Message) error: for _, message := range messages { - if err = s(message); err == nil && sub.opts.AutoAck { - err = message.Ack() + msg, ok := message.(*memoryMessage) + if !ok { + if b.opts.Logger.V(logger.ErrorLevel) { + b.opts.Logger.Error(ctx, "broker handler error", broker.ErrInvalidMessage) + } + } + msg.topic = topic + if err = s(msg); err == nil && sub.opts.AutoAck { + err = msg.Ack() } if err != nil { if b.opts.Logger.V(logger.ErrorLevel) { diff --git a/metadata/metadata.go b/metadata/metadata.go index 7664d0ea..abc8fd81 100644 --- a/metadata/metadata.go +++ b/metadata/metadata.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "net/textproto" + "sort" "strings" ) @@ -464,27 +465,29 @@ type Iterator struct { cnt int } -/* // Next advance iterator to next element -func (iter *Iterator) Next(k, v *string) bool { +func (iter *Iterator) Next(k *string, v *[]string) bool { if iter.cur+1 > iter.cnt { return false } - *k = iter.keys[iter.cur] - *v = iter.Metadata[*k] - iter.cur++ + if k != nil && v != nil { + *k = iter.keys[iter.cur] + vv := iter.md[*k] + *v = make([]string, len(vv)) + copy(*v, vv) + iter.cur++ + } return true } // Iterator returns the itarator for metadata in sorted order -func (Metadata Metadata) Iterator() *Iterator { - iter := &Iterator{Metadata: Metadata, cnt: len(Metadata)} +func (md Metadata) Iterator() *Iterator { + iter := &Iterator{md: md, cnt: len(md)} iter.keys = make([]string, 0, iter.cnt) - for k := range Metadata { + for k := range md { iter.keys = append(iter.keys, k) } sort.Strings(iter.keys) return iter } -*/ diff --git a/metadata/metadata_test.go b/metadata/metadata_test.go index 0891021f..4b1a47c7 100644 --- a/metadata/metadata_test.go +++ b/metadata/metadata_test.go @@ -102,22 +102,27 @@ func TestPassing(t *testing.T) { } } -/* -func TestIterator(_ *testing.T) { - md := Metadata{ - "1Last": "last", - "2First": "first", - "3Second": "second", - } +func TestIterator(t *testing.T) { + md := Pairs( + "1Last", "last", + "2First", "first", + "3Second", "second", + ) iter := md.Iterator() - var k, v string - + var k string + var v []string + chk := New(3) for iter.Next(&k, &v) { - // fmt.Printf("k: %s, v: %s\n", k, v) + chk[k] = v + } + + for k, v := range chk { + if cv, ok := md[k]; !ok || len(cv) != len(v) || cv[0] != v[0] { + t.Fatalf("XXXX %#+v %#+v", chk, md) + } } } -*/ func TestMedataCanonicalKey(t *testing.T) { md := New(1) diff --git a/register/memory/memory_test.go b/register/memory/memory_test.go index 71f4cd77..348a6c90 100644 --- a/register/memory/memory_test.go +++ b/register/memory/memory_test.go @@ -7,7 +7,7 @@ import ( "testing" "time" - "go.unistack.org/micro/v3/register" + "go.unistack.org/micro/v4/register" ) var testData = map[string][]*register.Service{