From 8c1a9fa2201acd9d13cadc11dce9b4831929b864 Mon Sep 17 00:00:00 2001 From: Ankhit Bala Venkata Date: Wed, 25 Sep 2024 15:47:57 -0700 Subject: [PATCH 1/3] Added logs and guards for some cases --- fsnotify/filewatcher.go | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/fsnotify/filewatcher.go b/fsnotify/filewatcher.go index 7262bb4..a8ecc16 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) + err := w.Remove(pth) + if err != nil { + log.Printf("fsnotify: Path Name-Resync=%s", pth) + 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 } From b983fa834e2394ee11bae92b0328ad69485838e9 Mon Sep 17 00:00:00 2001 From: Ankhit Bala Venkata Date: Wed, 25 Sep 2024 16:04:38 -0700 Subject: [PATCH 2/3] Update tests --- fsnotify/filewatcher_test.go | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) 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") From 66775d9f985de7767ecad0ad6de2d60d69dd0f9b Mon Sep 17 00:00:00 2001 From: Ankhit Bala Venkata Date: Wed, 25 Sep 2024 16:37:52 -0700 Subject: [PATCH 3/3] Skip non-existing error --- fsnotify/filewatcher.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fsnotify/filewatcher.go b/fsnotify/filewatcher.go index a8ecc16..f68fead 100644 --- a/fsnotify/filewatcher.go +++ b/fsnotify/filewatcher.go @@ -52,9 +52,9 @@ func readConfigFile(path string) (v []byte, err error) { } func resync(w watcher, pth string) (string, error) { + log.Printf("fsnotify: Path Name-Resync=%s", pth) err := w.Remove(pth) - if err != nil { - log.Printf("fsnotify: Path Name-Resync=%s", pth) + if err != nil && !errors.Is(err, rfsnotify.ErrNonExistentWatch) { return "", err } bs, err := readConfigFile(pth)