Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reuse MockEventDispatcher and NewMockServiceNameMapping in service discovery test #1189

Merged
merged 8 commits into from
May 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion common/extension/event_dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func SetAndInitGlobalDispatcher(name string) {

if dp, ok := dispatchers[name]; !ok || dp == nil {
panic("EventDispatcher for " + name + " is not found, make sure you have import the package, " +
"like import _ github.com/apache/dubbo-go/common/observer/dispatcher ")
"like import _ dubbo.apache.org/dubbo-go/v3/common/observer/dispatcher.")
}
globalEventDispatcher = dispatchers[name]()
}
Expand Down
5 changes: 3 additions & 2 deletions common/extension/metadata_remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func GetRemoteMetadataService() (remote.RemoteMetadataService, error) {
if creator != nil {
return creator()
}
return nil, perrors.New(fmt.Sprintf("could not find the metadata service creator for metadataType: remote, please check whether you have imported relative packages, \n" +
"remote - github.com/apache/dubbo-go/metadata/remote/impl"))
return nil, perrors.New(fmt.Sprintf("could not find the metadata service creator for metadataType: remote, " +
"please check whether you have imported relative packages, " +
"remote - dubbo.apache.org/dubbo-go/v3/metadata/remote/impl"))
}
5 changes: 3 additions & 2 deletions common/extension/metadata_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func GetLocalMetadataService(key string) (service.MetadataService, error) {
if creator, ok := localMetadataServiceInsMap[key]; ok {
return creator()
}
return nil, perrors.New(fmt.Sprintf("could not find the metadata service creator for metadataType: local, please check whether you have imported relative packages, \n" +
"local - github.com/apache/dubbo-go/metadata/service/inmemory, "))
return nil, perrors.New(fmt.Sprintf("could not find the metadata service creator for metadataType: local, " +
"please check whether you have imported relative packages, " +
"local - dubbo.apache.org/dubbo-go/v3/metadata/service/inmemory"))
}
5 changes: 3 additions & 2 deletions common/extension/metadata_service_proxy_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func GetMetadataServiceProxyFactory(name string) service.MetadataServiceProxyFac
if f, ok := metadataServiceProxyFactoryMap[name]; ok {
return f()
}
panic(fmt.Sprintf("could not find the metadata service factory creator for name: %s, please check whether you have imported relative packages, \n"+
"local - github.com/apache/dubbo-go/metadata/service/inmemory, \n", name))
panic(fmt.Sprintf("could not find the metadata service factory creator for name: %s, "+
"please check whether you have imported relative packages, "+
"local - dubbo.apache.org/dubbo-go/v3/metadata/service/inmemory", name))
}
2 changes: 1 addition & 1 deletion common/extension/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func SetRegistry(name string, v func(_ *common.URL) (registry.Registry, error))
// GetRegistry finds the registry extension with @name
func GetRegistry(name string, config *common.URL) (registry.Registry, error) {
if registrys[name] == nil {
panic("registry for " + name + " does not exist. please make sure that you have imported the package `github.com/apache/dubbo-go/registry/" + name + "`.")
panic("registry for " + name + " does not exist. please make sure that you have imported the package dubbo.apache.org/dubbo-go/v3/registry/" + name + ".")
}
return registrys[name](config)
}
25 changes: 17 additions & 8 deletions common/observer/dispatcher/mock_event_dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,33 +25,42 @@ import (
// It is only used by tests
// Now the implementation doing nothing,
// But you can modify this if needed
type MockEventDispatcher struct{}
type MockEventDispatcher struct {
Notify chan struct{}
Event observer.Event
}

func NewMockEventDispatcher() *MockEventDispatcher {
return &MockEventDispatcher{Notify: make(chan struct{}, 1)}
}

// AddEventListener do nothing
func (m MockEventDispatcher) AddEventListener(listener observer.EventListener) {
func (m *MockEventDispatcher) AddEventListener(listener observer.EventListener) {
}

// AddEventListeners do nothing
func (m MockEventDispatcher) AddEventListeners(listenersSlice []observer.EventListener) {
func (m *MockEventDispatcher) AddEventListeners(listenersSlice []observer.EventListener) {
}

// RemoveEventListener do nothing
func (m MockEventDispatcher) RemoveEventListener(listener observer.EventListener) {
func (m *MockEventDispatcher) RemoveEventListener(listener observer.EventListener) {
}

// RemoveEventListeners do nothing
func (m MockEventDispatcher) RemoveEventListeners(listenersSlice []observer.EventListener) {
func (m *MockEventDispatcher) RemoveEventListeners(listenersSlice []observer.EventListener) {
}

// GetAllEventListeners return empty list
func (m MockEventDispatcher) GetAllEventListeners() []observer.EventListener {
func (m *MockEventDispatcher) GetAllEventListeners() []observer.EventListener {
return make([]observer.EventListener, 0)
}

// RemoveAllEventListeners do nothing
func (m MockEventDispatcher) RemoveAllEventListeners() {
func (m *MockEventDispatcher) RemoveAllEventListeners() {
}

// Dispatch do nothing
func (m MockEventDispatcher) Dispatch(event observer.Event) {
func (m *MockEventDispatcher) Dispatch(event observer.Event) {
m.Event = event
m.Notify <- struct{}{}
}
2 changes: 1 addition & 1 deletion contributing.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,6 @@ import (

// block 3: the dubbo-go package
import (
"github.com/apache/dubbo-go/common"
"dubbo.apache.org/dubbo-go/v3/common"
)
```
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -820,6 +820,7 @@ github.com/zouyx/agollo/v3 v3.4.5 h1:7YCxzY9ZYaH9TuVUBvmI6Tk0mwMggikah+cfbYogcHQ
github.com/zouyx/agollo/v3 v3.4.5/go.mod h1:LJr3kDmm23QSW+F1Ol4TMHDa7HvJvscMdVxJ2IpUTVc=
go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
go.etcd.io/bbolt v1.3.3/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
go.etcd.io/bbolt v1.3.4 h1:hi1bXHMVrlQh6WwxAy+qZCV/SYIlqo+Ushwdpa4tAKg=
go.etcd.io/bbolt v1.3.4/go.mod h1:G5EMThwa9y8QZGBClrRx5EY+Yw9kAhnjy3bSjsnlVTQ=
go.etcd.io/etcd v0.0.0-20191023171146-3cf2f69b5738/go.mod h1:dnLIgRNXwCJa5e+c6mIZCrds/GIG4ncV9HhK5PX7jPg=
go.etcd.io/etcd v0.0.0-20200402134248-51bdeb39e698 h1:jWtjCJX1qxhHISBMLRztWwR+EXkI7MJAF2HjHAE/x/I=
Expand Down
36 changes: 36 additions & 0 deletions metadata/mapping/mock_service_name_mapping.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package mapping

import (
gxset "github.com/dubbogo/gost/container/set"
)

type MockServiceNameMapping struct{}

func NewMockServiceNameMapping() *MockServiceNameMapping {
return &MockServiceNameMapping{}
}

func (m *MockServiceNameMapping) Map(string, string, string, string) error {
return nil
}

func (m *MockServiceNameMapping) Get(string, string, string, string) (*gxset.HashSet, error) {
panic("implement me")
}
2 changes: 1 addition & 1 deletion protocol/dubbo3/protoc-gen-dubbo3/go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/apache/dubbo-go/protocol/dubbo3/protoc-gen-dubbo3
module dubbo.apache.org/dubbo-go/v3/protocol/dubbo3/protoc-gen-dubbo3

go 1.15

Expand Down
2 changes: 1 addition & 1 deletion protocol/dubbo3/protoc-gen-dubbo3/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
)

import (
_ "github.com/apache/dubbo-go/protocol/dubbo3/protoc-gen-dubbo3/plugin/dubbo3"
_ "dubbo.apache.org/dubbo-go/v3/protocol/dubbo3/protoc-gen-dubbo3/plugin/dubbo3"
)

func main() {
Expand Down
2 changes: 1 addition & 1 deletion protocol/invoker.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ var (
)

// Invoker the service invocation interface for the consumer
//go:generate mockgen -source invoker.go -destination mock/mock_invoker.go -self_package github.com/apache/dubbo-go/protocol/mock --package mock Invoker
//go:generate mockgen -source invoker.go -destination mock/mock_invoker.go -self_package dubbo.apache.org/dubbo-go/v3/protocol/mock --package mock Invoker
// Extension - Invoker
type Invoker interface {
common.Node
Expand Down
1 change: 0 additions & 1 deletion registry/consul/service_discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import (
"dubbo.apache.org/dubbo-go/v3/common/extension"
"dubbo.apache.org/dubbo-go/v3/common/logger"
"dubbo.apache.org/dubbo-go/v3/config"
_ "dubbo.apache.org/dubbo-go/v3/metadata/mapping/memory"
"dubbo.apache.org/dubbo-go/v3/registry"
)

Expand Down
51 changes: 11 additions & 40 deletions registry/consul/service_discovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,15 @@
package consul

import (
"dubbo.apache.org/dubbo-go/v3/registry/event"
"fmt"
gxset "github.com/dubbogo/gost/container/set"
"math/rand"
"strconv"
"testing"
"time"
)

import (
gxset "github.com/dubbogo/gost/container/set"
"github.com/stretchr/testify/assert"
)

Expand All @@ -36,8 +35,11 @@ import (
"dubbo.apache.org/dubbo-go/v3/common/constant"
"dubbo.apache.org/dubbo-go/v3/common/extension"
"dubbo.apache.org/dubbo-go/v3/common/observer"
"dubbo.apache.org/dubbo-go/v3/common/observer/dispatcher"
"dubbo.apache.org/dubbo-go/v3/config"
"dubbo.apache.org/dubbo-go/v3/metadata/mapping"
"dubbo.apache.org/dubbo-go/v3/registry"
"dubbo.apache.org/dubbo-go/v3/registry/event"
"dubbo.apache.org/dubbo-go/v3/remoting/consul"
)

Expand Down Expand Up @@ -92,9 +94,14 @@ func TestConsulServiceDiscovery_CRUD(t *testing.T) {
}()

prepareData()
eventDispatcher := MockEventDispatcher{Notify: make(chan struct{}, 1)}

eventDispatcher := dispatcher.NewMockEventDispatcher()
extension.SetEventDispatcher("mock", func() observer.EventDispatcher {
return &eventDispatcher
return eventDispatcher
})

extension.SetGlobalServiceNameMapping(func() mapping.ServiceNameMapping {
return mapping.NewMockServiceNameMapping()
})

extension.SetAndInitGlobalDispatcher("mock")
Expand Down Expand Up @@ -200,39 +207,3 @@ func prepareService() (registry.ServiceInstance, *common.URL) {
Metadata: nil,
}, registryUrl
}

type MockEventDispatcher struct {
Notify chan struct{}
Event observer.Event
}

// AddEventListener do nothing
func (m *MockEventDispatcher) AddEventListener(observer.EventListener) {
}

// AddEventListeners do nothing
func (m *MockEventDispatcher) AddEventListeners([]observer.EventListener) {
}

// RemoveEventListener do nothing
func (m *MockEventDispatcher) RemoveEventListener(observer.EventListener) {
}

// RemoveEventListeners do nothing
func (m *MockEventDispatcher) RemoveEventListeners([]observer.EventListener) {
}

// GetAllEventListeners return empty list
func (m *MockEventDispatcher) GetAllEventListeners() []observer.EventListener {
return make([]observer.EventListener, 0)
}

// RemoveAllEventListeners do nothing
func (m *MockEventDispatcher) RemoveAllEventListeners() {
}

// Dispatch do nothing
func (m *MockEventDispatcher) Dispatch(event observer.Event) {
m.Event = event
m.Notify <- struct{}{}
}
16 changes: 3 additions & 13 deletions registry/event/event_publishing_service_deiscovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
import (
"dubbo.apache.org/dubbo-go/v3/common/extension"
"dubbo.apache.org/dubbo-go/v3/common/observer"
dispatcher2 "dubbo.apache.org/dubbo-go/v3/common/observer/dispatcher"
"dubbo.apache.org/dubbo-go/v3/common/observer/dispatcher"
"dubbo.apache.org/dubbo-go/v3/config"
"dubbo.apache.org/dubbo-go/v3/metadata/mapping"
_ "dubbo.apache.org/dubbo-go/v3/metadata/service/inmemory"
Expand All @@ -45,7 +45,7 @@ func TestEventPublishingServiceDiscovery_DispatchEvent(t *testing.T) {
config.GetApplicationConfig().MetadataType = "local"

extension.SetGlobalServiceNameMapping(func() mapping.ServiceNameMapping {
return &mockServiceNameMapping{}
return mapping.NewMockServiceNameMapping()
})

dc := NewEventPublishingServiceDiscovery(&ServiceDiscoveryA{})
Expand All @@ -61,7 +61,7 @@ func TestEventPublishingServiceDiscovery_DispatchEvent(t *testing.T) {
extension.AddEventListener(func() observer.EventListener {
return tsi
})
extension.SetEventDispatcher("direct", dispatcher2.NewDirectEventDispatcher)
extension.SetEventDispatcher("direct", dispatcher.NewDirectEventDispatcher)
extension.SetAndInitGlobalDispatcher("direct")
err := dc.Destroy()
assert.Nil(t, err)
Expand Down Expand Up @@ -174,13 +174,3 @@ func (msd *ServiceDiscoveryA) DispatchEventForInstances(serviceName string, inst
func (msd *ServiceDiscoveryA) DispatchEvent(event *registry.ServiceInstancesChangedEvent) error {
return nil
}

type mockServiceNameMapping struct{}

func (m *mockServiceNameMapping) Map(serviceInterface string, group string, version string, protocol string) error {
return nil
}

func (m *mockServiceNameMapping) Get(serviceInterface string, group string, version string, protocol string) (*gxset.HashSet, error) {
return gxset.NewSet("dubbo"), nil
}
14 changes: 2 additions & 12 deletions registry/nacos/service_discovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@ func TestNacosServiceDiscovery_CRUD(t *testing.T) {
}
prepareData()
extension.SetEventDispatcher("mock", func() observer.EventDispatcher {
return &dispatcher.MockEventDispatcher{}
return dispatcher.NewMockEventDispatcher()
})

extension.SetGlobalServiceNameMapping(func() mapping.ServiceNameMapping {
return &mockServiceNameMapping{}
return mapping.NewMockServiceNameMapping()
})

extension.SetAndInitGlobalDispatcher("mock")
Expand Down Expand Up @@ -187,13 +187,3 @@ func prepareData() {
TimeoutStr: "10s",
}
}

type mockServiceNameMapping struct{}

func (m *mockServiceNameMapping) Map(string, string, string, string) error {
return nil
}

func (m *mockServiceNameMapping) Get(string, string, string, string) (*gxset.HashSet, error) {
panic("implement me")
}
Loading