Skip to content

Commit

Permalink
add nacos datasource test
Browse files Browse the repository at this point in the history
  • Loading branch information
xugang committed May 5, 2023
1 parent f5bbf5d commit daefe56
Show file tree
Hide file tree
Showing 5 changed files with 288 additions and 21 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ require (
github.com/go-redis/redis/v8 v8.11.5
github.com/go-resty/resty/v2 v2.7.0
github.com/gogf/gf v1.16.9
github.com/golang/mock v1.6.0
github.com/golang/protobuf v1.5.3
github.com/gorilla/websocket v1.5.0
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0
Expand Down Expand Up @@ -104,7 +105,6 @@ require (
github.com/goccy/go-json v0.10.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/gohugoio/hugo v0.111.3 // indirect
github.com/golang/mock v1.6.0 // indirect
github.com/golang/snappy v0.0.3 // indirect
github.com/gomodule/redigo v2.0.0+incompatible // indirect
github.com/google/flatbuffers v1.11.0 // indirect
Expand Down
149 changes: 149 additions & 0 deletions pkg/conf/datasource/nacos/mock/mock_config_client_interface.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 26 additions & 20 deletions pkg/conf/datasource/nacos/nacos.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
// Copyright 2022 Douyu
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package nacos

import (
"log"

"github.com/douyu/jupiter/pkg/conf"
"github.com/douyu/jupiter/pkg/util/xgo"
"github.com/nacos-group/nacos-sdk-go/v2/clients/config_client"
"github.com/nacos-group/nacos-sdk-go/v2/vo"
)
Expand All @@ -20,13 +31,19 @@ type nacosDataSource struct {
// NewDataSource creates an nacos DataSource
func NewDataSource(client config_client.IConfigClient, group, dataID string, watch bool) conf.DataSource {
ds := &nacosDataSource{
client: client,
group: group,
dataID: dataID,
client: client,
group: group,
dataID: dataID,
changed: make(chan struct{}, 1),
}
if watch {
ds.changed = make(chan struct{}, 1)
xgo.Go(ds.watch)
_ = ds.client.ListenConfig(vo.ConfigParam{
Group: ds.group,
DataId: ds.dataID,
OnChange: func(namespace, group, dataId, data string) {
ds.changed <- struct{}{}
},
})
}
return ds
}
Expand All @@ -44,25 +61,14 @@ func (ds *nacosDataSource) ReadConfig() ([]byte, error) {
return []byte(configData), nil
}

func (ds *nacosDataSource) watch() {
ds.client.ListenConfig(vo.ConfigParam{
Group: ds.group,
DataId: ds.dataID,
OnChange: func(namespace, group, dataId, data string) {
log.Println("nacos config changed: ", data)
ds.changed <- struct{}{}
},
})
}

// IsConfigChanged returns a chanel for notification when the config changed
func (ds *nacosDataSource) IsConfigChanged() <-chan struct{} {
return ds.changed
}

// Close stops watching the config changed
func (ds *nacosDataSource) Close() error {
ds.client.CancelListenConfig(vo.ConfigParam{
_ = ds.client.CancelListenConfig(vo.ConfigParam{
Group: ds.group,
DataId: ds.dataID,
})
Expand Down
98 changes: 98 additions & 0 deletions pkg/conf/datasource/nacos/nacos_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// Copyright 2022 Douyu
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package nacos

import (
"sync"
"testing"
"time"

"github.com/douyu/jupiter/pkg/conf"
"github.com/douyu/jupiter/pkg/conf/datasource/nacos/mock"
"github.com/golang/mock/gomock"
"github.com/nacos-group/nacos-sdk-go/v2/vo"
"github.com/stretchr/testify/assert"
)

var (
ds conf.DataSource
wg sync.WaitGroup
localParam = vo.ConfigParam{
DataId: "data-id",
Group: "group",
Content: "hello world",
}
newContent = "hello-world-changed"
)

func TestReadConfig(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

client := mock.NewMockIConfigClient(ctrl)
t.Run("with watch", func(t *testing.T) {
client.EXPECT().CancelListenConfig(gomock.Any()).Return(nil)
client.EXPECT().CloseClient().Return()
client.EXPECT().GetConfig(gomock.Any()).Return(localParam.Content, nil)
client.EXPECT().ListenConfig(gomock.Any()).DoAndReturn(func(param vo.ConfigParam) error {
go func() {
defer wg.Done()
time.Sleep(4 * time.Second)
param.OnChange("namespace", localParam.Group, localParam.DataId, newContent)
client.EXPECT().GetConfig(gomock.Any()).Return(newContent, nil)
time.Sleep(2 * time.Second)
teardown(t)
}()
wg.Add(1)
return nil
})

ds = NewDataSource(client, localParam.Group, localParam.DataId, true)
content, err := ds.ReadConfig()
assert.Nil(t, err)
assert.Equal(t, localParam.Content, string(content))
t.Logf("read config: %s", content)

wg.Add(1)
go func() {
defer wg.Done()
for range ds.IsConfigChanged() {
content, err := ds.ReadConfig()
assert.Nil(t, err)
assert.Equal(t, newContent, string(content))
t.Logf("read new config: %s", content)
}
}()

wg.Wait()
})

t.Run("without with", func(t *testing.T) {
client.EXPECT().CancelListenConfig(gomock.Any()).Return(nil)
client.EXPECT().CloseClient().Return()
client.EXPECT().GetConfig(gomock.Any()).Return(localParam.Content, nil)
ds = NewDataSource(client, localParam.Group, localParam.DataId, false)
defer teardown(t)
content, err := ds.ReadConfig()
assert.Nil(t, err)
assert.Equal(t, localParam.Content, string(content))
t.Logf("read config: %s", content)
})
}

func teardown(t *testing.T) {
t.Helper()
ds.Close()
}
14 changes: 14 additions & 0 deletions pkg/conf/datasource/nacos/register.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
// Copyright 2022 Douyu
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package nacos

import (
Expand Down

0 comments on commit daefe56

Please sign in to comment.