dubbo.apache.org/dubbo-go/v3@v3.1.1/config_center/nacos/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  	"net/url"
    22  	"testing"
    23  	"time"
    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  )
    34  
    35  func TestNewNacosClient(t *testing.T) {
    36  
    37  	params := url.Values{}
    38  	params.Set(constant.NacosNotLoadLocalCache, "true")
    39  
    40  	params.Set(constant.NacosNamespaceID, "nacos")
    41  	params.Set(constant.TimeoutKey, "5s")
    42  	params.Set(constant.ClientNameKey, "nacos-client")
    43  
    44  	registryUrl, _ := common.NewURL("registry://127.0.0.1:8848", common.WithParams(params))
    45  
    46  	c := &nacosDynamicConfiguration{
    47  		url:  registryUrl,
    48  		done: make(chan struct{}),
    49  	}
    50  	err := ValidateNacosClient(c)
    51  	assert.NoError(t, err)
    52  	c.wg.Add(1)
    53  	go HandleClientRestart(c)
    54  	go func() {
    55  		// c.configClient.Close() and <-c.configClient.Done() have order requirements.
    56  		// If c.configClient.Close() is called first.It is possible that "go HandleClientRestart(c)"
    57  		// sets c.configClient to nil before calling c.configClient.Done().
    58  		time.Sleep(time.Second)
    59  		c.client.Close()
    60  	}()
    61  	//<-c.client.Done()
    62  	c.Destroy()
    63  }
    64  
    65  func TestSetNacosClient(t *testing.T) {
    66  
    67  	params := url.Values{}
    68  	params.Set(constant.NacosNotLoadLocalCache, "true")
    69  
    70  	params.Set(constant.NacosNamespaceID, "nacos")
    71  	params.Set(constant.TimeoutKey, "5s")
    72  	params.Set(constant.ClientNameKey, "nacos-client")
    73  
    74  	registryUrl, _ := common.NewURL("registry://127.0.0.1:8848", common.WithParams(params))
    75  
    76  	c := &nacosDynamicConfiguration{
    77  		url:  registryUrl,
    78  		done: make(chan struct{}),
    79  	}
    80  
    81  	err := ValidateNacosClient(c)
    82  	assert.NoError(t, err)
    83  	c.wg.Add(1)
    84  	go HandleClientRestart(c)
    85  	go func() {
    86  		// c.configClient.Close() and <-c.configClient.Done() have order requirements.
    87  		// If c.configClient.Close() is called first.It is possible that "go HandleClientRestart(c)"
    88  		// sets c.configClient to nil before calling c.configClient.Done().
    89  		time.Sleep(time.Second)
    90  		c.client.Close()
    91  	}()
    92  	c.Destroy()
    93  }
    94  
    95  func TestNewNacosClient_connectError(t *testing.T) {
    96  	params := url.Values{}
    97  	params.Set(constant.NacosNotLoadLocalCache, "true")
    98  
    99  	params.Set(constant.NacosNamespaceID, "nacos")
   100  	params.Set(constant.TimeoutKey, "5s")
   101  	params.Set(constant.ClientNameKey, "nacos-client")
   102  
   103  	registryUrl, err := common.NewURL("registry://127.0.0.1:8848", common.WithParams(params))
   104  
   105  	assert.Nil(t, err)
   106  	c := &nacosDynamicConfiguration{
   107  		url:  registryUrl,
   108  		done: make(chan struct{}),
   109  	}
   110  	err = ValidateNacosClient(c)
   111  	assert.NoError(t, err)
   112  	c.wg.Add(1)
   113  	go HandleClientRestart(c)
   114  	go func() {
   115  		// c.configClient.Close() and <-c.configClient.Done() have order requirements.
   116  		// If c.configClient.Close() is called first.It is possible that "go HandleClientRestart(c)"
   117  		// sets c.configClient to nil before calling c.configClient.Done().
   118  		time.Sleep(time.Second)
   119  		c.client.Close()
   120  	}()
   121  	// <-c.client.Done()
   122  	// let configClient do retry
   123  	time.Sleep(5 * time.Second)
   124  	c.Destroy()
   125  }