Skip to content

Commit

Permalink
add metadata service test case
Browse files Browse the repository at this point in the history
  • Loading branch information
FoghostCn committed Apr 20, 2024
1 parent 28e44c4 commit 50e8b70
Show file tree
Hide file tree
Showing 5 changed files with 383 additions and 17 deletions.
17 changes: 16 additions & 1 deletion metadata/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func TestGetMetadataFromRpc(t *testing.T) {
mockInvoker := new(mockInvoker)
defer mockInvoker.AssertExpectations(t)
mockProtocol := new(mockProtocol)
defer mockProtocol.AssertExpectations(t)
extension.SetProtocol("dubbo", func() protocol.Protocol {
return mockProtocol
})
Expand Down Expand Up @@ -199,7 +200,8 @@ type mockProtocol struct {
}

func (m mockProtocol) Export(invoker protocol.Invoker) protocol.Exporter {

Check failure on line 202 in metadata/client_test.go

View workflow job for this annotation

GitHub Actions / lint (1.20)

copylocks: Export passes lock by value: dubbo.apache.org/dubbo-go/v3/metadata.mockProtocol contains github.com/stretchr/testify/mock.Mock contains sync.Mutex (govet)

Check failure on line 202 in metadata/client_test.go

View workflow job for this annotation

GitHub Actions / lint (1.20)

copylocks: Export passes lock by value: dubbo.apache.org/dubbo-go/v3/metadata.mockProtocol contains github.com/stretchr/testify/mock.Mock contains sync.Mutex (govet)
return nil
args := m.Called()
return args.Get(0).(protocol.Exporter)
}

func (m mockProtocol) Refer(url *common.URL) protocol.Invoker {

Check failure on line 207 in metadata/client_test.go

View workflow job for this annotation

GitHub Actions / lint (1.20)

copylocks: Refer passes lock by value: dubbo.apache.org/dubbo-go/v3/metadata.mockProtocol contains github.com/stretchr/testify/mock.Mock contains sync.Mutex (govet)

Check failure on line 207 in metadata/client_test.go

View workflow job for this annotation

GitHub Actions / lint (1.20)

copylocks: Refer passes lock by value: dubbo.apache.org/dubbo-go/v3/metadata.mockProtocol contains github.com/stretchr/testify/mock.Mock contains sync.Mutex (govet)
Expand Down Expand Up @@ -239,3 +241,16 @@ func (m mockInvoker) Invoke(ctx context.Context, inv protocol.Invocation) protoc
reply.Services = meta.Services
return args.Get(0).(protocol.Result)
}

type mockExporter struct {
mock.Mock
}

func (m mockExporter) GetInvoker() protocol.Invoker {

Check failure on line 249 in metadata/client_test.go

View workflow job for this annotation

GitHub Actions / lint (1.20)

copylocks: GetInvoker passes lock by value: dubbo.apache.org/dubbo-go/v3/metadata.mockExporter contains github.com/stretchr/testify/mock.Mock contains sync.Mutex (govet)

Check failure on line 249 in metadata/client_test.go

View workflow job for this annotation

GitHub Actions / lint (1.20)

copylocks: GetInvoker passes lock by value: dubbo.apache.org/dubbo-go/v3/metadata.mockExporter contains github.com/stretchr/testify/mock.Mock contains sync.Mutex (govet)
args := m.Called()
return args.Get(0).(protocol.Invoker)
}

func (m mockExporter) UnExport() {

Check failure on line 254 in metadata/client_test.go

View workflow job for this annotation

GitHub Actions / lint (1.20)

copylocks: UnExport passes lock by value: dubbo.apache.org/dubbo-go/v3/metadata.mockExporter contains github.com/stretchr/testify/mock.Mock contains sync.Mutex (govet)

Check failure on line 254 in metadata/client_test.go

View workflow job for this annotation

GitHub Actions / lint (1.20)

copylocks: UnExport passes lock by value: dubbo.apache.org/dubbo-go/v3/metadata.mockExporter contains github.com/stretchr/testify/mock.Mock contains sync.Mutex (govet)
m.Called()
}
2 changes: 1 addition & 1 deletion metadata/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import (
)

var (
metadataService MetadataService = &DefaultMetadataService{}
registryMetadataInfo = make(map[string]*info.MetadataInfo)
metadataService MetadataService = &DefaultMetadataService{metadataMap: registryMetadataInfo}
)

func GetMetadataService() MetadataService {
Expand Down
26 changes: 11 additions & 15 deletions metadata/metadata_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ import (

// version will be used by Version func
const (
version = "1.0.0"
allServiceInterfaces = "*"
version = "1.0.0"
allMatch = "*"
)

// MetadataService is used to define meta data related behaviors
Expand All @@ -58,12 +58,11 @@ type MetadataService interface {
GetMetadataInfo(revision string) (*info.MetadataInfo, error)
// GetMetadataServiceURL will return the url of metadata service
GetMetadataServiceURL() (*common.URL, error)
// SetMetadataServiceURL exporter to set url of metadata service, will not be exported by exporter,cause no error return
SetMetadataServiceURL(*common.URL)
}

// DefaultMetadataService is store and query the metadata info in memory when each service registry
type DefaultMetadataService struct {
metadataMap map[string]*info.MetadataInfo
metadataUrl *common.URL
}

Expand All @@ -73,19 +72,16 @@ func (mts *DefaultMetadataService) SetMetadataServiceURL(url *common.URL) {

// GetExportedURLs get all exported urls
func (mts *DefaultMetadataService) GetExportedURLs(serviceInterface string, group string, version string, protocol string) ([]*common.URL, error) {
if allServiceInterfaces == serviceInterface {
return mts.GetExportedServiceURLs()
}
all, err := mts.GetExportedServiceURLs()
if err != nil {
return nil, err
}
urls := make([]*common.URL, 0)
for _, url := range all {
if url.GetParam(constant.InterfaceKey, "") == serviceInterface &&
url.GetParam(constant.GroupKey, "") == group &&
url.GetParam(constant.ProtocolKey, "") == protocol &&
url.GetParam(constant.VersionKey, "") == version {
if (url.Interface() == serviceInterface || serviceInterface == allMatch) &&
(url.Group() == group || group == allMatch) &&
(url.Protocol == protocol || protocol == allMatch) &&
(url.Version() == version || version == allMatch) {
urls = append(urls, url)
}
}
Expand All @@ -97,7 +93,7 @@ func (mts *DefaultMetadataService) GetMetadataInfo(revision string) (*info.Metad
if revision == "" {
return nil, nil
}
for _, metadataInfo := range registryMetadataInfo {
for _, metadataInfo := range mts.metadataMap {
if metadataInfo.Revision == revision {
return metadataInfo, nil
}
Expand All @@ -109,7 +105,7 @@ func (mts *DefaultMetadataService) GetMetadataInfo(revision string) (*info.Metad
// GetExportedServiceURLs get exported service urls
func (mts *DefaultMetadataService) GetExportedServiceURLs() ([]*common.URL, error) {
urls := make([]*common.URL, 0)
for _, metadataInfo := range registryMetadataInfo {
for _, metadataInfo := range mts.metadataMap {
urls = append(urls, metadataInfo.GetExportedServiceURLs()...)
}
return urls, nil
Expand All @@ -127,7 +123,7 @@ func (mts *DefaultMetadataService) GetMetadataServiceURL() (*common.URL, error)

func (mts *DefaultMetadataService) GetSubscribedURLs() ([]*common.URL, error) {
urls := make([]*common.URL, 0)
for _, metadataInfo := range registryMetadataInfo {
for _, metadataInfo := range mts.metadataMap {
urls = append(urls, metadataInfo.GetSubscribedURLs()...)
}
return urls, nil
Expand Down Expand Up @@ -181,7 +177,7 @@ func (e *serviceExporter) Export() error {
proxyFactory := extension.GetProxyFactory("")
invoker := proxyFactory.GetInvoker(ivkURL)
e.protocolExporter = extension.GetProtocol(ivkURL.Protocol).Export(invoker)
e.service.SetMetadataServiceURL(ivkURL)
e.service.(*DefaultMetadataService).SetMetadataServiceURL(ivkURL)
logger.Infof("[Metadata Service] The MetadataService exports urls : %v ", ivkURL)
return nil
}
Expand Down
Loading

0 comments on commit 50e8b70

Please sign in to comment.