dubbo.apache.org/dubbo-go/v3@v3.1.1/registry/zookeeper/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 zookeeper
    19  
    20  import (
    21  	"context"
    22  	"sync"
    23  	"testing"
    24  )
    25  
    26  import (
    27  	"github.com/nacos-group/nacos-sdk-go/v2/model"
    28  	"github.com/nacos-group/nacos-sdk-go/v2/vo"
    29  
    30  	"github.com/stretchr/testify/assert"
    31  )
    32  
    33  import (
    34  	"dubbo.apache.org/dubbo-go/v3/common"
    35  	"dubbo.apache.org/dubbo-go/v3/common/constant"
    36  	"dubbo.apache.org/dubbo-go/v3/protocol"
    37  	"dubbo.apache.org/dubbo-go/v3/registry"
    38  )
    39  
    40  func Test_newZookeeperServiceDiscovery(t *testing.T) {
    41  	url, _ := common.NewURL("dubbo://127.0.0.1:2181",
    42  		common.WithParamsValue(constant.ClientNameKey, "zk-client"))
    43  	sd, err := newZookeeperServiceDiscovery(url)
    44  	assert.Nil(t, err)
    45  	err = sd.Destroy()
    46  	assert.Nil(t, err)
    47  
    48  }
    49  func Test_zookeeperServiceDiscovery_DataChange(t *testing.T) {
    50  	serviceDiscovery := &zookeeperServiceDiscovery{}
    51  	assert.Equal(t, registry.DefaultPageSize, serviceDiscovery.GetDefaultPageSize())
    52  }
    53  
    54  type testNotify struct {
    55  	wg *sync.WaitGroup
    56  	t  *testing.T
    57  }
    58  
    59  func (tn *testNotify) Notify(e *registry.ServiceEvent) {
    60  	assert.Equal(tn.t, "2181", e.Service.Port)
    61  	tn.wg.Done()
    62  }
    63  
    64  func (tn *testNotify) NotifyAll([]*registry.ServiceEvent, func()) {}
    65  
    66  type mockClient struct {
    67  	instance []interface{}
    68  }
    69  
    70  func (c mockClient) RegisterInstance(param vo.RegisterInstanceParam) (bool, error) {
    71  	return true, nil
    72  }
    73  
    74  func (c mockClient) DeregisterInstance(param vo.DeregisterInstanceParam) (bool, error) {
    75  	return true, nil
    76  }
    77  
    78  func (c mockClient) UpdateInstance(param vo.UpdateInstanceParam) (bool, error) {
    79  	return true, nil
    80  }
    81  
    82  func (c mockClient) GetService(param vo.GetServiceParam) (model.Service, error) {
    83  	panic("implement me")
    84  }
    85  
    86  func (c mockClient) SelectInstances(param vo.SelectInstancesParam) ([]model.Instance, error) {
    87  	panic("implement me")
    88  }
    89  
    90  func (c mockClient) SelectAllInstances(param vo.SelectAllInstancesParam) ([]model.Instance, error) {
    91  	panic("implement me")
    92  }
    93  
    94  func (c mockClient) SelectOneHealthyInstance(param vo.SelectOneHealthInstanceParam) (*model.Instance, error) {
    95  	panic("implement me")
    96  }
    97  
    98  func (c mockClient) Subscribe(param *vo.SubscribeParam) error {
    99  	return nil
   100  }
   101  
   102  func (c mockClient) Unsubscribe(param *vo.SubscribeParam) error {
   103  	panic("implement me")
   104  }
   105  
   106  func (c mockClient) GetAllServicesInfo(param vo.GetAllServiceInfoParam) (model.ServiceList, error) {
   107  	panic("implement me")
   108  }
   109  
   110  type mockProtocol struct{}
   111  
   112  func (m mockProtocol) Export(protocol.Invoker) protocol.Exporter {
   113  	panic("implement me")
   114  }
   115  
   116  func (m mockProtocol) Refer(*common.URL) protocol.Invoker {
   117  	return &mockInvoker{}
   118  }
   119  
   120  func (m mockProtocol) Destroy() {
   121  	panic("implement me")
   122  }
   123  
   124  type mockInvoker struct{}
   125  
   126  func (m *mockInvoker) GetURL() *common.URL {
   127  	panic("implement me")
   128  }
   129  
   130  func (m *mockInvoker) IsAvailable() bool {
   131  	panic("implement me")
   132  }
   133  
   134  func (m *mockInvoker) Destroy() {
   135  	panic("implement me")
   136  }
   137  
   138  func (m *mockInvoker) Invoke(context.Context, protocol.Invocation) protocol.Result {
   139  	// for getMetadataInfo and ServiceInstancesChangedListenerImpl onEvent
   140  	serviceInfo := &common.ServiceInfo{ServiceKey: "test", MatchKey: "test"}
   141  	services := make(map[string]*common.ServiceInfo)
   142  	services["test"] = serviceInfo
   143  	return &protocol.RPCResult{
   144  		Rest: &common.MetadataInfo{
   145  			Services: services,
   146  		},
   147  	}
   148  }