Skip to content

Commit

Permalink
Added logs and guards for some cases (#32)
Browse files Browse the repository at this point in the history
* Added logs and guards for some cases

* Update tests

* Skip non-existing error
  • Loading branch information
abalaven authored Sep 26, 2024
1 parent 802250a commit fe02b57
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
16 changes: 13 additions & 3 deletions fsnotify/filewatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package fsnotify

import (
"context"
"io/ioutil"
"log"
"net/url"
"os"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -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)
}
Expand All @@ -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
Expand All @@ -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
}
Expand Down Expand Up @@ -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() {
Expand All @@ -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
}
Expand Down
20 changes: 10 additions & 10 deletions fsnotify/filewatcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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
},
Expand All @@ -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()
Expand All @@ -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")

Expand Down

0 comments on commit fe02b57

Please sign in to comment.