github.com/dubbogo/gost@v1.14.0/database/kv/nacos/config_client_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 nacos
    19  
    20  import (
    21  	"fmt"
    22  	"math/rand"
    23  	"reflect"
    24  	"strconv"
    25  	"testing"
    26  	"time"
    27  )
    28  
    29  import (
    30  	"github.com/nacos-group/nacos-sdk-go/v2/common/constant"
    31  	"github.com/nacos-group/nacos-sdk-go/v2/vo"
    32  	"github.com/stretchr/testify/assert"
    33  )
    34  
    35  func TestStructAlign(t *testing.T) {
    36  	typ := reflect.TypeOf(NacosConfigClient{})
    37  	fmt.Printf("Struct is %d bytes long\n", typ.Size())
    38  	n := typ.NumField()
    39  	for i := 0; i < n; i++ {
    40  		field := typ.Field(i)
    41  		fmt.Printf("%s: at offset %v, size=%d, align=%d\n",
    42  			field.Name, field.Offset, field.Type.Size(),
    43  			field.Type.Align())
    44  	}
    45  }
    46  
    47  // TestNewNacosConfigClient config client
    48  func TestNewNacosConfigClient(t *testing.T) {
    49  
    50  	scs := []constant.ServerConfig{*constant.NewServerConfig("console.nacos.io", 80)}
    51  	cc := constant.ClientConfig{TimeoutMs: 5 * 1000, NotLoadCacheAtStart: true}
    52  
    53  	client1, err := NewNacosConfigClient("nacos", true, scs, cc)
    54  	assert.Nil(t, err)
    55  	client2, err := NewNacosConfigClient("nacos", true, scs, cc)
    56  	assert.Nil(t, err)
    57  	client3, err := NewNacosConfigClient("nacos", false, scs, cc)
    58  	assert.Nil(t, err)
    59  	client4, err := NewNacosConfigClient("test", true, scs, cc)
    60  	assert.Nil(t, err)
    61  
    62  	assert.Equal(t, client1, client2)
    63  	assert.Equal(t, client1.activeCount, uint32(2))
    64  	assert.Equal(t, client1.NacosClientValid(), true)
    65  	assert.Equal(t, client3.activeCount, uint32(1))
    66  	assert.Equal(t, client4.activeCount, uint32(1))
    67  
    68  	client1.Close()
    69  	assert.Equal(t, client1.activeCount, uint32(1))
    70  	client1.Close()
    71  
    72  	assert.Equal(t, client1.NacosClientValid(), false)
    73  	assert.Nil(t, client1.Client())
    74  
    75  	client1.Close()
    76  	assert.Equal(t, client1.NacosClientValid(), false)
    77  	assert.Nil(t, client1.Client())
    78  }
    79  
    80  func TestPublishConfig(t *testing.T) {
    81  
    82  	scs := []constant.ServerConfig{*constant.NewServerConfig("console.nacos.io", 8848)}
    83  
    84  	cc := constant.ClientConfig{
    85  		AppName:             "nacos",
    86  		NamespaceId:         "14e01fa8-a4aa-44cc-ad5b-c768f3c62bd5", //namespace id
    87  		TimeoutMs:           5000,
    88  		NotLoadCacheAtStart: true,
    89  		LogDir:              "/tmp/nacos/log",
    90  		CacheDir:            "/tmp/nacos/cache",
    91  		LogLevel:            "debug",
    92  	}
    93  
    94  	client, clientErr := NewNacosConfigClient("nacos", true, scs, cc)
    95  
    96  	assert.Nil(t, clientErr)
    97  
    98  	defer client.Close()
    99  
   100  	t.Run("publishConfig", func(t *testing.T) {
   101  		//publish config
   102  		//config key=dataId+group+namespaceId
   103  		push, err := client.Client().PublishConfig(vo.ConfigParam{
   104  			DataId:  "nacos-config",
   105  			Group:   "dubbo",
   106  			Content: "dubbo-go nb",
   107  		})
   108  		assert.Nil(t, err)
   109  		assert.Equal(t, push, true)
   110  	})
   111  
   112  	t.Run("getConfig", func(t *testing.T) {
   113  		//get config
   114  		cfg, err := client.Client().GetConfig(vo.ConfigParam{
   115  			DataId: "nacos-config",
   116  			Group:  "dubbo",
   117  		})
   118  		assert.Nil(t, err)
   119  		fmt.Println("GetConfig,config :", cfg)
   120  	})
   121  
   122  	t.Run("listenConfig", func(t *testing.T) {
   123  		randomizer := rand.New(rand.NewSource(time.Now().UnixNano()))
   124  		key := strconv.Itoa(randomizer.Intn(100))
   125  		//Listen config change,key=dataId+group+namespaceId.
   126  		err := client.Client().ListenConfig(vo.ConfigParam{
   127  			DataId: "nacos-config" + key,
   128  			Group:  "dubbo",
   129  			OnChange: func(namespace, group, dataId, data string) {
   130  				assert.Equal(t, data, "test-listen")
   131  				fmt.Println("config changed group:" + group + ", dataId:" + dataId + ", content:" + data)
   132  			},
   133  		})
   134  		assert.Nil(t, err)
   135  
   136  		_, err = client.Client().PublishConfig(vo.ConfigParam{
   137  			DataId:  "nacos-config" + key,
   138  			Group:   "dubbo",
   139  			Content: "test-listen",
   140  		})
   141  		assert.Nil(t, err)
   142  
   143  		time.Sleep(2 * time.Second)
   144  		_, err = client.Client().DeleteConfig(vo.ConfigParam{
   145  			DataId: "nacos-config" + key,
   146  			Group:  "dubbo"})
   147  		assert.Nil(t, err)
   148  	})
   149  
   150  	t.Run("searchConfig", func(t *testing.T) {
   151  		searchPage, err := client.Client().SearchConfig(vo.SearchConfigParam{
   152  			Search:   "accurate",
   153  			DataId:   "",
   154  			Group:    "dubbo",
   155  			PageNo:   1,
   156  			PageSize: 10,
   157  		})
   158  		fmt.Printf("Search config:%+v \n", searchPage)
   159  		assert.Nil(t, err)
   160  	})
   161  }