Fix event order problem

This commit is contained in:
R. Richard 2019-08-07 09:42:03 +02:00
parent e6d2777520
commit 1210a30c31

View File

@ -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
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 == path
return event
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()