Skip to content

Commit

Permalink
Check error in file watcher (#837)
Browse files Browse the repository at this point in the history
  • Loading branch information
aphralG authored Sep 13, 2024
1 parent a248fb6 commit 70a78e7
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion internal/watcher/file/file_watcher_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,12 @@ func (fws *FileWatcherService) watchDirectories(ctx context.Context) {

slog.DebugContext(ctx, "Creating file watchers", "directory", dir)

err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
err := filepath.Walk(dir, func(path string, info os.FileInfo, fileWalkErr error) error {
if fileWalkErr != nil {
return fileWalkErr
}
fws.addWatcher(ctx, path, info)

return nil
})
if err != nil {
Expand Down
14 changes: 14 additions & 0 deletions internal/watcher/file/file_watcher_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@
package file

import (
"bytes"
"context"
"os"
"path"
"strings"
"testing"
"time"

"github.com/nginx/agent/v3/test/stub"

"github.com/fsnotify/fsnotify"
"github.com/nginx/agent/v3/test/types"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -116,6 +120,16 @@ func TestFileWatcherService_removeWatcher(t *testing.T) {
value, ok := fileWatcherService.directoriesBeingWatched.Load(testDirectory)
assert.Nil(t, value)
assert.False(t, ok)

logBuf := &bytes.Buffer{}
stub.StubLoggerWith(logBuf)

fileWatcherService.directoriesBeingWatched.Store(testDirectory, true)
fileWatcherService.removeWatcher(ctx, testDirectory)

if s := logBuf.String(); !strings.Contains(s, "Failed to remove file watcher") {
t.Errorf("Unexpected log %s", s)
}
}

func TestFileWatcherService_isEventSkippable(t *testing.T) {
Expand Down

0 comments on commit 70a78e7

Please sign in to comment.