You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by fl...@apache.org on 2020/05/01 14:22:56 UTC

[dubbo-go] branch feature/dubbo-2.7.5 updated: Implement event dispatcher

This is an automated email from the ASF dual-hosted git repository.

flycash pushed a commit to branch feature/dubbo-2.7.5
in repository https://gitbox.apache.org/repos/asf/dubbo-go.git


The following commit(s) were added to refs/heads/feature/dubbo-2.7.5 by this push:
     new 530f563  Implement event dispatcher
     new 9b41e95  Merge pull request #497 from flycash/2.7.5
530f563 is described below

commit 530f563498bcbb9b568cf86393c7bf85df2eb775
Author: flycash <fl...@apache.org>
AuthorDate: Tue Apr 28 22:23:47 2020 +0800

    Implement event dispatcher
---
 .../observer/dispatcher/mock_event_dispatcher.go   | 58 ++++++++++++++++++++++
 go.mod                                             |  2 +-
 go.sum                                             |  2 +
 registry/nacos/service_discovery.go                |  2 +-
 registry/nacos/service_discovery_test.go           | 17 +++++--
 5 files changed, 75 insertions(+), 6 deletions(-)

diff --git a/common/observer/dispatcher/mock_event_dispatcher.go b/common/observer/dispatcher/mock_event_dispatcher.go
new file mode 100644
index 0000000..45cdaa7
--- /dev/null
+++ b/common/observer/dispatcher/mock_event_dispatcher.go
@@ -0,0 +1,58 @@
+/*
+ * 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 dispatcher
+
+import (
+	"github.com/apache/dubbo-go/common/observer"
+)
+
+// MockEventDispatcher will do nothing.
+// It is only used by tests
+// Now the implementation doing nothing,
+// But you can modify this if needed
+type MockEventDispatcher struct {
+}
+
+// AddEventListener do nothing
+func (m MockEventDispatcher) AddEventListener(listener observer.EventListener) {
+}
+
+// AddEventListeners do nothing
+func (m MockEventDispatcher) AddEventListeners(listenersSlice []observer.EventListener) {
+}
+
+// RemoveEventListener do nothing
+func (m MockEventDispatcher) RemoveEventListener(listener observer.EventListener) {
+}
+
+// RemoveEventListeners do nothing
+func (m MockEventDispatcher) RemoveEventListeners(listenersSlice []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) {
+}
diff --git a/go.mod b/go.mod
index 40e4aea..1c3c52d 100644
--- a/go.mod
+++ b/go.mod
@@ -29,7 +29,7 @@ require (
 	github.com/magiconair/properties v1.8.1
 	github.com/mitchellh/mapstructure v1.1.2
 	github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd
-	github.com/nacos-group/nacos-sdk-go v0.3.0
+	github.com/nacos-group/nacos-sdk-go v0.3.1
 	github.com/opentracing/opentracing-go v1.1.0
 	github.com/pkg/errors v0.9.1
 	github.com/prometheus/client_golang v1.1.0
diff --git a/go.sum b/go.sum
index ff5a38a..22370ce 100644
--- a/go.sum
+++ b/go.sum
@@ -388,6 +388,8 @@ github.com/nacos-group/nacos-sdk-go v0.0.0-20191128082542-fe1b325b125c h1:WoCa3A
 github.com/nacos-group/nacos-sdk-go v0.0.0-20191128082542-fe1b325b125c/go.mod h1:CEkSvEpoveoYjA81m4HNeYQ0sge0LFGKSEqO3JKHllo=
 github.com/nacos-group/nacos-sdk-go v0.3.0 h1:2v2QmihtyX6ZUXAN+ya+5h2pedn7R5M+WJwSJPFsuMY=
 github.com/nacos-group/nacos-sdk-go v0.3.0/go.mod h1:ESKb6yF0gxSc8GuS+0jaMBe+n8rJ5/k4ya6LyFG2xi8=
+github.com/nacos-group/nacos-sdk-go v0.3.1 h1:MI7bNDAN5m9UFcRRUTSPfJi4dCQo+TYG85qVB1rCHeg=
+github.com/nacos-group/nacos-sdk-go v0.3.1/go.mod h1:ESKb6yF0gxSc8GuS+0jaMBe+n8rJ5/k4ya6LyFG2xi8=
 github.com/nicolai86/scaleway-sdk v1.10.2-0.20180628010248-798f60e20bb2 h1:BQ1HW7hr4IVovMwWg0E0PYcyW8CzqDcVmaew9cujU4s=
 github.com/nicolai86/scaleway-sdk v1.10.2-0.20180628010248-798f60e20bb2/go.mod h1:TLb2Sg7HQcgGdloNxkrmtgDNR9uVYF3lfdFIN4Ro6Sk=
 github.com/oklog/run v0.0.0-20180308005104-6934b124db28 h1:Hbr3fbVPXea52oPQeP7KLSxP52g6SFaNY1IqAmUyEW0=
diff --git a/registry/nacos/service_discovery.go b/registry/nacos/service_discovery.go
index 7d3406c..fbd84ac 100644
--- a/registry/nacos/service_discovery.go
+++ b/registry/nacos/service_discovery.go
@@ -237,7 +237,7 @@ func (n *nacosServiceDiscovery) DispatchEventForInstances(serviceName string, in
 
 // DispatchEvent will dispatch the event
 func (n *nacosServiceDiscovery) DispatchEvent(event *registry.ServiceInstancesChangedEvent) error {
-	// TODO(waiting for event dispatcher, another task)
+	extension.GetGlobalDispatcher().Dispatch(event)
 	return nil
 }
 
diff --git a/registry/nacos/service_discovery_test.go b/registry/nacos/service_discovery_test.go
index a756e86..0ac46cb 100644
--- a/registry/nacos/service_discovery_test.go
+++ b/registry/nacos/service_discovery_test.go
@@ -30,19 +30,28 @@ import (
 	"github.com/apache/dubbo-go/common"
 	"github.com/apache/dubbo-go/common/constant"
 	"github.com/apache/dubbo-go/common/extension"
+	"github.com/apache/dubbo-go/common/observer"
+	"github.com/apache/dubbo-go/common/observer/dispatcher"
 	"github.com/apache/dubbo-go/registry"
 )
 
 func TestNacosServiceDiscovery_Destroy(t *testing.T) {
-	serviceDiscovry, err := extension.GetServiceDiscovery(constant.NACOS_KEY, mockUrl())
+	serviceDiscovery, err := extension.GetServiceDiscovery(constant.NACOS_KEY, mockUrl())
 	assert.Nil(t, err)
-	assert.NotNil(t, serviceDiscovry)
-	err = serviceDiscovry.Destroy()
+	assert.NotNil(t, serviceDiscovery)
+	err = serviceDiscovery.Destroy()
 	assert.Nil(t, err)
-	assert.Nil(t, serviceDiscovry.(*nacosServiceDiscovery).namingClient)
+	assert.Nil(t, serviceDiscovery.(*nacosServiceDiscovery).namingClient)
 }
 
 func TestNacosServiceDiscovery_CRUD(t *testing.T) {
+
+	extension.SetEventDispatcher("mock", func() observer.EventDispatcher {
+		return &dispatcher.MockEventDispatcher{}
+	})
+
+	extension.SetAndInitGlobalDispatcher("mock")
+
 	serviceName := "service-name"
 	id := "id"
 	host := "host"