From 3cd8bc33d6671ae630b5b58989db4b3aca9c1d8a Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Fri, 1 Mar 2024 21:39:31 +0300 Subject: [PATCH] fixup test --- service.go | 142 +++++++++++++++++++++++++++++------------------- service_test.go | 12 +--- 2 files changed, 88 insertions(+), 66 deletions(-) diff --git a/service.go b/service.go index 693f8d40..9ae899a3 100644 --- a/service.go +++ b/service.go @@ -7,11 +7,15 @@ import ( "go.unistack.org/micro/v3/broker" "go.unistack.org/micro/v3/client" + "go.unistack.org/micro/v3/codec" "go.unistack.org/micro/v3/config" + "go.unistack.org/micro/v3/flow" "go.unistack.org/micro/v3/logger" "go.unistack.org/micro/v3/meter" "go.unistack.org/micro/v3/register" + "go.unistack.org/micro/v3/resolver" "go.unistack.org/micro/v3/router" + "go.unistack.org/micro/v3/selector" "go.unistack.org/micro/v3/server" "go.unistack.org/micro/v3/store" "go.unistack.org/micro/v3/tracer" @@ -72,8 +76,8 @@ func RegisterSubscriber(topic string, s server.Server, h interface{}, opts ...se } type service struct { - sync.RWMutex opts Options + sync.RWMutex } // NewService creates and returns a new Service based on the packages within. @@ -377,69 +381,95 @@ func (s *service) Run() error { } func getNameIndex(n string, ifaces interface{}) int { - switch values := ifaces.(type) { - case []router.Router: - for idx, iface := range values { - if iface.Name() == n { - return idx - } - } - case []register.Register: - for idx, iface := range values { - if iface.Name() == n { - return idx - } - } - case []store.Store: - for idx, iface := range values { - if iface.Name() == n { - return idx - } - } - case []tracer.Tracer: - for idx, iface := range values { - if iface.Name() == n { - return idx - } - } - case []server.Server: - for idx, iface := range values { - if iface.Name() == n { - return idx - } - } - case []config.Config: - for idx, iface := range values { - if iface.Name() == n { - return idx - } - } - case []meter.Meter: - for idx, iface := range values { - if iface.Name() == n { - return idx - } - } + type namer interface { + Name() string + } + + switch vt := ifaces.(type) { case []broker.Broker: - for idx, iface := range values { - if iface.Name() == n { + for idx, iface := range vt { + if nm, ok := iface.(namer); ok && nm.Name() == n { return idx } } case []client.Client: - for idx, iface := range values { - if iface.Name() == n { + for idx, iface := range vt { + if nm, ok := iface.(namer); ok && nm.Name() == n { + return idx + } + } + case []codec.Codec: + for idx, iface := range vt { + if nm, ok := iface.(namer); ok && nm.Name() == n { + return idx + } + } + case []config.Config: + for idx, iface := range vt { + if nm, ok := iface.(namer); ok && nm.Name() == n { + return idx + } + } + case []flow.Flow: + for idx, iface := range vt { + if nm, ok := iface.(namer); ok && nm.Name() == n { + return idx + } + } + case []logger.Logger: + for idx, iface := range vt { + if nm, ok := iface.(namer); ok && nm.Name() == n { + return idx + } + } + case []meter.Meter: + for idx, iface := range vt { + if nm, ok := iface.(namer); ok && nm.Name() == n { + return idx + } + } + case []register.Register: + for idx, iface := range vt { + if nm, ok := iface.(namer); ok && nm.Name() == n { + return idx + } + } + case []resolver.Resolver: + for idx, iface := range vt { + if nm, ok := iface.(namer); ok && nm.Name() == n { + return idx + } + } + case []router.Router: + for idx, iface := range vt { + if nm, ok := iface.(namer); ok && nm.Name() == n { + return idx + } + } + case []selector.Selector: + for idx, iface := range vt { + if nm, ok := iface.(namer); ok && nm.Name() == n { + return idx + } + } + case []server.Server: + for idx, iface := range vt { + if nm, ok := iface.(namer); ok && nm.Name() == n { + return idx + } + } + case []store.Store: + for idx, iface := range vt { + if nm, ok := iface.(namer); ok && nm.Name() == n { + return idx + } + } + case []tracer.Tracer: + for idx, iface := range vt { + if nm, ok := iface.(namer); ok && nm.Name() == n { return idx } } - /* - case []logger.Logger: - for idx, iface := range values { - if iface.Name() == n { - return idx - } - } - */ } return 0 diff --git a/service_test.go b/service_test.go index 1a23de60..97fd612b 100644 --- a/service_test.go +++ b/service_test.go @@ -22,13 +22,14 @@ func TestClient(t *testing.T) { c2 := client.NewClient(client.Name("test2")) svc := NewService(Client(c1, c2)) + if err := svc.Init(); err != nil { t.Fatal(err) } x1 := svc.Client("test2") if x1.Name() != "test2" { - t.Fatal("invalid client") + t.Fatalf("invalid client %#+v", svc.Options().Clients) } } @@ -40,15 +41,6 @@ func (ti *testItem) Name() string { return ti.name } -func TestGetNameIndex(t *testing.T) { - item1 := &testItem{name: "first"} - item2 := &testItem{name: "second"} - items := []interface{}{item1, item2} - if idx := getNameIndex("second", items); idx != 1 { - t.Fatalf("getNameIndex func error, item not found") - } -} - func TestRegisterHandler(t *testing.T) { type args struct { s server.Server