register/noop: add noop register #306
							
								
								
									
										142
									
								
								service.go
									
									
									
									
									
								
							
							
						
						
									
										142
									
								
								service.go
									
									
									
									
									
								
							| @@ -7,11 +7,15 @@ import ( | |||||||
|  |  | ||||||
| 	"go.unistack.org/micro/v3/broker" | 	"go.unistack.org/micro/v3/broker" | ||||||
| 	"go.unistack.org/micro/v3/client" | 	"go.unistack.org/micro/v3/client" | ||||||
|  | 	"go.unistack.org/micro/v3/codec" | ||||||
| 	"go.unistack.org/micro/v3/config" | 	"go.unistack.org/micro/v3/config" | ||||||
|  | 	"go.unistack.org/micro/v3/flow" | ||||||
| 	"go.unistack.org/micro/v3/logger" | 	"go.unistack.org/micro/v3/logger" | ||||||
| 	"go.unistack.org/micro/v3/meter" | 	"go.unistack.org/micro/v3/meter" | ||||||
| 	"go.unistack.org/micro/v3/register" | 	"go.unistack.org/micro/v3/register" | ||||||
|  | 	"go.unistack.org/micro/v3/resolver" | ||||||
| 	"go.unistack.org/micro/v3/router" | 	"go.unistack.org/micro/v3/router" | ||||||
|  | 	"go.unistack.org/micro/v3/selector" | ||||||
| 	"go.unistack.org/micro/v3/server" | 	"go.unistack.org/micro/v3/server" | ||||||
| 	"go.unistack.org/micro/v3/store" | 	"go.unistack.org/micro/v3/store" | ||||||
| 	"go.unistack.org/micro/v3/tracer" | 	"go.unistack.org/micro/v3/tracer" | ||||||
| @@ -72,8 +76,8 @@ func RegisterSubscriber(topic string, s server.Server, h interface{}, opts ...se | |||||||
| } | } | ||||||
|  |  | ||||||
| type service struct { | type service struct { | ||||||
| 	sync.RWMutex |  | ||||||
| 	opts Options | 	opts Options | ||||||
|  | 	sync.RWMutex | ||||||
| } | } | ||||||
|  |  | ||||||
| // NewService creates and returns a new Service based on the packages within. | // 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 { | func getNameIndex(n string, ifaces interface{}) int { | ||||||
| 	switch values := ifaces.(type) { | 	type namer interface { | ||||||
| 	case []router.Router: | 		Name() string | ||||||
| 		for idx, iface := range values { | 	} | ||||||
| 			if iface.Name() == n { |  | ||||||
| 				return idx | 	switch vt := ifaces.(type) { | ||||||
| 			} |  | ||||||
| 		} |  | ||||||
| 	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 |  | ||||||
| 			} |  | ||||||
| 		} |  | ||||||
| 	case []broker.Broker: | 	case []broker.Broker: | ||||||
| 		for idx, iface := range values { | 		for idx, iface := range vt { | ||||||
| 			if iface.Name() == n { | 			if nm, ok := iface.(namer); ok && nm.Name() == n { | ||||||
| 				return idx | 				return idx | ||||||
| 			} | 			} | ||||||
| 		} | 		} | ||||||
| 	case []client.Client: | 	case []client.Client: | ||||||
| 		for idx, iface := range values { | 		for idx, iface := range vt { | ||||||
| 			if iface.Name() == n { | 			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 | 				return idx | ||||||
| 			} | 			} | ||||||
| 		} | 		} | ||||||
| 		/* |  | ||||||
| 			case []logger.Logger: |  | ||||||
| 					for idx, iface := range values { |  | ||||||
| 						if iface.Name() == n { |  | ||||||
| 							return idx |  | ||||||
| 						} |  | ||||||
| 					} |  | ||||||
| 		*/ |  | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	return 0 | 	return 0 | ||||||
|   | |||||||
| @@ -22,13 +22,14 @@ func TestClient(t *testing.T) { | |||||||
| 	c2 := client.NewClient(client.Name("test2")) | 	c2 := client.NewClient(client.Name("test2")) | ||||||
|  |  | ||||||
| 	svc := NewService(Client(c1, c2)) | 	svc := NewService(Client(c1, c2)) | ||||||
|  |  | ||||||
| 	if err := svc.Init(); err != nil { | 	if err := svc.Init(); err != nil { | ||||||
| 		t.Fatal(err) | 		t.Fatal(err) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	x1 := svc.Client("test2") | 	x1 := svc.Client("test2") | ||||||
| 	if x1.Name() != "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 | 	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) { | func TestRegisterHandler(t *testing.T) { | ||||||
| 	type args struct { | 	type args struct { | ||||||
| 		s    server.Server | 		s    server.Server | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user