From e7dd74e4b13f60a09339288c30b347b4b37c1fe8 Mon Sep 17 00:00:00 2001 From: holger krekel Date: Tue, 9 Jun 2020 23:04:54 +0200 Subject: [PATCH] simplify configure() and don't keep state on the account in non-test mode --- python/src/deltachat/__init__.py | 4 +- python/src/deltachat/account.py | 21 ++++------ python/src/deltachat/testplugin.py | 28 +++++++------ python/src/deltachat/tracker.py | 1 + python/tests/test_account.py | 63 +++++++++--------------------- 5 files changed, 45 insertions(+), 72 deletions(-) diff --git a/python/src/deltachat/__init__.py b/python/src/deltachat/__init__.py index e4f26d4dd..6240dfed1 100644 --- a/python/src/deltachat/__init__.py +++ b/python/src/deltachat/__init__.py @@ -77,8 +77,8 @@ def run_cmdline(argv=None, account_plugins=None): ac.set_config("mvbox_move", "0") ac.set_config("mvbox_watch", "0") ac.set_config("sentbox_watch", "0") - ac.configure() - ac.wait_configure_finish() + configtracker = ac.configure() + configtracker.wait_finish() # start IO threads and configure if neccessary ac.start_io() diff --git a/python/src/deltachat/account.py b/python/src/deltachat/account.py index 44a104534..7b4ac457b 100644 --- a/python/src/deltachat/account.py +++ b/python/src/deltachat/account.py @@ -561,29 +561,24 @@ class Account(object): :raises MissingCredentials: if `addr` and `mail_pw` values are not set. :raises ConfigureFailed: if the account could not be configured. - :returns: None (account is configured and with io-scheduling running) + :returns: None """ if not self.is_configured(): raise ValueError("account not configured, cannot start io") lib.dc_start_io(self._dc_context) def configure(self): + """ Start configuration process and return a Configtracker instance + on which you can block with wait_finish() to get a True/False success + value for the configuration process. + """ assert not self.is_configured() - assert not hasattr(self, "_configtracker") if not self.get_config("addr") or not self.get_config("mail_pw"): raise MissingCredentials("addr or mail_pwd not set in config") - if hasattr(self, "_configtracker"): - self.remove_account_plugin(self._configtracker) - self._configtracker = ConfigureTracker(self) - self.add_account_plugin(self._configtracker) + configtracker = ConfigureTracker(self) + self.add_account_plugin(configtracker) lib.dc_configure(self._dc_context) - - def wait_configure_finish(self): - try: - self._configtracker.wait_finish() - finally: - self.remove_account_plugin(self._configtracker) - del self._configtracker + return configtracker def is_started(self): return self._event_thread.is_alive() and bool(lib.dc_is_io_running(self._dc_context)) diff --git a/python/src/deltachat/testplugin.py b/python/src/deltachat/testplugin.py index a8b4386c1..32df32beb 100644 --- a/python/src/deltachat/testplugin.py +++ b/python/src/deltachat/testplugin.py @@ -303,33 +303,25 @@ def acfactory(pytestconfig, tmpdir, request, session_liveconfig, data): configdict["mvbox_move"] = str(int(move)) configdict["sentbox_watch"] = str(int(sentbox)) ac.update_config(configdict) - ac.configure() + ac._configtracker = ac.configure() return ac def get_one_online_account(self, pre_generated_key=True, mvbox=False, move=False): ac1 = self.get_online_configuring_account( pre_generated_key=pre_generated_key, mvbox=mvbox, move=move) - ac1.wait_configure_finish() - ac1.start_io() + self.wait_configure_and_start_io() return ac1 def get_two_online_accounts(self, move=False, quiet=False): ac1 = self.get_online_configuring_account(move=move, quiet=quiet) ac2 = self.get_online_configuring_account(quiet=quiet) - ac1.wait_configure_finish() - ac1.start_io() - ac2.wait_configure_finish() - ac2.start_io() + self.wait_configure_and_start_io() return ac1, ac2 def get_many_online_accounts(self, num, move=True): accounts = [self.get_online_configuring_account(move=move, quiet=True) for i in range(num)] - for acc in accounts: - acc._configtracker.wait_finish() - acc.start_io() - print("{}: {} account was successfully setup".format( - acc.get_config("displayname"), acc.get_config("addr"))) + self.wait_configure_and_start_io() for acc in accounts: acc.add_account_plugin(FFIEventLogger(acc)) return accounts @@ -349,9 +341,19 @@ def acfactory(pytestconfig, tmpdir, request, session_liveconfig, data): mvbox_move=account.get_config("mvbox_move"), sentbox_watch=account.get_config("sentbox_watch"), )) - ac.configure() + ac._configtracker = ac.configure() return ac + def wait_configure_and_start_io(self): + for acc in self._accounts: + if hasattr(acc, "_configtracker"): + acc._configtracker.wait_finish() + del acc._configtracker + if acc.is_configured() and not acc.is_started(): + acc.start_io() + print("{}: {} account was successfully setup".format( + acc.get_config("displayname"), acc.get_config("addr"))) + def run_bot_process(self, module, ffi=True): fn = module.__file__ diff --git a/python/src/deltachat/tracker.py b/python/src/deltachat/tracker.py index 25f3c0da7..1d35c3345 100644 --- a/python/src/deltachat/tracker.py +++ b/python/src/deltachat/tracker.py @@ -64,6 +64,7 @@ class ConfigureTracker: if success: self._gm.hook.dc_account_extra_configure(account=self.account) self._configure_events.put(success) + self.account.remove_account_plugin(self) def wait_smtp_connected(self): """ wait until smtp is configured. """ diff --git a/python/tests/test_account.py b/python/tests/test_account.py index adb5de36b..36154838a 100644 --- a/python/tests/test_account.py +++ b/python/tests/test_account.py @@ -544,10 +544,7 @@ class TestOnlineAccount: ) # rsa key gen can be slow especially on CI, adjust timeout ac1._evtracker.set_timeout(120) - ac1.wait_configure_finish() - ac1.start_io() - ac2.wait_configure_finish() - ac2.start_io() + acfactory.wait_configure_and_start_io() chat = acfactory.get_accepted_chat(ac1, ac2) lp.sec("ac1: send unencrypted message to ac2") @@ -579,7 +576,7 @@ class TestOnlineAccount: ac1._configtracker.wait_progress() ac1.stop_ongoing() try: - ac1.wait_configure_finish() + ac1._configtracker.wait_finish() except Exception: pass @@ -602,12 +599,7 @@ class TestOnlineAccount: # are copied to it via BCC. ac1_clone = acfactory.clone_online_account(ac1) - ac1.wait_configure_finish() - ac1.start_io() - ac2.wait_configure_finish() - ac2.start_io() - ac1_clone.wait_configure_finish() - ac1_clone.start_io() + acfactory.wait_configure_and_start_io() chat = acfactory.get_accepted_chat(ac1, ac2) @@ -712,13 +704,8 @@ class TestOnlineAccount: lp.sec("ac2: start without mvbox/sentbox threads") ac2 = acfactory.get_online_configuring_account() - lp.sec("ac2: waiting for configuration") - ac2.wait_configure_finish() - ac2.start_io() - - lp.sec("ac1: waiting for configuration") - ac1.wait_configure_finish() - ac1.start_io() + lp.sec("ac2 and ac1: waiting for configuration") + acfactory.wait_configure_and_start_io() lp.sec("ac1: send message and wait for ac2 to receive it") acfactory.get_accepted_chat(ac1, ac2).send_text("message1") @@ -727,10 +714,7 @@ class TestOnlineAccount: def test_move_works(self, acfactory): ac1 = acfactory.get_online_configuring_account() ac2 = acfactory.get_online_configuring_account(mvbox=True, move=True) - ac2.wait_configure_finish() - ac2.start_io() - ac1.wait_configure_finish() - ac1.start_io() + acfactory.wait_configure_and_start_io() chat = acfactory.get_accepted_chat(ac1, ac2) chat.send_text("message1") ev = ac2._evtracker.get_matching("DC_EVENT_INCOMING_MSG") @@ -741,10 +725,7 @@ class TestOnlineAccount: ac1 = acfactory.get_online_configuring_account(mvbox=True, move=True) ac1.set_config("bcc_self", "1") ac2 = acfactory.get_online_configuring_account() - ac2.wait_configure_finish() - ac2.start_io() - ac1.wait_configure_finish() - ac1.start_io() + acfactory.wait_configure_and_start_io() chat = acfactory.get_accepted_chat(ac1, ac2) chat.send_text("message1") @@ -1184,10 +1165,7 @@ class TestOnlineAccount: # as of Jul2019 ac1 = acfactory.get_online_configuring_account() ac2 = acfactory.clone_online_account(ac1) - ac2.wait_configure_finish() - ac2.start_io() - ac1.wait_configure_finish() - ac1.start_io() + acfactory.wait_configure_and_start_io() lp.sec("trigger ac setup message and return setupcode") assert ac1.get_info()["fingerprint"] != ac2.get_info()["fingerprint"] @@ -1210,10 +1188,7 @@ class TestOnlineAccount: ac1 = acfactory.get_online_configuring_account() ac2 = acfactory.clone_online_account(ac1) ac2._evtracker.set_timeout(30) - ac2.wait_configure_finish() - ac2.start_io() - ac1.wait_configure_finish() - ac1.start_io() + acfactory.wait_configure_and_start_io() lp.sec("trigger ac setup message but ignore") assert ac1.get_info()["fingerprint"] != ac2.get_info()["fingerprint"] @@ -1513,7 +1488,7 @@ class TestOnlineAccount: lp.sec("ac3 reinstalls DC and generates a new key") ac3.stop_io() ac4 = acfactory.clone_online_account(ac3, pre_generated_key=False) - ac4.wait_configure_finish() + ac4._configtracker.wait_finish() # Create contacts to make sure incoming messages are not treated as contact requests chat41 = ac4.create_chat(ac1) chat42 = ac4.create_chat(ac2) @@ -1686,26 +1661,26 @@ class TestOnlineConfigureFails: def test_invalid_password(self, acfactory): ac1, configdict = acfactory.get_online_config() ac1.update_config(dict(addr=configdict["addr"], mail_pw="123")) - ac1.configure() - ac1._configtracker.wait_progress(500) - ac1._configtracker.wait_progress(0) + configtracker = ac1.configure() + configtracker.wait_progress(500) + configtracker.wait_progress(0) ev = ac1._evtracker.get_matching("DC_EVENT_ERROR_NETWORK") assert "cannot login" in ev.data2.lower() def test_invalid_user(self, acfactory): ac1, configdict = acfactory.get_online_config() ac1.update_config(dict(addr="x" + configdict["addr"], mail_pw=configdict["mail_pw"])) - ac1.configure() - ac1._configtracker.wait_progress(500) - ac1._configtracker.wait_progress(0) + configtracker = ac1.configure() + configtracker.wait_progress(500) + configtracker.wait_progress(0) ev = ac1._evtracker.get_matching("DC_EVENT_ERROR_NETWORK") assert "cannot login" in ev.data2.lower() def test_invalid_domain(self, acfactory): ac1, configdict = acfactory.get_online_config() ac1.update_config((dict(addr=configdict["addr"] + "x", mail_pw=configdict["mail_pw"]))) - ac1.configure() - ac1._configtracker.wait_progress(500) - ac1._configtracker.wait_progress(0) + configtracker = ac1.configure() + configtracker.wait_progress(500) + configtracker.wait_progress(0) ev = ac1._evtracker.get_matching("DC_EVENT_ERROR_NETWORK") assert "could not connect" in ev.data2.lower()