Skip to content

Commit

Permalink
fix: UT supplement
Browse files Browse the repository at this point in the history
  • Loading branch information
dongsheng.qi committed Mar 6, 2024
1 parent ac9ca83 commit 08f907f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
38 changes: 37 additions & 1 deletion client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,14 @@ import (
"fmt"
"net/http"
"net/http/httptest"
"reflect"
"testing"
"time"

"github.com/agiledragon/gomonkey/v2"

"github.com/apolloconfig/agollo/v4/agcache/memory"
"github.com/apolloconfig/agollo/v4/component/remote"
"github.com/apolloconfig/agollo/v4/env/config"
"github.com/apolloconfig/agollo/v4/env/server"

Expand All @@ -37,7 +41,7 @@ import (

const testDefaultNamespace = "application"

//init param
// init param
func init() {
extension.SetCacheFactory(&memory.DefaultCacheFactory{})
}
Expand Down Expand Up @@ -352,3 +356,35 @@ func TestUseEventDispatch(t *testing.T) {
l := cache.GetChangeListeners()
Assert(t, l.Len(), Equal(1))
}

func TestGetConfigAndInitValNotNil(t *testing.T) {
var apc *remote.AbsApolloConfig
patch := 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: "testNotFound",
},
Configurations: map[string]interface{}{"testKey": "testValue"},
}
})
defer patch.Reset()

client := createMockApolloConfig(120)
cf := client.GetConfig("testNotFound")
Assert(t, cf, NotNilVal())
// cache should be updated
Assert(t, client.cache.GetConfig("testNotFound"), NotNilVal())
}

func TestGetConfigAndInitValNil(t *testing.T) {
var apc *remote.AbsApolloConfig
patch := gomonkey.ApplyMethod(reflect.TypeOf(apc), "SyncWithNamespace", func(_ *remote.AbsApolloConfig, namespace string, appConfigFunc func() config.AppConfig) *config.ApolloConfig {
return nil
})
defer patch.Reset()

client := createMockApolloConfig(120)
cf := client.GetConfig("testNotFound")
Assert(t, cf, NilVal())
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module github.com/apolloconfig/agollo/v4

require (
github.com/agiledragon/gomonkey/v2 v2.11.0 // indirect
github.com/spf13/viper v1.8.1
github.com/tevid/gohamcrest v1.1.1
)
Expand Down

0 comments on commit 08f907f

Please sign in to comment.