@@ -12,23 +12,20 @@ import (
1212
1313func TestWatchDebouncesDuplicates (t * testing.T ) {
1414 ctx , cancel := context .WithCancel (context .Background ())
15- w := & fsnotify.Watcher {
16- Events : make (chan fsnotify.Event ),
17- }
1815 events := make (chan fsnotify.Event , 2 )
1916 errors := make (chan error )
2017 watchPattern , err := regexp .Compile (".*" )
2118 if err != nil {
2219 t .Fatal (fmt .Errorf ("failed to compile watch pattern: %w" , err ))
2320 }
24- rw := NewRecursiveWatcher (ctx , w , watchPattern , events , errors )
21+ rw , err := Recursive (ctx , watchPattern , events , errors )
22+ if err != nil {
23+ t .Fatal (fmt .Errorf ("failed to create recursive watcher: %w" , err ))
24+ }
2525 go func () {
2626 rw .w .Events <- fsnotify.Event {Name : "test.templ" }
2727 rw .w .Events <- fsnotify.Event {Name : "test.templ" }
28- cancel ()
29- close (rw .w .Events )
3028 }()
31- rw .loop ()
3229 count := 0
3330 exp := time .After (300 * time .Millisecond )
3431 for {
@@ -39,6 +36,8 @@ func TestWatchDebouncesDuplicates(t *testing.T) {
3936 if count != 1 {
4037 t .Errorf ("expected 1 event, got %d" , count )
4138 }
39+ cancel ()
40+ rw .Close ()
4241 return
4342 }
4443 }
@@ -64,23 +63,20 @@ func TestWatchDoesNotDebounceDifferentEvents(t *testing.T) {
6463 }
6564 for _ , test := range tests {
6665 ctx , cancel := context .WithCancel (context .Background ())
67- w := & fsnotify.Watcher {
68- Events : make (chan fsnotify.Event ),
69- }
7066 events := make (chan fsnotify.Event , 2 )
7167 errors := make (chan error )
7268 watchPattern , err := regexp .Compile (".*" )
7369 if err != nil {
7470 t .Fatal (fmt .Errorf ("failed to compile watch pattern: %w" , err ))
7571 }
76- rw := NewRecursiveWatcher (ctx , w , watchPattern , events , errors )
72+ rw , err := Recursive (ctx , watchPattern , events , errors )
73+ if err != nil {
74+ t .Fatal (fmt .Errorf ("failed to create recursive watcher: %w" , err ))
75+ }
7776 go func () {
7877 rw .w .Events <- test .event1
7978 rw .w .Events <- test .event2
80- cancel ()
81- close (rw .w .Events )
8279 }()
83- rw .loop ()
8480 count := 0
8581 exp := time .After (300 * time .Millisecond )
8682 for {
@@ -91,6 +87,8 @@ func TestWatchDoesNotDebounceDifferentEvents(t *testing.T) {
9187 if count != 2 {
9288 t .Errorf ("expected 2 event, got %d" , count )
9389 }
90+ cancel ()
91+ rw .Close ()
9492 return
9593 }
9694 }
@@ -99,24 +97,21 @@ func TestWatchDoesNotDebounceDifferentEvents(t *testing.T) {
9997
10098func TestWatchDoesNotDebounceSeparateEvents (t * testing.T ) {
10199 ctx , cancel := context .WithCancel (context .Background ())
102- w := & fsnotify.Watcher {
103- Events : make (chan fsnotify.Event ),
104- }
105100 events := make (chan fsnotify.Event , 2 )
106101 errors := make (chan error )
107102 watchPattern , err := regexp .Compile (".*" )
108103 if err != nil {
109104 t .Fatal (fmt .Errorf ("failed to compile watch pattern: %w" , err ))
110105 }
111- rw := NewRecursiveWatcher (ctx , w , watchPattern , events , errors )
106+ rw , err := Recursive (ctx , watchPattern , events , errors )
107+ if err != nil {
108+ t .Fatal (fmt .Errorf ("failed to create recursive watcher: %w" , err ))
109+ }
112110 go func () {
113111 rw .w .Events <- fsnotify.Event {Name : "test.templ" }
114112 <- time .After (200 * time .Millisecond )
115113 rw .w .Events <- fsnotify.Event {Name : "test.templ" }
116- cancel ()
117- close (rw .w .Events )
118114 }()
119- rw .loop ()
120115 count := 0
121116 exp := time .After (500 * time .Millisecond )
122117 for {
@@ -127,6 +122,8 @@ func TestWatchDoesNotDebounceSeparateEvents(t *testing.T) {
127122 if count != 2 {
128123 t .Errorf ("expected 2 event, got %d" , count )
129124 }
125+ cancel ()
126+ rw .Close ()
130127 return
131128 }
132129 }
0 commit comments