Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding Unwatch function for file provider #245

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 15 additions & 2 deletions providers/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import (

// File implements a File provider.
type File struct {
path string
path string
watchers []*fsnotify.Watcher
}

// Provider returns a file provider.
Expand Down Expand Up @@ -122,7 +123,19 @@ func (f *File) Watch(cb func(event interface{}, err error)) error {

w.Close()
}()

f.watchers = append(f.watchers, w)
// Watch the directory for changes.
return w.Add(fDir)
}

// Unwatch stops watching the files. It closes all the fsnotify watchers
// and event channels associated with them.
func (f *File) Unwatch() error {
for _, watcher := range f.watchers {
rhnvrm marked this conversation as resolved.
Show resolved Hide resolved
err := watcher.Close()
if err != nil {
return err
}
}
return nil
}
33 changes: 33 additions & 0 deletions tests/koanf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,39 @@ func TestWatchFile(t *testing.T) {
}, "file watch reload didn't change config")
}

func TestUnwatchFile(t *testing.T) {
var (
assert = assert.New(t)
k = koanf.New(delim)
)

// Create a tmp config file.
tmpDir := t.TempDir()
tmpFile := filepath.Join(tmpDir, "koanf_mock")
err := os.WriteFile(tmpFile, []byte(`{"parent": {"name": "name1"}}`), 0600)
require.NoError(t, err, "error creating temp config file: %v", err)

// Load the new config file.
f := file.Provider(tmpFile)
k.Load(f, json.Parser())

var wg sync.WaitGroup
wg.Add(1) // our assurance that cb is called max once
f.Watch(func(event interface{}, err error) {
expectedErrorMessage := "fsnotify watch channel closed"
// When the file is changed, and the fsnotify watch channel is closed, an error is thrown. Assert this condition.
assert.EqualError(err, expectedErrorMessage, "Expected error message: %s", expectedErrorMessage)
wg.Done()
})

// unwatch the file so that it is not reloaded when changed
f.Unwatch()
// Wait a second and change the file.
time.Sleep(1 * time.Second)
os.WriteFile(tmpFile, []byte(`{"parent": {"name": "name2"}}`), 0600)
wg.Wait()
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the sequence of Watch/Unwatch/Watch needs to be checked

func TestWatchFileSymlink(t *testing.T) {
var (
assert = assert.New(t)
Expand Down