diff --git a/test/minimail.py b/test/minimail.py index 689b6f4..a6a5a77 100644 --- a/test/minimail.py +++ b/test/minimail.py @@ -6,15 +6,61 @@ # this file is under GNU General Public License 3.0 -import pEp from secrets import token_urlsafe +from itertools import compress +from functools import partial +from time import sleep + + +def unlock(inbox): + lockfile = inbox / "lock" + try: + lockfile.unlink() + except: + pass + + +class Lock: + def __init__(self, inbox): + self.inbox = inbox + + def __exit__(self, *exc): + unlock(self.inbox) + + def __enter__(self): + lockfile = self.inbox / "lock" + while lockfile.is_file(): + sleep(1) + lockfile.touch() def send(inbox, msg): - name = token_urlsafe(16) + ".eml" - with open(inbox / name, "wb") as f: - f.write(str(msg).encode()) + with Lock(inbox): + name = token_urlsafe(16) + ".eml" + with open(inbox / name, "wb") as f: + f.write(str(msg).encode()) + + +def newer(file1, file2): + if not file2.is_file(): + return False + elif not file1.is_file(): + return True + + stat1 = file1.stat() + stat2 = file2.stat() + return stat2.st_mtime > stat1.st_mtime + +def recv_all(inbox, marker): + with Lock(inbox): + r = [] + while not r: + for f in compress(inbox.glob("*.eml"), partial(newer, file1=marker)): + t = f.readall() + r.append(t) + if not r: + sleep(1) + marker.touch(exist_ok=True) + return r -def recv(inbox): - pass diff --git a/test/sync_test.py b/test/sync_test.py index a29efc2..a520b7e 100644 --- a/test/sync_test.py +++ b/test/sync_test.py @@ -60,7 +60,6 @@ EINTR = 4 if __name__ == "__main__": from optparse import OptionParser - from multiprocessing import Process optParser = OptionParser() optParser.add_option("-c", "--clean", action="store_true", dest="clean", @@ -68,11 +67,16 @@ if __name__ == "__main__": options, args = optParser.parse_args() if options.clean: + from minimail import unlock + rmrf("TestInbox") + unlock(pathlib.Path("TestInbox")) rmrf("Alice") rmrf("Barbara") else: + from multiprocessing import Process + os.makedirs("TestInbox", exist_ok=True) setup("Alice") setup("Barbara")