Skip to content

Commit 2deb5ed

Browse files
author
dongsheng.qi
committed
fix: 调整
1 parent 6116cb4 commit 2deb5ed

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

Diff for: client.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,15 @@ func (c *internalClient) GetConfigAndInit(namespace string) *storage.Config {
176176

177177
func (c *internalClient) SyncAndUpdate(namespace string, apolloConfig *config.ApolloConfig) {
178178
// update appConfig only if namespace does not exist yet
179-
if !strings.Contains(c.appConfig.NamespaceName, namespace) {
179+
namespaces := strings.Split(c.appConfig.NamespaceName, ",")
180+
exists := false
181+
for _, n := range namespaces {
182+
if n == namespace {
183+
exists = true
184+
break
185+
}
186+
}
187+
if !exists {
180188
c.appConfig.NamespaceName += "," + namespace
181189
}
182190

Diff for: client_test.go

+20-1
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,6 @@ func TestGetConfigAndInitValNotNil(t *testing.T) {
368368
Configurations: map[string]interface{}{"testKey": "testUpdatedValue"},
369369
}
370370
})
371-
defer patch.Reset()
372371

373372
client := createMockApolloConfig(120)
374373
cf := client.GetConfig("testNotFound")
@@ -380,6 +379,26 @@ func TestGetConfigAndInitValNotNil(t *testing.T) {
380379
// cache should be updated with new configuration
381380
Assert(t, client.cache.GetConfig("testNotFound"), NotNilVal())
382381
Assert(t, client.cache.GetConfig("testNotFound").GetValue("testKey"), Equal("testUpdatedValue"))
382+
Assert(t, client.appConfig.NamespaceName, Equal("application,testNotFound"))
383+
patch.Reset()
384+
385+
// second replace
386+
patch1 := gomonkey.ApplyMethod(reflect.TypeOf(apc), "SyncWithNamespace", func(_ *remote.AbsApolloConfig, namespace string, appConfigFunc func() config.AppConfig) *config.ApolloConfig {
387+
return &config.ApolloConfig{
388+
ApolloConnConfig: config.ApolloConnConfig{
389+
AppID: "testID",
390+
NamespaceName: "testNotFound1",
391+
},
392+
Configurations: map[string]interface{}{"testKey": "testUpdatedValue"},
393+
}
394+
})
395+
defer patch1.Reset()
396+
client.appConfig.NamespaceName = "testNotFound1"
397+
cf1 := client.GetConfig("testNotFound1")
398+
Assert(t, cf1, NotNilVal())
399+
Assert(t, client.cache.GetConfig("testNotFound1"), NotNilVal())
400+
// appConfig namespace existed, should not be appended
401+
Assert(t, client.appConfig.NamespaceName, Equal("testNotFound1"))
383402
}
384403

385404
func TestGetConfigAndInitValNil(t *testing.T) {

0 commit comments

Comments
 (0)