aboutsummaryrefslogtreecommitdiff
path: root/watcher_test.go
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2024-12-23 02:42:06 +0700
committerShulhan <ms@kilabit.info>2025-01-07 00:13:55 +0700
commitb686ea0a41b7af68d4d86ff3cc2c3068ebb88b66 (patch)
treeb8725c3c2e2461e2b0542c76880d385ee5aa6741 /watcher_test.go
parentff338f853eb7537230c84ccc06feae3b63859877 (diff)
downloadciigo-b686ea0a41b7af68d4d86ff3cc2c3068ebb88b66.tar.xz
all: refactoring to use [watchfs/v2]
The [watchfs/v2] bring new enhancements that watching only single file instead of all markup files for changes. This minimize number of goroutine calling os.Stat on each markup files being watched on directory.
Diffstat (limited to 'watcher_test.go')
-rw-r--r--watcher_test.go31
1 files changed, 9 insertions, 22 deletions
diff --git a/watcher_test.go b/watcher_test.go
index 063eb8f..9067d57 100644
--- a/watcher_test.go
+++ b/watcher_test.go
@@ -40,8 +40,7 @@ func TestWatcher(t *testing.T) {
if err != nil {
t.Fatal(err)
}
-
- testWatcher.watchDir.Delay = 100 * time.Millisecond
+ testWatcher.opts.FileWatcherOptions.Interval = 50 * time.Millisecond
err = testWatcher.start()
if err != nil {
@@ -55,7 +54,7 @@ func TestWatcher(t *testing.T) {
t.Fatal(err)
}
- var pathFileMarkup = filepath.Join(testWatcher.dir, `test.adoc`)
+ var pathFileMarkup = filepath.Join(testWatcher.opts.Root, `test.adoc`)
t.Run(`testAdocCreate`, func(tt *testing.T) {
testAdocCreate(tt, testWatcher, tdata, pathFileMarkup)
@@ -67,7 +66,7 @@ func TestWatcher(t *testing.T) {
testAdocDelete(tt, testWatcher, pathFileMarkup)
})
- pathFileMarkup = filepath.Join(testWatcher.dir, `test.md`)
+ pathFileMarkup = filepath.Join(testWatcher.opts.Root, `test.md`)
t.Run(`testMarkdownCreate`, func(tt *testing.T) {
testMarkdownCreate(tt, testWatcher, tdata, pathFileMarkup)
@@ -91,14 +90,11 @@ func testAdocCreate(t *testing.T, testWatcher *watcher, tdata *test.Data, pathFi
gotBody []byte
)
- // Let the OS sync the file system before we create new file,
- // otherwise the modtime for fs.Root does not changes.
- time.Sleep(100 * time.Millisecond)
-
err = os.WriteFile(pathFile, expBody, 0600)
if err != nil {
t.Fatal(err)
}
+ testWatcher.watchDir.ForceRescan()
got = testWatcher.waitChanges()
@@ -124,14 +120,11 @@ func testAdocUpdate(t *testing.T, testWatcher *watcher, tdata *test.Data, pathFi
gotBody []byte
)
- // Let the OS sync the file system before we create new file,
- // otherwise the modtime for fs.Root does not changes.
- time.Sleep(100 * time.Millisecond)
-
err = os.WriteFile(pathFile, expBody, 0600)
if err != nil {
t.Fatal(err)
}
+ testWatcher.watchDir.ForceRescan()
got = testWatcher.waitChanges()
@@ -159,6 +152,7 @@ func testAdocDelete(t *testing.T, testWatcher *watcher, pathFile string) {
if err != nil {
t.Fatal(err)
}
+ testWatcher.watchDir.ForceRescan()
got = testWatcher.waitChanges()
@@ -178,14 +172,11 @@ func testMarkdownCreate(t *testing.T, testWatcher *watcher, tdata *test.Data, pa
gotBody []byte
)
- // Let the OS sync the file system before we create new file,
- // otherwise the modtime for fs.Root does not changes.
- time.Sleep(100 * time.Millisecond)
-
err = os.WriteFile(pathFileMarkdown, body, 0600)
if err != nil {
t.Fatal(err)
}
+ testWatcher.watchDir.ForceRescan()
got = testWatcher.waitChanges()
@@ -211,14 +202,11 @@ func testMarkdownUpdate(t *testing.T, testWatcher *watcher, tdata *test.Data, pa
gotBody []byte
)
- // Let the OS sync the file system before we create new file,
- // otherwise the modtime for fs.Root does not changes.
- time.Sleep(100 * time.Millisecond)
-
err = os.WriteFile(pathFileMarkdown, body, 0600)
if err != nil {
t.Fatal(err)
}
+ testWatcher.watchDir.ForceRescan()
got = testWatcher.waitChanges()
@@ -241,12 +229,11 @@ func testMarkdownDelete(t *testing.T, testWatcher *watcher, pathFileMarkdown str
gotIsExist bool
)
- time.Sleep(100 * time.Millisecond)
-
err = os.Remove(pathFileMarkdown)
if err != nil {
t.Fatal(err)
}
+ testWatcher.watchDir.ForceRescan()
got = testWatcher.waitChanges()
test.Assert(t, `md file updated`, pathFileMarkdown, got.path)