Browse Source

Better multi-process test. Still PEP_MESSAGE_CONSUMED needs to be supported

PYADPT-55
Edouard Tisserant 9 years ago
parent
commit
46175a3d52
  1. 13
      test/mp_sync_test.py
  2. 125
      test/multipEp.py

13
test/mp_sync_test.py

@ -1,5 +1,5 @@
""" """
test for simplest keysync scenario : two device setting up sam account test for simplest keysync scenario : two device setting up same account
Launch it with something like : Launch it with something like :
DYLD_LIBRARY_PATH=/Users/ed/lib/ PYTHONPATH=`pwd`/../build/lib.macosx-10.11-x86_64-3.4 python3.4 sync_test.py DYLD_LIBRARY_PATH=/Users/ed/lib/ PYTHONPATH=`pwd`/../build/lib.macosx-10.11-x86_64-3.4 python3.4 sync_test.py
@ -8,11 +8,6 @@ DYLD_LIBRARY_PATH=/Users/ed/lib/ PYTHONPATH=`pwd`/../build/lib.macosx-10.11-x86_
import multipEp as mp import multipEp as mp
def create_account(address, name):
i = mp.pEp.Identity(address, name)
mp.pEp.myself(i)
mp.own_addresses.append(address)
# unused # unused
def send_message(from_address, to_address): def send_message(from_address, to_address):
m = mp.pEp.outgoing_message(Identity(from_address, from_address)) m = mp.pEp.outgoing_message(Identity(from_address, from_address))
@ -24,8 +19,10 @@ def send_message(from_address, to_address):
scenario0 = [ scenario0 = [
#("instance name", ["func name", [args], {kwargs}]), #("instance name", ["func name", [args], {kwargs}]),
("A", [create_account, ["some.one@some.where", "Some One"]]), ("A", [mp.create_account, ["some.one@some.where", "Some One"]]),
("B", [create_account, ["some.one@some.where", "Some One"]]) ("B", [mp.create_account, ["some.one@some.where", "Some One"]]),
("A", [None, None]),
("B", [None, None])
] ]
if __name__ == "__main__": if __name__ == "__main__":

125
test/multipEp.py

@ -8,27 +8,48 @@ from collections import OrderedDict
pEp = None pEp = None
handler = None handler = None
own_addresses = [] own_addresses = []
sent_messages = []
def pEp_instance_run(iname, conn): def create_account(address, name):
global pEp, handler, sent_messages global own_addresses
i = pEp.Identity(address, name)
pEp.myself(i)
own_addresses.append(address)
def header(blah=None):
if blah is None:
return "-" * 80
else:
return ("-" * (39 - int(len(blah)/2)) +
" " + blah + " " +
"-" * (39 - len(blah) + int(len(blah)/2)))
pEp = importlib.import_module("pEp")
hdr = "-" * (39 - int(len(iname)/2)) + " " + iname + " " + "-" * (39 - len(iname) + int(len(iname)/2)) def pEp_instance_run(iname, conn, msgs_folders):
global pEp, handler, own_addresses
pEp = importlib.import_module("pEp")
class Handler(pEp.SyncMixIn): class Handler(pEp.SyncMixIn):
def messageToSend(self, msg): def messageToSend(self, msg):
msgstr = str(msg) print(header("SYNC MESSAGE from instance"+iname))
print(hdr) print("from :", msg.from_)
print(msgstr.replace("\r", "")) print("to :", msg.to)
print("-" * 80) print("short :", msg.shortmsg)
sent_messages.append(msgstr) print("long :", (msg.longmsg[:250]+" [...]"
if len(msg.longmsg)>250
else msg.longmsg))
print(msg.attachments)
print(header())
for rcpt in msg.to + msg.cc + msg.bcc:
# list inside dict from MP manager are not mutable, apparently.
msgs = msgs_folders.get(rcpt.address,[])
msgs.append(str(msg))
msgs_folders[rcpt.address] = msgs
def showHandshake(self, me, partner): def showHandshake(self, me, partner):
print(hdr) print(header("HANDSHAKE from instance"+iname))
print("handshake needed between " + repr(me) + " and " + repr(partner)) print("handshake needed between " + repr(me) + " and " + repr(partner))
print("-" * 80) print(header())
# TODO: accept or reject # TODO: accept or reject
handler = Handler() handler = Handler()
@ -38,56 +59,60 @@ def pEp_instance_run(iname, conn):
if order is None: if order is None:
break break
command, new_msgs = order func = order[0]
if new_msgs is not None: print(header("DECRYPT messages for instance "+iname))
for msgstr in new_msgs: # decrypt every non-consumed message for all instance accounts
for own_address in own_addresses:
msgs_for_me = msgs_folders[own_address]
for idx, msgstr in enumerate(msgs_for_me):
msg = pEp.incoming_message(msgstr) msg = pEp.incoming_message(msgstr)
for to in msg.to: decrypt_result = msg.decrypt()
# check if mail is for an account owned by that instance # TODO get status instead of an exception when consumed...
# checking if to.user_id == pEp.PEP_OWN_USERID #if decrypt_result == 0xff02: #PEP_MESSAGE_CONSUMED
# could lead to false positive depending on what pEpEngine # msgs_for_me.pop(idx)
# consider to be its own identity print(header())
if to.address in own_addresses:
decrypt_result = msg.decrypt()
break
res = None res = None
if command is not None: if func is not None:
func = command[0] print(header("Instance " + iname + " : function " + func.__name__))
args, kwargs = command[1:] + [[], {}][len(command) - 1:] args, kwargs = order[1:] + [[], {}][len(order) - 1:]
print(func, args, kwargs) print("args :", args)
print("kwargs :", kwargs)
res = func(*args,**kwargs) res = func(*args,**kwargs)
print(func, args, kwargs, " -> ", res) print(" -> ", res)
print(header())
conn.send([res, sent_messages]) conn.send(res)
sent_messages = []
def pEp_instance_main(iname, conn): def pEp_instance_main(iname, conn, msgs_folders):
# run with a dispensable $HOME to get fresh DB and PGP keyrings # run with a dispensable $HOME to get fresh DB and PGP keyrings
with tempfile.TemporaryDirectory() as tmpdirname: with tempfile.TemporaryDirectory() as tmpdirname:
print(iname + " instance runs into " + tmpdirname) print("Instance " + iname + " runs into " + tmpdirname)
os.environ['HOME'] = tmpdirname os.environ['HOME'] = tmpdirname
pEp_instance_run(iname, conn) pEp_instance_run(iname, conn, msgs_folders)
print(iname + " exiting") print(iname + " exiting")
def run_scenario(scenario): def run_scenario(scenario):
instances = OrderedDict() instances = OrderedDict()
for iname, order in scenario: with multiprocessing.Manager() as manager:
if iname not in instances: msgs_folders = manager.dict()
to_inst_msg = [] for iname, order in scenario:
conn, child_conn = multiprocessing.Pipe() if iname not in instances:
proc = multiprocessing.Process(target=pEp_instance_main, args=(iname,child_conn)) conn, child_conn = multiprocessing.Pipe()
proc.start() proc = multiprocessing.Process(
instances[iname] = (proc, conn, to_inst_msg) target=pEp_instance_main,
else: args=(iname,child_conn,msgs_folders))
proc, conn, to_inst_msg = instances[iname] proc.start()
instances[iname] = (proc, conn)
conn.send([order, to_inst_msg]) else:
res, from_inst_msgs = conn.recv() proc, conn = instances[iname]
for iname, (proc, conn, to_inst_msg) in instances.items(): conn.send(order)
# tell process to terminate res = conn.recv()
conn.send(None)
proc.join() for iname, (proc, conn) in instances.items():
# tell process to terminate
conn.send(None)
proc.join()

Loading…
Cancel
Save