Skip to content
Snippets Groups Projects
Commit 934ad5c4 authored by tiecheng's avatar tiecheng
Browse files

remove callback add

watcher error print warn
parent 3ecffc7b
No related branches found
No related tags found
No related merge requests found
......@@ -76,19 +76,11 @@ func TestAddListener(t *testing.T) {
value = "Test Value 2"
err = file.PublishConfig(key, group, value)
assert.NoError(t, err)
listener.wg.Add(1)
value = "Test Value 3"
err = file.PublishConfig(key, group, value)
assert.NoError(t, err)
listener.wg.Wait()
time.Sleep(time.Second)
defer destroy(file.rootPath, file)
}
func TestAddAndRemoveListener(t *testing.T) {
func TestRemoveListener(t *testing.T) {
file, err := initFileData(t)
group := "dubbogo"
value := "Test Value"
......@@ -103,19 +95,17 @@ func TestAddAndRemoveListener(t *testing.T) {
err = file.PublishConfig(key, group, value)
assert.NoError(t, err)
// sleep, make sure callback run success, do `l.wg.Done()`
// make sure callback before RemoveListener
time.Sleep(time.Second)
file.RemoveListener(key, listener, config_center.WithGroup(group))
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()
time.Sleep(time.Second)
defer destroy(file.rootPath, file)
}
......@@ -158,8 +148,8 @@ func TestPublishConfig(t *testing.T) {
}
func destroy(path string, fdc *FileSystemDynamicConfiguration) {
os.RemoveAll(path)
fdc.Close()
os.RemoveAll(path)
}
type mockDataListener struct {
......
......@@ -55,21 +55,21 @@ func NewCacheListener(rootPath string) *CacheListener {
logger.Debugf("watcher %s, event %v", cl.rootPath, event)
if event.Op&fsnotify.Write == fsnotify.Write {
if l, ok := cl.keyListeners.Load(key); ok {
allCallback(l.(map[config_center.ConfigurationListener]struct{}), key, remoting.EventTypeUpdate)
dataChangeCallback(l.(map[config_center.ConfigurationListener]struct{}), key, remoting.EventTypeUpdate)
}
}
if event.Op&fsnotify.Create == fsnotify.Create {
if l, ok := cl.keyListeners.Load(key); ok {
allCallback(l.(map[config_center.ConfigurationListener]struct{}), key, remoting.EventTypeAdd)
dataChangeCallback(l.(map[config_center.ConfigurationListener]struct{}), key, remoting.EventTypeAdd)
}
}
if event.Op&fsnotify.Remove == fsnotify.Remove {
if l, ok := cl.keyListeners.Load(key); ok {
allCallback(l.(map[config_center.ConfigurationListener]struct{}), key, remoting.EventTypeDel)
removeCallback(l.(map[config_center.ConfigurationListener]struct{}), key, remoting.EventTypeDel)
}
}
case err := <-watch.Errors:
logger.Errorf("file : listen watch fail:", err)
logger.Warnf("file : listen watch fail:", err)
}
}
}()
......@@ -82,7 +82,16 @@ func NewCacheListener(rootPath string) *CacheListener {
return cl
}
func allCallback(lmap map[config_center.ConfigurationListener]struct{}, key string, event remoting.EventType) {
func removeCallback(lmap map[config_center.ConfigurationListener]struct{}, key string, event remoting.EventType) {
if len(lmap) == 0 {
logger.Warnf("file watch callback but configuration listener is empty, key:%s, event:%v", key, event)
}
for l := range lmap {
callback(l, key, "", event)
}
}
func dataChangeCallback(lmap map[config_center.ConfigurationListener]struct{}, key string, event remoting.EventType) {
if len(lmap) == 0 {
logger.Warnf("file watch callback but configuration listener is empty, key:%s, event:%v", key, event)
}
......@@ -105,6 +114,7 @@ func (cl *CacheListener) Close() {
}
// AddListener will add a listener if loaded
// if you watcher a file or directory not exist, will error with no such file or directory
func (cl *CacheListener) AddListener(key string, listener config_center.ConfigurationListener) {
// reference from https://stackoverflow.com/questions/34018908/golang-why-dont-we-have-a-set-datastructure
// make a map[your type]struct{} like set in java
......
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