Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix listen config not notify change #50

Merged
merged 1 commit into from
Apr 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion clients/config_client/config_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
59 changes: 24 additions & 35 deletions clients/config_client/config_client_test.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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",
Expand All @@ -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
Expand Down
4 changes: 0 additions & 4 deletions clients/nacos_client/nacos_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions clients/naming_client/naming_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
72 changes: 72 additions & 0 deletions example/config/main.go
Original file line number Diff line number Diff line change
@@ -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 {}

}
2 changes: 1 addition & 1 deletion example/main/main.go → example/service/main.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package service

import (
"github.com/nacos-group/nacos-sdk-go/clients"
Expand Down