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

Added logs and guards for some cases #32

Merged
merged 3 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading