Skip to content

Commit

Permalink
update nacos_test
Browse files Browse the repository at this point in the history
  • Loading branch information
binbin0325 committed Jul 16, 2020
1 parent b94d867 commit 14500c5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 21 deletions.
8 changes: 4 additions & 4 deletions ext/datasource/nacos/nacos.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type NacosDataSource struct {
datasource.Base
serverConfig constant.ServerConfig
clientConfig constant.ClientConfig
configClient *config_client.ConfigClient
client *config_client.ConfigClient
isInitialized util.AtomicBool
configParam ConfigParam
listener *vo.ConfigParam
Expand Down Expand Up @@ -63,8 +63,8 @@ func (s *NacosDataSource) Initialize() error {
if err != nil {
return errors.Errorf("Nacosclient failed to build, err: %+v", err)
}
s.configClient = &client
err = s.listen(s.configClient)
s.client = &client
err = s.listen(s.client)
return err
}

Expand All @@ -76,7 +76,7 @@ func buildNacosClient(s *NacosDataSource) (nacos_client.INacosClient, error) {
return &nc, err
}
func (s *NacosDataSource) ReadSource() ([]byte, error) {
content, err := s.configClient.GetConfig(vo.ConfigParam{
content, err := s.client.GetConfig(vo.ConfigParam{
DataId: s.configParam.DataId,
Group: s.configParam.Group,
})
Expand Down
50 changes: 33 additions & 17 deletions ext/datasource/nacos/nacos_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,22 @@ var configParam = ConfigParam{
DataId: "system-rules",
Group: "sentinel-go",
}
var configParamErr = ConfigParam{
Group: "sentinel-go",
}

func cretateConfigClientTest() config_client.ConfigClient {
func cretateConfigClientTest() (*config_client.ConfigClient, error) {
nc := nacos_client.NacosClient{}
nc.SetServerConfig([]constant.ServerConfig{serverConfig})
nc.SetClientConfig(clientConfigTest)
nc.SetHttpAgent(&http_agent.HttpAgent{})
client, _ := config_client.NewConfigClient(&nc)
err := nc.SetServerConfig([]constant.ServerConfig{serverConfig})
err = nc.SetClientConfig(constant.ClientConfig{
TimeoutMs: 10000,
ListenInterval: 20000,
BeatInterval: 10000,
})
err = nc.SetHttpAgent(&http_agent.HttpAgent{})
client, err := config_client.NewConfigClient(&nc)

return client
return &client, err
}

func prePushSystemRules(content string) (bool, error) {
client := cretateConfigClientTest()
func prePushSystemRules(client *config_client.ConfigClient, content string) (bool, error) {
success, err := client.PublishConfig(vo.ConfigParam{
DataId: configParam.DataId,
Group: configParam.Group,
Expand Down Expand Up @@ -100,7 +100,9 @@ func TestNewNacosDataSource(t *testing.T) {
func TestNacosDataSource_Initialize(t *testing.T) {

t.Run("NacosDataSource_Initialize_BuildNacosClient", func(t *testing.T) {
published, err := prePushSystemRules(TestSystemRules)
client, err := cretateConfigClientTest()
assert.Nil(t, err)
published, err := prePushSystemRules(client, TestSystemRules)
assert.True(t, err == nil && published, "Push systemRules configuration is successful.")

nds, err := getNacosDataSource(clientConfigTest, serverConfig, configParam)
Expand All @@ -110,7 +112,9 @@ func TestNacosDataSource_Initialize(t *testing.T) {
})

t.Run("NacosDataSource_Initialize_BuildNacosClientErr", func(t *testing.T) {
published, err := prePushSystemRules(TestSystemRules)
client, err := cretateConfigClientTest()
assert.Nil(t, err)
published, err := prePushSystemRules(client, TestSystemRules)
assert.True(t, err == nil && published, "Push systemRules configuration is successful.")

clientConfigTest.TimeoutMs = 0
Expand All @@ -123,10 +127,16 @@ func TestNacosDataSource_Initialize(t *testing.T) {

func TestNacosDataSource_ReadSource(t *testing.T) {
t.Run("NacosDataSource_ReadSource", func(t *testing.T) {
published, err := prePushSystemRules(TestSystemRules)
client, err := cretateConfigClientTest()
assert.Nil(t, err)
published, err := prePushSystemRules(client, TestSystemRules)
assert.True(t, err == nil && published, "Push systemRules configuration is successful.")

nds, err := getNacosDataSource(clientConfigTest, serverConfig, configParam)
nds, err := getNacosDataSource(constant.ClientConfig{
TimeoutMs: 10000,
ListenInterval: 20000,
BeatInterval: 10000,
}, serverConfig, configParam)
assert.True(t, err == nil, "New NacosDataSource success.")
err = nds.Initialize()
assert.True(t, err == nil, "NacosDataSource initialize.")
Expand All @@ -137,10 +147,16 @@ func TestNacosDataSource_ReadSource(t *testing.T) {
}

func TestNacosDataSource_Close(t *testing.T) {
published, err := prePushSystemRules(TestSystemRules)
client, err := cretateConfigClientTest()
assert.Nil(t, err)
published, err := prePushSystemRules(client, TestSystemRules)
assert.True(t, err == nil && published, "Push systemRules configuration is successful.")

nds, err := getNacosDataSource(clientConfigTest, serverConfig, configParam)
nds, err := getNacosDataSource(constant.ClientConfig{
TimeoutMs: 10000,
ListenInterval: 20000,
BeatInterval: 10000,
}, serverConfig, configParam)
assert.True(t, err == nil, "New NacosDataSource success.")
err = nds.Initialize()
assert.True(t, err == nil, "NacosDataSource initialize.")
Expand Down

0 comments on commit 14500c5

Please sign in to comment.