Skip to content

Commit

Permalink
Fixes #5 - DirWatcher & FileWatcher now implement context.Context and…
Browse files Browse the repository at this point in the history
… if the caller needs, one can wait for internal goroutines to finish
  • Loading branch information
illarion committed Sep 10, 2024
1 parent dbeaa2a commit 714973e
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 6 deletions.
8 changes: 5 additions & 3 deletions dirwatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,19 @@ import (
// Events can be masked by providing fileMask. DirWatcher does not generate events for
// folders or subfolders.
type DirWatcher struct {
context.Context
C chan FileEvent
}

// NewDirWatcher creates DirWatcher recursively waiting for events in the given root folder and
// emitting FileEvents in channel C, that correspond to fileMask. Folder events are ignored (having IN_ISDIR set to 1)
func NewDirWatcher(ctx context.Context, fileMask uint32, root string) (*DirWatcher, error) {
ctx, cancel := context.WithCancel(ctx)
dw := &DirWatcher{
C: make(chan FileEvent),
Context: ctx,
C: make(chan FileEvent),
}

ctx, cancel := context.WithCancel(ctx)

i, err := NewInotify(ctx)
if err != nil {
cancel()
Expand Down Expand Up @@ -59,6 +60,7 @@ func NewDirWatcher(ctx context.Context, fileMask uint32, root string) (*DirWatch
events := make(chan FileEvent)

go func() {
defer cancel()
for _, event := range queue {
events <- event
}
Expand Down
18 changes: 18 additions & 0 deletions dirwatcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,22 @@ func TestDirWatcher(t *testing.T) {
}

})

t.Run("ClosedDirwatcherHasDoneContext", func(t *testing.T) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()

dw, err := NewDirWatcher(ctx, IN_CREATE, dir)
if err != nil {
t.Error(err)
}

cancel()

select {
case <-dw.Context.Done():
default:
t.Fail()
}
})
}
7 changes: 4 additions & 3 deletions filewatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,19 @@ import (
// FileWatcher waits for events generated by filesystem for a specific list of file paths, including
// IN_CREATE for not yet existing files and IN_DELETE for removed.
type FileWatcher struct {
context.Context
C chan FileEvent
}

// NewFileWatcher creates FileWatcher with provided inotify mask and list of files to wait events for.
func NewFileWatcher(ctx context.Context, mask uint32, files ...string) (*FileWatcher, error) {

ctx, cancel := context.WithCancel(ctx)
f := &FileWatcher{
C: make(chan FileEvent),
Context: ctx,
C: make(chan FileEvent),
}

ctx, cancel := context.WithCancel(ctx)

inotify, err := NewInotify(ctx)
if err != nil {
cancel()
Expand Down
19 changes: 19 additions & 0 deletions filewatcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,23 @@ func TestFileWatcher(t *testing.T) {

})

t.Run("ClosedFileWatcherHasClosedChannel", func(t *testing.T) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()

fw, err := NewFileWatcher(ctx, IN_ALL_EVENTS, filepath.Join(dir, "foo"))
if err != nil {
t.Error(err)
}

cancel()

select {
case <-fw.Done():
default:
t.Fail()
}

})

}

0 comments on commit 714973e

Please sign in to comment.