From 78611fda8e892b9b9e863091f3226cda684a622d Mon Sep 17 00:00:00 2001
From: tiecheng <18768171164@163.com>
Date: Tue, 8 Sep 2020 10:04:12 +0800
Subject: [PATCH] fix impl_test mockDataListener use

---
 config_center/file/impl.go         |  4 +++-
 config_center/file/impl_test.go    | 22 ++++++----------------
 config_center/file/listener.go     | 11 ++++++++---
 registry/file/service_discovery.go |  2 +-
 4 files changed, 18 insertions(+), 21 deletions(-)

diff --git a/config_center/file/impl.go b/config_center/file/impl.go
index 765fe7c79..5293b73eb 100644
--- a/config_center/file/impl.go
+++ b/config_center/file/impl.go
@@ -90,6 +90,7 @@ func newFileSystemDynamicConfiguration(url *common.URL) (*FileSystemDynamicConfi
 	return c, nil
 }
 
+// RootPath get root path
 func (fsdc *FileSystemDynamicConfiguration) RootPath() string {
 	return fsdc.rootPath
 }
@@ -185,9 +186,10 @@ func (fsdc *FileSystemDynamicConfiguration) RemoveConfig(key string, group strin
 
 // Close close file watcher
 func (fsdc *FileSystemDynamicConfiguration) Close() error {
-	return fsdc.cacheListener.watch.Close()
+	return fsdc.cacheListener.Close()
 }
 
+// GetPath get path
 func (fsdc *FileSystemDynamicConfiguration) GetPath(key string, group string) string {
 	if len(key) == 0 {
 		return path.Join(fsdc.rootPath, group)
diff --git a/config_center/file/impl_test.go b/config_center/file/impl_test.go
index 607a89ced..5f5e64878 100644
--- a/config_center/file/impl_test.go
+++ b/config_center/file/impl_test.go
@@ -20,7 +20,6 @@ package file
 import (
 	"fmt"
 	"os"
-	"sync"
 	"testing"
 	"time"
 )
@@ -72,11 +71,11 @@ func TestAddListener(t *testing.T) {
 	listener := &mockDataListener{}
 	file.AddListener(key, listener, config_center.WithGroup(group))
 
-	listener.wg.Add(1)
 	value = "Test Value 2"
 	err = file.PublishConfig(key, group, value)
 	assert.NoError(t, err)
-	listener.wg.Wait()
+	// remove need wait a moment
+	time.Sleep(time.Second)
 	defer destroy(file.rootPath, file)
 }
 
@@ -90,22 +89,18 @@ func TestRemoveListener(t *testing.T) {
 	listener := &mockDataListener{}
 	file.AddListener(key, listener, config_center.WithGroup(group))
 
-	listener.wg.Add(1)
 	value = "Test Value 2"
 	err = file.PublishConfig(key, group, value)
 	assert.NoError(t, err)
 
 	// make sure callback before RemoveListener
 	time.Sleep(time.Second)
-
-	listener.wg.Add(1)
 	file.RemoveListener(key, listener, config_center.WithGroup(group))
 	value = "Test Value 3"
 	err = file.PublishConfig(key, group, value)
 	assert.NoError(t, err)
-	listener.wg.Done()
-
-	listener.wg.Wait()
+	// remove need wait a moment
+	time.Sleep(time.Second)
 	defer destroy(file.rootPath, file)
 }
 
@@ -152,13 +147,8 @@ func destroy(path string, fdc *FileSystemDynamicConfiguration) {
 	os.RemoveAll(path)
 }
 
-type mockDataListener struct {
-	wg    sync.WaitGroup
-	event string
-}
+type mockDataListener struct{}
 
 func (l *mockDataListener) Process(configType *config_center.ConfigChangeEvent) {
-	fmt.Println("process!!!!!")
-	l.wg.Done()
-	l.event = configType.Key
+	fmt.Printf("process!!!!! %v", configType)
 }
diff --git a/config_center/file/listener.go b/config_center/file/listener.go
index d1922a1fe..fc1facf4d 100644
--- a/config_center/file/listener.go
+++ b/config_center/file/listener.go
@@ -33,6 +33,7 @@ import (
 	"github.com/apache/dubbo-go/remoting"
 )
 
+// CacheListener is file watcher
 type CacheListener struct {
 	watch        *fsnotify.Watcher
 	keyListeners sync.Map
@@ -69,7 +70,10 @@ func NewCacheListener(rootPath string) *CacheListener {
 					}
 				}
 			case err := <-watch.Errors:
-				logger.Warnf("file : listen watch fail:", err)
+				// err may be nil, ignore
+				if err != nil {
+					logger.Warnf("file : listen watch fail:%+v", err)
+				}
 			}
 		}
 	}()
@@ -105,12 +109,13 @@ func callback(listener config_center.ConfigurationListener, path, data string, e
 	listener.Process(&config_center.ConfigChangeEvent{Key: path, Value: data, ConfigType: event})
 }
 
-func (cl *CacheListener) Close() {
+// Close will remove key listener and close watcher
+func (cl *CacheListener) Close() error {
 	cl.keyListeners.Range(func(key, value interface{}) bool {
 		cl.keyListeners.Delete(key)
 		return true
 	})
-	cl.watch.Close()
+	return cl.watch.Close()
 }
 
 // AddListener will add a listener if loaded
diff --git a/registry/file/service_discovery.go b/registry/file/service_discovery.go
index 700cabd08..d10de1818 100644
--- a/registry/file/service_discovery.go
+++ b/registry/file/service_discovery.go
@@ -134,7 +134,7 @@ func (fssd *fileSystemServiceDiscovery) Destroy() error {
 
 // nolint
 func (fssd *fileSystemServiceDiscovery) releaseAndRemoveRegistrationFiles(file string) {
-	os.Remove(file)
+	os.RemoveAll(file)
 }
 
 // ----------------- registration ----------------
-- 
GitLab