Skip to content
Snippets Groups Projects
Commit 40f3a1e4 authored by tiecheng's avatar tiecheng
Browse files

fix impl_test mockDataListener use

parent 7c1c590a
No related branches found
No related tags found
No related merge requests found
......@@ -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)
......
......@@ -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)
}
......@@ -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
......
......@@ -134,7 +134,7 @@ func (fssd *fileSystemServiceDiscovery) Destroy() error {
// nolint
func (fssd *fileSystemServiceDiscovery) releaseAndRemoveRegistrationFiles(file string) {
os.Remove(file)
os.RemoveAll(file)
}
// ----------------- registration ----------------
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment