@@ -66,6 +66,52 @@ void main() {
6666 },
6767 );
6868
69+ // Regression test for https://github.com/dart-lang/tools/issues/2152:
70+ // watcher can throws if a directory is created then quickly deleted.
71+ group ('Transient directory' , () {
72+ late StreamSubscription <Object > subscription;
73+ late Directory temp;
74+ late Watcher watcher;
75+ late int errorsSeen;
76+
77+ setUp (() async {
78+ temp = Directory .systemTemp.createTempSync ();
79+ watcher = DirectoryWatcher (temp.path);
80+ errorsSeen = 0 ;
81+ subscription = watcher.events.listen (
82+ (e) {},
83+ onError: (Object e, _) {
84+ print ('Event stream error: $e ' );
85+ ++ errorsSeen;
86+ },
87+ );
88+ await watcher.ready;
89+ });
90+
91+ tearDown (() {
92+ subscription.cancel ();
93+ });
94+
95+ test ('does not break watching' , () async {
96+ // Iterate creating 10 directories and deleting 1-10 of them. This means
97+ // the directories will exist for different lengths of times, exploring
98+ // possible race conditions in directory handling.
99+ for (var i = 0 ; i != 50 ; ++ i) {
100+ for (var j = 0 ; j != 10 ; ++ j) {
101+ File ('${temp .path }\\ $j \\ file' ).createSync (recursive: true );
102+ }
103+ await Future <void >.delayed (const Duration (milliseconds: 1 ));
104+ for (var j = 0 ; j != i % 10 + 1 ; ++ j) {
105+ final d = Directory ('${temp .path }\\ $j ' );
106+ d.deleteSync (recursive: true );
107+ }
108+ await Future <void >.delayed (const Duration (milliseconds: 1 ));
109+ }
110+
111+ expect (errorsSeen, 0 );
112+ });
113+ });
114+
69115 // The Windows native watcher has a buffer that gets exhausted if events are
70116 // not handled quickly enough. Then, it throws an error and stops watching.
71117 // The exhaustion is reliably triggered if enough events arrive during a sync
0 commit comments