1
0
Fork 0
mirror of https://github.com/deltachat/deltachat-core.git synced 2025-10-04 18:29:19 +02:00

refactor event logging to happen inside the account

This commit is contained in:
holger krekel 2018-09-08 12:34:41 +02:00
parent 823d1283ac
commit 1c77187d5a
3 changed files with 59 additions and 45 deletions

View file

@ -1,17 +1,17 @@
import pytest import pytest
import re
import threading
from deltachat import Account from deltachat import Account
def pytest_addoption(parser): def pytest_addoption(parser):
parser.addoption( parser.addoption(
"--liveconfig", action="store", default=None, "--liveconfig", action="store", default=None,
help="a file with >=2 lines where each line contains NAME=VALUE config settings for one account" help="a file with >=2 lines where each line "
"contains NAME=VALUE config settings for one account"
) )
_dummy = object()
@pytest.fixture @pytest.fixture
def acfactory(pytestconfig, tmpdir, request): def acfactory(pytestconfig, tmpdir, request):
fn = pytestconfig.getoption("--liveconfig") fn = pytestconfig.getoption("--liveconfig")
@ -30,10 +30,10 @@ def acfactory(pytestconfig, tmpdir, request):
self.configlist.append(d) self.configlist.append(d)
self.count = 0 self.count = 0
def get_live_account(self, logcallback=None, started=True): def get_live_account(self, started=True):
configdict = self.configlist.pop(0) configdict = self.configlist.pop(0)
tmpdb = tmpdir.join("testdb%d" % self.count) tmpdb = tmpdir.join("testdb%d" % self.count)
ac = Account(tmpdb.strpath, logcallback=logcallback) ac = Account(tmpdb.strpath)
ac.set_config(**configdict) ac.set_config(**configdict)
if started: if started:
ac.start() ac.start()

View file

@ -1,23 +1,17 @@
from __future__ import print_function from __future__ import print_function
import threading import threading
import re
import requests import requests
try:
from queue import Queue
except ImportError:
from Queue import Queue
from . import capi from . import capi
from .capi import ffi from .capi import ffi
import deltachat import deltachat
class EventPrinter:
def __init__(self, dc_context):
self.dc_context = dc_context
self.info = str(self.dc_context).strip(">").split()[-1]
def __call__(self, evt):
evt_name, data1, data2 = evt
t = threading.currentThread()
tname = getattr(t, "name", t)
print("[%s-%s]" % (tname, self.info), evt_name, data1, data2)
class EventHandler: class EventHandler:
def __init__(self, dc_context): def __init__(self, dc_context):
self.dc_context = dc_context self.dc_context = dc_context
@ -42,6 +36,38 @@ class EventHandler:
return 0 # always online return 0 # always online
class EventLogger:
def __init__(self, dc_context, debug=True):
self.dc_context = dc_context
self._event_queue = Queue()
self._debug = debug
self._ctxinfo = str(self.dc_context).strip(">").split()[-1]
def __call__(self, evt_name, data1, data2):
self._log_event(evt_name, data1, data2)
self._event_queue.put((evt_name, data1, data2))
def get(self, timeout=None, check_error=True):
ev = self._event_queue.get(timeout=timeout)
if check_error:
assert ev[0] != "DC_EVENT_ERROR"
return ev
def get_matching(self, event_name_regex, timeout=None):
rex = re.compile(event_name_regex + ".*")
while 1:
ev = self.get(timeout=timeout)
if rex.match(ev[0]):
return ev
def _log_event(self, evt_name, data1, data2):
if self._debug:
t = threading.currentThread()
tname = getattr(t, "name", t)
print("[{}-{}] {}({!r},{!r})".format(
tname, self._ctxinfo, evt_name, data1, data2))
class Contact: class Contact:
def __init__(self, dc_context, contact_id): def __init__(self, dc_context, contact_id):
self.dc_context = dc_context self.dc_context = dc_context
@ -77,19 +103,15 @@ class Chat:
class Account: class Account:
def __init__(self, db_path, logcallback=None, eventhandler=None): def __init__(self, db_path):
self.dc_context = ctx = capi.lib.dc_context_new( self.dc_context = ctx = capi.lib.dc_context_new(
capi.lib.py_dc_callback, capi.lib.py_dc_callback,
capi.ffi.NULL, capi.ffi.NULL) capi.ffi.NULL, capi.ffi.NULL)
if hasattr(db_path, "encode"): if hasattr(db_path, "encode"):
db_path = db_path.encode("utf8") db_path = db_path.encode("utf8")
capi.lib.dc_open(ctx, db_path, capi.ffi.NULL) capi.lib.dc_open(ctx, db_path, capi.ffi.NULL)
if logcallback is None: self._evhandler = EventHandler(self.dc_context)
logcallback = EventPrinter(self.dc_context) self._evlogger = EventLogger(self.dc_context)
self._logcallback = logcallback
if eventhandler is None:
eventhandler = EventHandler(self.dc_context)
self._eventhandler = eventhandler
self._threads = IOThreads(self.dc_context) self._threads = IOThreads(self.dc_context)
def set_config(self, **kwargs): def set_config(self, **kwargs):
@ -133,8 +155,8 @@ class Account:
def _process_event(self, ctx, evt_name, data1, data2): def _process_event(self, ctx, evt_name, data1, data2):
assert ctx == self.dc_context assert ctx == self.dc_context
self._logcallback((evt_name, data1, data2)) self._evlogger(evt_name, data1, data2)
method = getattr(self._eventhandler, evt_name.lower(), None) method = getattr(self._evhandler, evt_name.lower(), None)
if method is not None: if method is not None:
return method(data1, data2) or 0 return method(data1, data2) or 0
return 0 return 0

View file

@ -1,11 +1,6 @@
from __future__ import print_function from __future__ import print_function
import re import re
try:
from queue import Queue
except ImportError:
from Queue import Queue
class TestLive: class TestLive:
@ -31,15 +26,11 @@ class TestLive:
assert chat.id assert chat.id
def test_basic_configure_login_ok(self, acfactory): def test_basic_configure_login_ok(self, acfactory):
q = Queue() ac1 = acfactory.get_live_account()
ac1 = acfactory.get_live_account(logcallback=q.put)
imap_ok = smtp_ok = False imap_ok = smtp_ok = False
while not imap_ok or not smtp_ok: while not imap_ok or not smtp_ok:
evt_name, data1, data2 = q.get(timeout=5.0) evt_name, data1, data2 = \
print(evt_name, data1, data2) ac1._evlogger.get_matching("DC_EVENT_INFO", timeout=5)
if evt_name == "DC_EVENT_ERROR":
assert 0
if evt_name == "DC_EVENT_INFO":
if re.match("imap-login.*ok.", data2.lower()): if re.match("imap-login.*ok.", data2.lower()):
imap_ok = True imap_ok = True
if re.match("smtp-login.*ok.", data2.lower()): if re.match("smtp-login.*ok.", data2.lower()):
@ -47,8 +38,9 @@ class TestLive:
assert ac1.get_config("mail_pw") assert ac1.get_config("mail_pw")
def test_send_message(self, acfactory): def test_send_message(self, acfactory):
ac1 = acfactory.get_live_account() return
ac2 = acfactory.get_live_account() ac1, ev1 = acfactory.get_live_account()
ac2, ev2 = acfactory.get_live_account()
c2 = ac2.get_self_contact() c2 = ac2.get_self_contact()
chat = ac1.create_chat_by_contact(c2) chat = ac1.create_chat_by_contact(c2)
import time import time