-
Notifications
You must be signed in to change notification settings - Fork 701
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: xugang <xugang@colipu.com>
- Loading branch information
Showing
7 changed files
with
464 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
149 changes: 149 additions & 0 deletions
149
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.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// 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 ( | ||
"github.com/douyu/jupiter/pkg/conf" | ||
"github.com/nacos-group/nacos-sdk-go/v2/clients/config_client" | ||
"github.com/nacos-group/nacos-sdk-go/v2/vo" | ||
) | ||
|
||
type nacosDataSource struct { | ||
client config_client.IConfigClient | ||
group string | ||
dataID string | ||
|
||
changed chan 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, | ||
changed: make(chan struct{}, 1), | ||
} | ||
if watch { | ||
_ = ds.client.ListenConfig(vo.ConfigParam{ | ||
Group: ds.group, | ||
DataId: ds.dataID, | ||
OnChange: func(namespace, group, dataId, data string) { | ||
ds.changed <- struct{}{} | ||
}, | ||
}) | ||
} | ||
return ds | ||
} | ||
|
||
// ReadConfig reads config content from nacos | ||
func (ds *nacosDataSource) ReadConfig() ([]byte, error) { | ||
configData, err := ds.client.GetConfig(vo.ConfigParam{ | ||
Group: ds.group, | ||
DataId: ds.dataID, | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return []byte(configData), nil | ||
} | ||
|
||
// 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{ | ||
Group: ds.group, | ||
DataId: ds.dataID, | ||
}) | ||
ds.client.CloseClient() | ||
close(ds.changed) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
Oops, something went wrong.