AlexStocks commented on a change in pull request #1693:
URL: https://github.com/apache/dubbo-go/pull/1693#discussion_r776571600



##########
File path: registry/etcdv3/service_discovery_test.go
##########
@@ -17,51 +17,145 @@
 
 package etcdv3
 
-//
-//var testName = "test"
-//
-//func setUp() {
-//     config.GetRootConfig().ServiceDiscoveries[testName] = 
&config.ServiceDiscoveryConfig{
-//             Protocol:  "etcdv3",
-//             RemoteRef: testName,
-//     }
-//
-//     //config.GetRootConfig().Remotes[testName] = &config.RemoteConfig{
-//     //      Address:    "localhost:2379",
-//     //      TimeoutStr: "10s",
-//     //}
-//}
-//
-//func Test_newEtcdV3ServiceDiscovery(t *testing.T) {
-//     name := constant.ETCDV3_KEY
-//     _, err := newEtcdV3ServiceDiscovery()
-//
-//     // warn: log configure file name is nil
-//     assert.NotNil(t, err)
-//
-//     sdc := &config.ServiceDiscoveryConfig{
-//             Protocol:  "etcdv3",
-//             RemoteRef: "mock",
-//     }
-//     config.GetRootConfig().ServiceDiscoveries[name] = sdc
-//
-//     _, err = newEtcdV3ServiceDiscovery()
-//
-//     // RemoteConfig not found
-//     assert.NotNil(t, err)
-//
-//     //config.GetRootConfig().Remotes["mock"] = &config.RemoteConfig{
-//     //      Address:    "localhost:2379",
-//     //      TimeoutStr: "10s",
-//     //}
-//
-//     res, err := newEtcdV3ServiceDiscovery()
-//     assert.Nil(t, err)
-//     assert.NotNil(t, res)
-//}
-//
-//func TestEtcdV3ServiceDiscovery_GetDefaultPageSize(t *testing.T) {
-//     setUp()
-//     serviceDiscovery := &etcdV3ServiceDiscovery{}
-//     assert.Equal(t, registry.DefaultPageSize, 
serviceDiscovery.GetDefaultPageSize())
-//}
+import (
+       "context"
+       "sync"
+       "testing"
+)
+
+import (
+       gxset "github.com/dubbogo/gost/container/set"
+
+       "github.com/stretchr/testify/assert"
+)
+
+import (
+       "dubbo.apache.org/dubbo-go/v3/common"
+       "dubbo.apache.org/dubbo-go/v3/common/constant"
+       "dubbo.apache.org/dubbo-go/v3/common/extension"
+       "dubbo.apache.org/dubbo-go/v3/protocol"
+       "dubbo.apache.org/dubbo-go/v3/registry"
+       "dubbo.apache.org/dubbo-go/v3/registry/event"
+)
+
+const testName = "test"
+
+func Test_newEtcdV3ServiceDiscovery(t *testing.T) {
+       url, _ := common.NewURL("dubbo://127.0.0.1:2379")
+       sd, err := newEtcdV3ServiceDiscovery(url)
+       assert.Nil(t, err)
+       err = sd.Destroy()
+       assert.Nil(t, err)
+}
+
+func TestEtcdV3ServiceDiscovery_GetDefaultPageSize(t *testing.T) {
+       serviceDiscovery := &etcdV3ServiceDiscovery{}
+       assert.Equal(t, registry.DefaultPageSize, 
serviceDiscovery.GetDefaultPageSize())
+}
+
+func Test_Function(t *testing.T) {
+
+       extension.SetProtocol("mock", func() protocol.Protocol {
+               return &mockProtocol{}
+       })
+
+       url, _ := common.NewURL("dubbo://127.0.0.1:2379")
+       sd, _ := newEtcdV3ServiceDiscovery(url)
+       defer func() {
+               _ = sd.Destroy()
+       }()
+
+       ins := &registry.DefaultServiceInstance{
+               ID:          "testID",
+               ServiceName: testName,
+               Host:        "127.0.0.1",
+               Port:        2233,
+               Enable:      true,
+               Healthy:     true,
+               Metadata:    nil,
+       }
+       ins.Metadata = map[string]string{"t1": "test12", 
constant.MetadataServiceURLParamsPropertyName: 
`{"protocol":"mock","timeout":"10000","version":"1.0.0","dubbo":"2.0.2","release":"2.7.6","port":"2233"}`}
+       err := sd.Register(ins)
+       assert.Nil(t, err)
+
+       wg := &sync.WaitGroup{}
+       wg.Add(1)
+       tn := &testNotify{
+               wg: wg,
+               t:  t,
+       }
+       hs := gxset.NewSet()
+       hs.Add(testName)
+
+       sicl := event.NewServiceInstancesChangedListener(hs)
+       sicl.AddListenerAndNotify(testName, tn)
+       err = sd.AddListener(sicl)
+       assert.NoError(t, err)
+
+       ins = &registry.DefaultServiceInstance{
+               ID:          "testID",
+               ServiceName: testName,
+               Host:        "127.0.0.1",
+               Port:        2233,
+               Enable:      true,
+               Healthy:     true,
+               Metadata:    nil,
+       }
+       ins.Metadata = map[string]string{"t1": "test12", 
constant.MetadataServiceURLParamsPropertyName: 
`{"protocol":"mock","timeout":"10000","version":"1.0.0","dubbo":"2.0.2","release":"2.7.6","port":"2233"}`}
+       err = sd.Update(ins)
+       assert.NoError(t, err)
+       tn.wg.Wait()
+}
+
+type testNotify struct {
+       wg *sync.WaitGroup
+       t  *testing.T
+}
+
+func (tn *testNotify) Notify(e *registry.ServiceEvent) {
+       assert.Equal(tn.t, "2233", e.Service.Port)
+       tn.wg.Done()
+}
+func (tn *testNotify) NotifyAll([]*registry.ServiceEvent, func()) {
+

Review comment:
       写代码的时候不要太随意。没有 body 的 func 不要留空行。




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to