Skip to content

Commit

Permalink
fix: 调整
Browse files Browse the repository at this point in the history
  • Loading branch information
dongsheng.qi committed Mar 26, 2024
1 parent 6116cb4 commit 2deb5ed
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
10 changes: 9 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,15 @@ func (c *internalClient) GetConfigAndInit(namespace string) *storage.Config {

func (c *internalClient) SyncAndUpdate(namespace string, apolloConfig *config.ApolloConfig) {
// update appConfig only if namespace does not exist yet
if !strings.Contains(c.appConfig.NamespaceName, namespace) {
namespaces := strings.Split(c.appConfig.NamespaceName, ",")
exists := false
for _, n := range namespaces {
if n == namespace {
exists = true
break
}
}
if !exists {
c.appConfig.NamespaceName += "," + namespace
}

Expand Down
21 changes: 20 additions & 1 deletion client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,6 @@ func TestGetConfigAndInitValNotNil(t *testing.T) {
Configurations: map[string]interface{}{"testKey": "testUpdatedValue"},
}
})
defer patch.Reset()

client := createMockApolloConfig(120)
cf := client.GetConfig("testNotFound")
Expand All @@ -380,6 +379,26 @@ func TestGetConfigAndInitValNotNil(t *testing.T) {
// cache should be updated with new configuration
Assert(t, client.cache.GetConfig("testNotFound"), NotNilVal())
Assert(t, client.cache.GetConfig("testNotFound").GetValue("testKey"), Equal("testUpdatedValue"))
Assert(t, client.appConfig.NamespaceName, Equal("application,testNotFound"))
patch.Reset()

// second replace
patch1 := gomonkey.ApplyMethod(reflect.TypeOf(apc), "SyncWithNamespace", func(_ *remote.AbsApolloConfig, namespace string, appConfigFunc func() config.AppConfig) *config.ApolloConfig {
return &config.ApolloConfig{
ApolloConnConfig: config.ApolloConnConfig{
AppID: "testID",
NamespaceName: "testNotFound1",
},
Configurations: map[string]interface{}{"testKey": "testUpdatedValue"},
}
})
defer patch1.Reset()
client.appConfig.NamespaceName = "testNotFound1"
cf1 := client.GetConfig("testNotFound1")
Assert(t, cf1, NotNilVal())
Assert(t, client.cache.GetConfig("testNotFound1"), NotNilVal())
// appConfig namespace existed, should not be appended
Assert(t, client.appConfig.NamespaceName, Equal("testNotFound1"))
}

func TestGetConfigAndInitValNil(t *testing.T) {
Expand Down

0 comments on commit 2deb5ed

Please sign in to comment.