From 1210a30c31c28d009ebfe8d34a1af535ba1b7868 Mon Sep 17 00:00:00 2001 From: "R. Richard" Date: Wed, 7 Aug 2019 09:42:03 +0200 Subject: [PATCH] Fix event order problem --- tfw/internals/inotify/test_inotify.py | 40 +++++++++++++++------------ 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/tfw/internals/inotify/test_inotify.py b/tfw/internals/inotify/test_inotify.py index d132706..3fabf6d 100644 --- a/tfw/internals/inotify/test_inotify.py +++ b/tfw/internals/inotify/test_inotify.py @@ -55,12 +55,14 @@ class InotifyContext: def join(self, path): return join(self.workdir, path) - def check_event(self, event_type, path): - self.missing_events += 1 - event = self.event_to_queue[event_type].get(timeout=0.1) - assert isinstance(event, event_type) - assert event.src_path == path - return event + def check_event(self, event_type, paths): + if isinstance(paths, str): + paths = [paths] + self.missing_events += len(paths) + for _ in range(len(paths)): + event = self.event_to_queue[event_type].get(timeout=0.1) + assert isinstance(event, event_type) + assert event.src_path in paths def check_empty(self, event_type): with pytest.raises(Empty): @@ -69,7 +71,7 @@ class InotifyContext: def check_any(self): attrs = self.observer.__dict__.values() total = sum([q.qsize() for q in attrs if isinstance(q, Queue)]) - return total+self.missing_events == len(self.observer.any_list) + return total + self.missing_events == len(self.observer.any_list) class InotifyTestObserver(InotifyObserver): @@ -100,7 +102,7 @@ def generate_name(): def context(): with TemporaryDirectory() as workdir: subdir = join(workdir, generate_name()) - subfile = join(subdir, generate_name()+'.txt') + subfile = join(subdir, generate_name() + '.txt') mkdir(subdir) Path(subfile).touch() monitor = InotifyTestObserver(workdir, recursive=True) @@ -124,16 +126,21 @@ def test_modify(context): context.missing_events += 1 except Empty: break - rename(context.subfile, context.subfile+'_new') + rename(context.subfile, context.subfile + '_new') context.check_event(InotifyDirModifiedEvent, context.subdir) assert context.check_any() -def test_move(context): - rename(context.subdir, context.subdir+'_new') - context.check_event(InotifyDirMovedEvent, context.subdir) +def test_file_move(context): + rename(context.subfile, context.subfile + '_new') context.check_event(InotifyFileMovedEvent, context.subfile) assert context.check_any() +def test_folder_move(context): + remove(context.subfile) + rename(context.subdir, context.subdir + '_new') + context.check_event(InotifyDirMovedEvent, context.subdir) + assert context.check_any() + def test_delete(context): rmtree(context.subdir) context.check_event(InotifyFileDeletedEvent, context.subfile) @@ -171,9 +178,8 @@ def test_exclude(context): context.observer.exclude = None def test_stress(context): - newfile = [] - for i in range(1024): - newfile.append(context.create_random_file(context.subdir, '.txt')) - for i in range(1024): - context.check_event(InotifyFileCreatedEvent, newfile[i]) + newfiles = [] + for _ in range(1024): + newfiles.append(context.create_random_file(context.subdir, '.txt')) + context.check_event(InotifyFileCreatedEvent, newfiles) assert context.check_any()