From e892c5cf4dfec51afa7b2499680000caac80f931 Mon Sep 17 00:00:00 2001 From: holger krekel Date: Mon, 15 Jul 2019 23:31:30 +0200 Subject: [PATCH] fix test for events --- python/src/deltachat/__init__.py | 3 +++ python/src/deltachat/account.py | 5 ++++- python/tests/test_lowlevel.py | 23 ++++++++++++----------- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/python/src/deltachat/__init__.py b/python/src/deltachat/__init__.py index 6ba1ae9c8..7c7d16963 100644 --- a/python/src/deltachat/__init__.py +++ b/python/src/deltachat/__init__.py @@ -44,6 +44,9 @@ def py_dc_callback(ctx, evt, data1, data2): try: ret = callback(ctx, evt_name, data1, data2) + if ret is None: + ret = 0 + assert isinstance(ret, int), repr(res) if event_sig_types & 4: return ffi.cast('uintptr_t', ret) elif event_sig_types & 8: diff --git a/python/src/deltachat/account.py b/python/src/deltachat/account.py index 6fb32b73e..0dbc7dcdd 100644 --- a/python/src/deltachat/account.py +++ b/python/src/deltachat/account.py @@ -45,6 +45,9 @@ class Account(object): self._configkeys = self.get_config("sys.config_keys").split() self._imex_completed = threading.Event() + def __del__(self): + lib.dc_close(self._dc_context) + def _check_config_key(self, name): if name not in self._configkeys: raise KeyError("{!r} not a valid config key, existing keys: {!r}".format( @@ -414,7 +417,7 @@ class EventLogger: raise ValueError("{}({!r},{!r})".format(*ev)) return ev - def get_matching(self, event_name_regex): + def get_matching(self, event_name_regex, check_error=True): self._log("-- waiting for event with regex: {} --".format(event_name_regex)) rex = re.compile("(?:{}).*".format(event_name_regex)) while 1: diff --git a/python/tests/test_lowlevel.py b/python/tests/test_lowlevel.py index 3020ea49c..484a61795 100644 --- a/python/tests/test_lowlevel.py +++ b/python/tests/test_lowlevel.py @@ -1,25 +1,26 @@ from __future__ import print_function import pytest from deltachat import capi, Account, const, set_context_callback +from deltachat.capi import ffi from deltachat.cutil import as_dc_charpointer -from queue import Queue +from deltachat.account import EventLogger def test_empty_context(): ctx = capi.lib.dc_context_new(capi.ffi.NULL, capi.ffi.NULL, capi.ffi.NULL) capi.lib.dc_close(ctx) -def test_set_context(): - ctx = capi.lib.dc_context_new(capi.ffi.NULL, capi.ffi.NULL, capi.ffi.NULL) - - q = Queue() - set_context_callback(ctx, lambda *args: q.put(args)) - - name = as_dc_charpointer("ein") - email = as_dc_charpointer("ein@kontakt.org") - contact_id = capi.lib.dc_create_contact(ctx, name, email) +def test_dc_close_events(): + ctx = capi.lib.dc_context_new(capi.lib.py_dc_callback, ffi.NULL, ffi.NULL) + evlog = EventLogger(ctx) + evlog.set_timeout(5) + set_context_callback(ctx, lambda ctx, evt_name, data1, data2: evlog(evt_name, data1, data2)) capi.lib.dc_close(ctx) - q.get(timeout=10) + # test that we get events from dc_close + print(evlog.get_matching("DC_EVENT_INFO", check_error=False)) + print(evlog.get_matching("DC_EVENT_INFO", check_error=False)) + print(evlog.get_matching("DC_EVENT_INFO", check_error=False)) + print(evlog.get_matching("DC_EVENT_INFO", check_error=False)) def test_wrong_db(tmpdir):