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

file/dirwatcher_test: wait for Done() in all relevant tests #8

Closed
wants to merge 1 commit into from
Closed
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
21 changes: 16 additions & 5 deletions dirwatcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,20 @@ func TestDirWatcher(t *testing.T) {
t.Run("ExistingFile", func(t *testing.T) {

ctx, cancel := context.WithCancel(ctx)
defer cancel()
dw, err := NewDirWatcher(ctx, IN_CREATE, dir)
if err != nil {
t.Error(err)
}
defer func() {
cancel()
<-dw.Done()
}()

f, err := os.OpenFile(filepath.Join(dir, "f1"), os.O_CREATE, os.ModePerm)
f.Close()

defer os.Remove(filepath.Join(dir, "f1"))

dw, err := NewDirWatcher(ctx, IN_CREATE, dir)
if err != nil {
t.Error(err)
}
Expand All @@ -45,12 +51,14 @@ func TestDirWatcher(t *testing.T) {
t.Run("FileInSubdir", func(t *testing.T) {

ctx, cancel := context.WithCancel(ctx)
defer cancel()

dw, err := NewDirWatcher(ctx, IN_CREATE, dir)
if err != nil {
t.Error(err)
}
defer func() {
cancel()
<-dw.Done()
}()

err = os.Mkdir(filepath.Join(dir, "subfolder"), os.ModePerm)
if err != nil {
Expand Down Expand Up @@ -82,12 +90,15 @@ func TestDirWatcher(t *testing.T) {

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

dw, err := NewDirWatcher(ctx, IN_CREATE, dir)
if err != nil {
t.Error(err)
}
defer func() {
cancel()
<-dw.Done()
}()

go func() {
for e := range dw.C {
Expand Down
16 changes: 9 additions & 7 deletions filewatcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,20 @@ func TestFileWatcher(t *testing.T) {
defer os.Remove(dir)

t.Run("Simple", func(t *testing.T) {

ctx, cancel := context.WithCancel(ctx)
defer cancel()

f1 := filepath.Join(dir, "/dir1/foo")
f2 := filepath.Join(dir, "/dir2/bar")

os.MkdirAll(filepath.Dir(f1), os.ModePerm)
os.MkdirAll(filepath.Dir(f2), os.ModePerm)

ctx, cancel := context.WithCancel(ctx)
fw, err := NewFileWatcher(ctx, IN_ALL_EVENTS, f1, f2)
if err != nil {
t.Error(err)
}
defer func() {
cancel()
<-fw.Done()
}()

{
f, err := os.OpenFile(f1, os.O_RDWR|os.O_CREATE, 0)
Expand Down Expand Up @@ -64,12 +64,14 @@ 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)
}
defer func() {
cancel()
<-fw.Done()
}()

go func() {
for e := range fw.C {
Expand Down
Loading