dubbo.apache.org/dubbo-go/v3@v3.1.1/registry/etcdv3/service_discovery_test.go (about) 1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package etcdv3 19 20 import ( 21 "context" 22 "sync" 23 "testing" 24 ) 25 26 import ( 27 "github.com/stretchr/testify/assert" 28 ) 29 30 import ( 31 "dubbo.apache.org/dubbo-go/v3/common" 32 "dubbo.apache.org/dubbo-go/v3/common/constant" 33 "dubbo.apache.org/dubbo-go/v3/common/extension" 34 "dubbo.apache.org/dubbo-go/v3/protocol" 35 "dubbo.apache.org/dubbo-go/v3/registry" 36 ) 37 38 const testName = "test" 39 40 func TestNewEtcdV3ServiceDiscovery(t *testing.T) { 41 url, _ := common.NewURL("dubbo://127.0.0.1:2379") 42 sd, err := newEtcdV3ServiceDiscovery(url) 43 assert.Nil(t, err) 44 err = sd.Destroy() 45 assert.Nil(t, err) 46 } 47 48 func TestEtcdV3ServiceDiscoveryGetDefaultPageSize(t *testing.T) { 49 serviceDiscovery := &etcdV3ServiceDiscovery{} 50 assert.Equal(t, registry.DefaultPageSize, serviceDiscovery.GetDefaultPageSize()) 51 } 52 53 func TestFunction(t *testing.T) { 54 55 extension.SetProtocol("mock", func() protocol.Protocol { 56 return &mockProtocol{} 57 }) 58 59 url, _ := common.NewURL("dubbo://127.0.0.1:2379") 60 sd, _ := newEtcdV3ServiceDiscovery(url) 61 defer func() { 62 _ = sd.Destroy() 63 }() 64 65 ins := ®istry.DefaultServiceInstance{ 66 ID: "testID", 67 ServiceName: testName, 68 Host: "127.0.0.1", 69 Port: 2233, 70 Enable: true, 71 Healthy: true, 72 Metadata: nil, 73 } 74 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"}`} 75 err := sd.Register(ins) 76 assert.Nil(t, err) 77 78 ins = ®istry.DefaultServiceInstance{ 79 ID: "testID", 80 ServiceName: testName, 81 Host: "127.0.0.1", 82 Port: 2233, 83 Enable: true, 84 Healthy: true, 85 Metadata: nil, 86 } 87 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"}`} 88 err = sd.Update(ins) 89 assert.NoError(t, err) 90 err = sd.Unregister(ins) 91 assert.NoError(t, err) 92 } 93 94 type testNotify struct { 95 wg *sync.WaitGroup 96 t *testing.T 97 } 98 99 func (tn *testNotify) Notify(e *registry.ServiceEvent) { 100 assert.Equal(tn.t, "2233", e.Service.Port) 101 tn.wg.Done() 102 } 103 104 func (tn *testNotify) NotifyAll([]*registry.ServiceEvent, func()) {} 105 106 type mockProtocol struct{} 107 108 func (m mockProtocol) Export(protocol.Invoker) protocol.Exporter { 109 panic("implement me") 110 } 111 112 func (m mockProtocol) Refer(*common.URL) protocol.Invoker { 113 return &mockInvoker{} 114 } 115 116 func (m mockProtocol) Destroy() { 117 panic("implement me") 118 } 119 120 type mockInvoker struct{} 121 122 func (m *mockInvoker) GetURL() *common.URL { 123 panic("implement me") 124 } 125 126 func (m *mockInvoker) IsAvailable() bool { 127 panic("implement me") 128 } 129 130 func (m *mockInvoker) Destroy() { 131 panic("implement me") 132 } 133 134 func (m *mockInvoker) Invoke(context.Context, protocol.Invocation) protocol.Result { 135 // for getMetadataInfo and ServiceInstancesChangedListenerImpl onEvent 136 serviceInfo := &common.ServiceInfo{ServiceKey: "test", MatchKey: "test"} 137 services := make(map[string]*common.ServiceInfo) 138 services["test"] = serviceInfo 139 return &protocol.RPCResult{ 140 Rest: &common.MetadataInfo{ 141 Services: services, 142 }, 143 } 144 }