diff --git a/client.go b/client.go index 5f2d6e5..af12532 100644 --- a/client.go +++ b/client.go @@ -137,9 +137,7 @@ func StartWithConfig(loadAppConfig func() (*config.AppConfig, error)) (Client, e log.Debug("init notifySyncConfigServices finished") //start long poll sync config - configComponent := ¬ify.ConfigComponent{} - configComponent.SetAppConfig(c.getAppConfig) - configComponent.SetCache(c.cache) + configComponent := notify.NewConfigComponent(c.getAppConfig, c.cache) go component.StartRefreshConfig(configComponent) c.configComponent = configComponent diff --git a/component/notify/componet_notify.go b/component/notify/componet_notify.go index 7d69e95..a860e44 100644 --- a/component/notify/componet_notify.go +++ b/component/notify/componet_notify.go @@ -20,23 +20,32 @@ package notify import ( "time" + "github.com/apolloconfig/agollo/v4/component/log" "github.com/apolloconfig/agollo/v4/component/remote" - "github.com/apolloconfig/agollo/v4/storage" - "github.com/apolloconfig/agollo/v4/env/config" + "github.com/apolloconfig/agollo/v4/storage" ) const ( longPollInterval = 2 * time.Second //2s ) -//ConfigComponent 配置组件 +// ConfigComponent 配置组件 type ConfigComponent struct { appConfigFunc func() config.AppConfig cache *storage.Cache stopCh chan interface{} } +// NewConfigComponent . +func NewConfigComponent(appConfigFunc func() config.AppConfig, cache *storage.Cache) *ConfigComponent { + return &ConfigComponent{ + appConfigFunc: appConfigFunc, + cache: cache, + stopCh: make(chan interface{}), + } +} + // SetAppConfig nolint func (c *ConfigComponent) SetAppConfig(appConfigFunc func() config.AppConfig) { c.appConfigFunc = appConfigFunc @@ -47,7 +56,7 @@ func (c *ConfigComponent) SetCache(cache *storage.Cache) { c.cache = cache } -//Start 启动配置组件定时器 +// Start 启动配置组件定时器 func (c *ConfigComponent) Start() { if c.stopCh == nil { c.stopCh = make(chan interface{}) @@ -56,7 +65,6 @@ func (c *ConfigComponent) Start() { t2 := time.NewTimer(longPollInterval) instance := remote.CreateAsyncApolloConfig() //long poll for sync -loop: for { select { case <-t2.C: @@ -66,7 +74,8 @@ loop: } t2.Reset(longPollInterval) case <-c.stopCh: - break loop + log.Debugf("agollo component stop finished") + return } } } diff --git a/component/notify/componet_notify_test.go b/component/notify/componet_notify_test.go index bb5c4ef..a13f322 100644 --- a/component/notify/componet_notify_test.go +++ b/component/notify/componet_notify_test.go @@ -18,8 +18,10 @@ package notify import ( + "context" "encoding/json" "testing" + "time" "github.com/apolloconfig/agollo/v4/cluster/roundrobin" _ "github.com/apolloconfig/agollo/v4/cluster/roundrobin" @@ -105,7 +107,7 @@ func getTestAppConfig() *config.AppConfig { func TestConfigComponent_SetAppConfig_UpdatesAppConfigCorrectly(t *testing.T) { expectedAppConfig := getTestAppConfig() - c := &ConfigComponent{} + c := NewConfigComponent(nil, nil) // set appConfigFunc c.SetAppConfig(func() config.AppConfig { return *expectedAppConfig @@ -120,3 +122,29 @@ func TestConfigComponent_SetAppConfig_UpdatesAppConfigCorrectly(t *testing.T) { Assert(t, c.appConfigFunc().AppID, Equal("test1")) Assert(t, c.appConfigFunc().NamespaceName, Equal("application,abc")) } + +func TestConfigComponent_Stop(t *testing.T) { + // 测试 stop 快于 start 情况 + c := NewConfigComponent(nil, nil) + c.Stop() + + ctx, cancel := context.WithTimeout(context.TODO(), time.Second) + defer cancel() + + // 启动开始 + done := make(chan struct{}) + go func() { + c.Start() + done <- struct{}{} + }() + + var isDone bool + select { + case <-ctx.Done(): + // 失败 + case <-done: + isDone = true + } + + Assert(t, isDone, Equal(true)) +}