From ff65a5619a4ccc168978b25b40c2749603761d87 Mon Sep 17 00:00:00 2001 From: lizhipeng Date: Thu, 16 Apr 2020 23:59:52 +0800 Subject: [PATCH] fix listen config not notify change --- clients/config_client/config_client.go | 3 +- clients/config_client/config_client_test.go | 59 +++++++---------- clients/nacos_client/nacos_client.go | 4 -- clients/naming_client/naming_client_test.go | 13 ++++ example/config/main.go | 72 +++++++++++++++++++++ example/{main => service}/main.go | 2 +- 6 files changed, 112 insertions(+), 41 deletions(-) create mode 100644 example/config/main.go rename example/{main => service}/main.go (99%) diff --git a/clients/config_client/config_client.go b/clients/config_client/config_client.go index 4b4272ae..8269eda6 100644 --- a/clients/config_client/config_client.go +++ b/clients/config_client/config_client.go @@ -260,7 +260,8 @@ func (client *ConfigClient) listenConfigTask(clientConfig constant.ClientConfig, for _, serverConfig := range client.configProxy.GetServerList() { path := client.buildBasePath(serverConfig) + "/listener" - changedTmp, err := listen(agent, path, clientConfig.TimeoutMs, clientConfig.ListenInterval, params) + //TimeoutMS必须大于等于listenInterval,否则listen会因为timeout连接中断 + changedTmp, err := listen(agent, path, clientConfig.ListenInterval, clientConfig.ListenInterval, params) if err == nil { changed = changedTmp break diff --git a/clients/config_client/config_client_test.go b/clients/config_client/config_client_test.go index 5545e212..f08ae0e6 100644 --- a/clients/config_client/config_client_test.go +++ b/clients/config_client/config_client_test.go @@ -1,6 +1,7 @@ package config_client import ( + "errors" "fmt" "github.com/golang/mock/gomock" "github.com/nacos-group/nacos-sdk-go/clients/nacos_client" @@ -144,7 +145,7 @@ func Test_SearchConfig(t *testing.T) { PageSize: 10, }) assert.Nil(t, err) - assert.Nil(t, configPage) + assert.NotEmpty(t, configPage) assert.NotEmpty(t, configPage.PageItems) } @@ -387,37 +388,22 @@ func Test_DeleteConfigWithoutGroup(t *testing.T) { func TestListenConfig(t *testing.T) { client := cretateConfigClientTest() + var err error + var success bool + ch := make(chan string) + go func() { + err = client.ListenConfig(vo.ConfigParam{ + DataId: "dataId", + Group: "group", + OnChange: func(namespace, group, dataId, data string) { + fmt.Println("group:" + group + ", dataId:" + dataId + ", data:" + data) + ch <- data + }, + }) + assert.Nil(t, err) + }() - success, err := client.PublishConfig(vo.ConfigParam{ - DataId: "dataId", - Group: "group", - Content: "hello world!"}) - - assert.Nil(t, err) - assert.Equal(t, true, success) - - content := "" - - err = client.ListenConfig(vo.ConfigParam{ - DataId: "dataId", - Group: "group", - OnChange: func(namespace, group, dataId, data string) { - fmt.Println("group:" + group + ", dataId:" + dataId + ", data:" + data) - content = data - }, - }) - - err = client.ListenConfig(vo.ConfigParam{ - DataId: "abc", - Group: "DEFAULT_GROUP", - OnChange: func(namespace, group, dataId, data string) { - fmt.Println("group:" + group + ", dataId:" + dataId + ", data:" + data) - }, - }) - - time.Sleep(5 * time.Second) - - assert.Equal(t, "hello world!", content) + time.Sleep(2 * time.Second) success, err = client.PublishConfig(vo.ConfigParam{ DataId: "dataId", @@ -426,10 +412,13 @@ func TestListenConfig(t *testing.T) { assert.Nil(t, err) assert.Equal(t, true, success) - - time.Sleep(10 * time.Second) - - assert.Equal(t, "abc", content) + select { + case content := <-ch: + fmt.Println("content:" + content) + case <-time.After(10 * time.Second): + fmt.Println("timeout") + assert.Errorf(t, errors.New("timeout"), "timeout") + } } // listenConfigTask diff --git a/clients/nacos_client/nacos_client.go b/clients/nacos_client/nacos_client.go index 731719e8..be37e9fe 100644 --- a/clients/nacos_client/nacos_client.go +++ b/clients/nacos_client/nacos_client.go @@ -33,10 +33,6 @@ func (client *NacosClient) SetClientConfig(config constant.ClientConfig) (err er err = errors.New("[client.SetClientConfig] config.TimeoutMs should > 0") return } - if config.TimeoutMs >= config.ListenInterval { - err = errors.New("[client.SetClientConfig] config.TimeoutMs should < config.ListenInterval") - return - } if config.BeatInterval <= 0 { config.BeatInterval = 5 * 1000 diff --git a/clients/naming_client/naming_client_test.go b/clients/naming_client/naming_client_test.go index 2150024a..285a5975 100644 --- a/clients/naming_client/naming_client_test.go +++ b/clients/naming_client/naming_client_test.go @@ -759,3 +759,16 @@ func TestNamingClient_SelectInstances_Empty(t *testing.T) { assert.NotNil(t, err) assert.Equal(t, 0, len(instances)) } + +func TestNamingClient_GetAllServicesInfo(t *testing.T) { + nc := nacos_client.NacosClient{} + nc.SetServerConfig([]constant.ServerConfig{serverConfigTest}) + nc.SetClientConfig(clientConfigTest) + nc.SetHttpAgent(&http_agent.HttpAgent{}) + client, _ := NewNamingClient(&nc) + reslut, err := client.GetAllServicesInfo(vo.GetAllServiceInfoParam{ + GroupName: "DEFAULT_GROUP", + }) + fmt.Println(len(reslut)) + assert.NotNil(t, err) +} diff --git a/example/config/main.go b/example/config/main.go new file mode 100644 index 00000000..b888148c --- /dev/null +++ b/example/config/main.go @@ -0,0 +1,72 @@ +package main + +import ( + "fmt" + "github.com/nacos-group/nacos-sdk-go/clients/config_client" + "github.com/nacos-group/nacos-sdk-go/clients/nacos_client" + "github.com/nacos-group/nacos-sdk-go/common/constant" + "github.com/nacos-group/nacos-sdk-go/common/http_agent" + "github.com/nacos-group/nacos-sdk-go/vo" + "time" +) + +var clientConfigTest = constant.ClientConfig{ + TimeoutMs: 10 * 1000, + BeatInterval: 5 * 1000, + ListenInterval: 30 * 1000, + NotLoadCacheAtStart: true, +} + +var serverConfigTest = constant.ServerConfig{ + IpAddr: "console.nacos.io", + Port: 80, + ContextPath: "/nacos", +} + +func cretateConfigClientTest() config_client.ConfigClient { + nc := nacos_client.NacosClient{} + nc.SetServerConfig([]constant.ServerConfig{serverConfigTest}) + nc.SetClientConfig(clientConfigTest) + nc.SetHttpAgent(&http_agent.HttpAgent{}) + client, _ := config_client.NewConfigClient(&nc) + return client +} + +func main() { + client := cretateConfigClientTest() + + _, err := client.PublishConfig(vo.ConfigParam{ + DataId: "dataId", + Group: "group", + Content: "hello world!"}) + if err != nil { + fmt.Printf("success err:%s", err.Error()) + } + content := "" + + err = client.ListenConfig(vo.ConfigParam{ + DataId: "dataId", + Group: "group", + OnChange: func(namespace, group, dataId, data string) { + fmt.Println("group:" + group + ", dataId:" + dataId + ", data:" + data) + content = data + }, + }) + + err = client.ListenConfig(vo.ConfigParam{ + DataId: "abc", + Group: "DEFAULT_GROUP", + OnChange: func(namespace, group, dataId, data string) { + fmt.Println("group:" + group + ", dataId:" + dataId + ", data:" + data) + }, + }) + + time.Sleep(5 * time.Second) + _, err = client.PublishConfig(vo.ConfigParam{ + DataId: "dataId", + Group: "group", + Content: "abc"}) + + select {} + +} diff --git a/example/main/main.go b/example/service/main.go similarity index 99% rename from example/main/main.go rename to example/service/main.go index 1493a703..2c2ccb1c 100644 --- a/example/main/main.go +++ b/example/service/main.go @@ -1,4 +1,4 @@ -package main +package service import ( "github.com/nacos-group/nacos-sdk-go/clients"