diff --git a/fsnotify/filewatcher.go b/fsnotify/filewatcher.go index 7262bb4..f68fead 100644 --- a/fsnotify/filewatcher.go +++ b/fsnotify/filewatcher.go @@ -2,9 +2,9 @@ package fsnotify import ( "context" - "io/ioutil" "log" "net/url" + "os" "strings" "sync" "time" @@ -43,7 +43,7 @@ type pathWatch struct { } func readConfigFile(path string) (v []byte, err error) { - v, err = ioutil.ReadFile(path) + v, err = os.ReadFile(path) if err != nil { return nil, errors.Wrapf(err, "could not read %v", path) } @@ -52,7 +52,11 @@ func readConfigFile(path string) (v []byte, err error) { } func resync(w watcher, pth string) (string, error) { - w.Remove(pth) + log.Printf("fsnotify: Path Name-Resync=%s", pth) + err := w.Remove(pth) + if err != nil && !errors.Is(err, rfsnotify.ErrNonExistentWatch) { + return "", err + } bs, err := readConfigFile(pth) if err != nil { return "", err @@ -65,6 +69,7 @@ func (s *Strategy) run() { for { select { case e := <-s.watcher.GetEvents(): + log.Printf("fsnotify: Path Name-Run=%s", e.Name) if e.Op != rfsnotify.Write && e.Op != rfsnotify.Remove { continue } @@ -98,6 +103,10 @@ func (s *Strategy) run() { func (s *Strategy) setVal(pth string, val string) { s.mu.Lock() defer s.mu.Unlock() + if _, ok := s.paths[pth]; !ok { + log.Printf("fsnotify: Path not in map=%s", pth) + return + } s.paths[pth].value = val values := s.paths[pth].values go func() { @@ -120,6 +129,7 @@ func (s *Strategy) Watch(ctx context.Context, pth string, options url.Values) (v } notifier, found := s.paths[pth] if !found { + log.Printf("fsnotify: Path Name-Init=%s", pth) if err := s.watcher.Add(pth); err != nil { return "", nil, err } diff --git a/fsnotify/filewatcher_test.go b/fsnotify/filewatcher_test.go index 08c9fed..1b67aa4 100644 --- a/fsnotify/filewatcher_test.go +++ b/fsnotify/filewatcher_test.go @@ -3,14 +3,14 @@ package fsnotify import ( "context" "fmt" + "net/url" + "os" + "time" + rfsnotify "github.com/fsnotify/fsnotify" . "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo/extensions/table" . "github.com/onsi/gomega" - "io/ioutil" - "net/url" - "os" - "time" ) func assertStringFromChannel(name string, want string, from <-chan string) { @@ -113,7 +113,7 @@ var _ = Describe("FileWatcher", func() { }), Entry("URL surrounded with whitespaces --> URL trimmed", test{ setup: func(args *args) { - f, _ := ioutil.TempFile("", "unittest_") + f, _ := os.CreateTemp("", "unittest_") f.Write([]byte("\r\n \t " + paramsURL + " \t \r\n")) args.pth = f.Name() f.Close() @@ -131,7 +131,7 @@ var _ = Describe("FileWatcher", func() { }), Entry("params surrounded with whitespaces --> params trimmed", test{ setup: func(args *args) { - f, _ := ioutil.TempFile("", "unittest_") + f, _ := os.CreateTemp("", "unittest_") f.Write([]byte("\r\n \t " + paramsParsed + " \t \r\n")) args.pth = f.Name() f.Close() @@ -149,7 +149,7 @@ var _ = Describe("FileWatcher", func() { }), Entry("a, update b", test{ setup: func(args *args) { - f, _ := ioutil.TempFile("", "unittest_") + f, _ := os.CreateTemp("", "unittest_") f.Write([]byte("a")) args.pth = f.Name() f.Close() @@ -159,7 +159,7 @@ var _ = Describe("FileWatcher", func() { if value != "a" { return fmt.Errorf("expected 'a' got %v", value) } - ioutil.WriteFile(args.pth, []byte("b"), 0660) + os.WriteFile(args.pth, []byte("b"), 0660) assertStringFromChannel("wating for update b", "b", values) return nil }, @@ -169,7 +169,7 @@ var _ = Describe("FileWatcher", func() { }), Entry("a, rm a, create b", test{ setup: func(args *args) { - f, _ := ioutil.TempFile("", "unittest_") + f, _ := os.CreateTemp("", "unittest_") f.Write([]byte("a")) args.pth = f.Name() f.Close() @@ -187,7 +187,7 @@ var _ = Describe("FileWatcher", func() { return fmt.Errorf("expected no change, got %v", v) case <-time.After(time.Second): } - err = ioutil.WriteFile(args.pth, []byte("b"), 0660) + err = os.WriteFile(args.pth, []byte("b"), 0660) Expect(err).ToNot(HaveOccurred(), "creating new file")