From f5157392b627f862651c134d8a552aada0496535 Mon Sep 17 00:00:00 2001 From: holger krekel Date: Tue, 3 May 2022 12:56:24 +0200 Subject: [PATCH] some renaming and some docstrings --- python/src/deltachat/testplugin.py | 46 ++++++++++++++++++------------ python/tests/test_account.py | 18 ++++++------ python/tests/test_lowlevel.py | 14 ++++----- 3 files changed, 44 insertions(+), 34 deletions(-) diff --git a/python/src/deltachat/testplugin.py b/python/src/deltachat/testplugin.py index f889c9489..567dda027 100644 --- a/python/src/deltachat/testplugin.py +++ b/python/src/deltachat/testplugin.py @@ -139,6 +139,8 @@ def testprocess(request): class TestProcess: + """ A pytest session-scoped instance to help with managing "live" account configurations. + """ def __init__(self, pytestconfig): self.pytestconfig = pytestconfig @@ -211,7 +213,11 @@ def data(request): return Data() -class PendingConfigure: +class ACSetup: + """ accounts setup helper to deal with multiple configure-process + and io & imap initialization phases. From tests, use the higher level + public ACFactory methods instead of its private helper class. + """ CONFIGURING = "CONFIGURING" CONFIGURED = "CONFIGURED" IDLEREADY = "IDLEREADY" @@ -222,18 +228,20 @@ class PendingConfigure: self._imap_cleaned = set() self.init_time = init_time - def add_account(self, acc, reconfigure=False): + def start_configure(self, account, reconfigure=False): + """ add an account and start its configure process. """ class PendingTracker: @account_hookimpl def ac_configure_completed(this, success): - self._configured_events.put((acc, success)) + self._configured_events.put((account, success)) - acc.add_account_plugin(PendingTracker(), name="pending_tracker") - self._account2state[acc] = self.CONFIGURING - acc.configure(reconfigure=reconfigure) - print("started configure on pending", acc) + account.add_account_plugin(PendingTracker(), name="pending_tracker") + self._account2state[account] = self.CONFIGURING + account.configure(reconfigure=reconfigure) + print("started configure on pending", account) def wait_one_configured(self, account): + """ wait until this account has successfully configured. """ if self._account2state[account] == self.CONFIGURING: while 1: acc = self._pop_config_success() @@ -243,7 +251,11 @@ class PendingConfigure: acc._evtracker.consume_events() def bring_online(self): - """ Wait for all accounts to finish configuration. + """ Wait for all accounts to become ready to receive messages. + + This will initialize logging, start IO and the direct_imap attribute + for each account which either is CONFIGURED already or which is CONFIGURING + and successfully completing the configuration process. """ print("wait_all_configured finds accounts=", self._account2state) for acc, state in self._account2state.items(): @@ -280,6 +292,7 @@ class PendingConfigure: acc.add_account_plugin(logger, name=acc._logid) def init_direct_imap(self, acc): + """ idempotent function for initializing direct_imap.""" from deltachat.direct_imap import DirectImap if not hasattr(acc, "direct_imap"): acc.direct_imap = DirectImap(acc) @@ -304,13 +317,12 @@ class ACFactory: self.init_time = time.time() self.tmpdir = tmpdir self.pytestconfig = request.config - self.testprocess = testprocess self.data = data self._liveconfig_producer = testprocess.get_liveconfig_producer() self._finalizers = [] self._accounts = [] - self._pending_configure = PendingConfigure(self.init_time) + self._acsetup = ACSetup(self.init_time) self._preconfigured_keys = ["alice", "bob", "charlie", "dom", "elena", "fiona"] self.set_logging_default(False) @@ -338,7 +350,7 @@ class ACFactory: if "e2ee_enabled" not in configdict: configdict["e2ee_enabled"] = "1" - if self.testprocess.pytestconfig.getoption("--strict-tls"): + if self.pytestconfig.getoption("--strict-tls"): # Enable strict certificate checks for online accounts configdict["imap_certificate_checks"] = str(const.DC_CERTCK_STRICT) configdict["smtp_certificate_checks"] = str(const.DC_CERTCK_STRICT) @@ -400,7 +412,7 @@ class ACFactory: ) configdict.update(kwargs) ac = self.prepare_account_from_liveconfig(configdict) - self._pending_configure.add_account(ac) + self._acsetup.start_configure(ac) return ac def prepare_account_from_liveconfig(self, configdict): @@ -413,15 +425,13 @@ class ACFactory: self._preconfigure_key(ac, configdict["addr"]) return ac - def new_cloned_configuring_account(self, account): - return self.new_online_configuring_account(cloned_from=account) - - def wait_configured(self, acc): - self._pending_configure.wait_one_configured(acc) + def wait_configured(self, account): + """ Wait until the specified account has successfully completed configure. """ + self._acsetup.wait_one_configured(account) def bring_accounts_online(self): print("bringing accounts online") - self._pending_configure.bring_online() + self._acsetup.bring_online() print("all accounts online") def get_online_accounts(self, num): diff --git a/python/tests/test_account.py b/python/tests/test_account.py index 76108f850..85c2a6aea 100644 --- a/python/tests/test_account.py +++ b/python/tests/test_account.py @@ -726,7 +726,7 @@ class TestOnlineAccount: def test_one_account_send_bcc_setting(self, acfactory, lp): ac1 = acfactory.new_online_configuring_account() ac2 = acfactory.new_online_configuring_account() - ac1_clone = acfactory.new_cloned_configuring_account(ac1) + ac1_clone = acfactory.new_online_configuring_account(cloned_from=ac1) acfactory.bring_accounts_online() # test if sent messages are copied to it via BCC. @@ -1090,7 +1090,7 @@ class TestOnlineAccount: """Test that message marked as seen on one device is marked as seen on another.""" ac1 = acfactory.new_online_configuring_account() ac2 = acfactory.new_online_configuring_account() - ac1_clone = acfactory.new_cloned_configuring_account(ac1) + ac1_clone = acfactory.new_online_configuring_account(cloned_from=ac1) acfactory.bring_accounts_online() ac1.set_config("bcc_self", "1") @@ -1531,7 +1531,7 @@ class TestOnlineAccount: def test_no_old_msg_is_fresh(self, acfactory, lp): ac1 = acfactory.new_online_configuring_account() ac2 = acfactory.new_online_configuring_account() - ac1_clone = acfactory.new_cloned_configuring_account(ac1) + ac1_clone = acfactory.new_online_configuring_account(cloned_from=ac1) acfactory.bring_accounts_online() ac1.set_config("e2ee_enabled", "0") @@ -1895,7 +1895,7 @@ class TestOnlineAccount: # before ther setup message is send. DC does not read old messages # as of Jul2019 ac1 = acfactory.new_online_configuring_account() - ac2 = acfactory.new_cloned_configuring_account(ac1) + ac2 = acfactory.new_online_configuring_account(cloned_from=ac1) acfactory.bring_accounts_online() lp.sec("trigger ac setup message and return setupcode") @@ -1916,7 +1916,7 @@ class TestOnlineAccount: def test_ac_setup_message_twice(self, acfactory, lp): ac1 = acfactory.new_online_configuring_account() - ac2 = acfactory.new_cloned_configuring_account(ac1) + ac2 = acfactory.new_online_configuring_account(cloned_from=ac1) acfactory.bring_accounts_online() lp.sec("trigger ac setup message but ignore") @@ -2400,7 +2400,7 @@ class TestOnlineAccount: lp.sec("ac3 reinstalls DC and generates a new key") ac3.stop_io() acfactory.remove_preconfigured_keys() - ac4 = acfactory.new_cloned_configuring_account(ac3) + ac4 = acfactory.new_online_configuring_account(cloned_from=ac3) acfactory.wait_configured(ac4) # Create contacts to make sure incoming messages are not treated as contact requests chat41 = ac4.create_chat(ac1) @@ -2787,7 +2787,7 @@ class TestOnlineAccount: # would also find the "Sent" folder, but it would be too late: # The sentbox thread, started by `start_io()`, would have seen that there is no # ConfiguredSentboxFolder and do nothing. - acfactory._pending_configure.add_account(ac1, reconfigure=True) + acfactory._acsetup.start_configure(ac1, reconfigure=True) acfactory.bring_accounts_online() assert_folders_configured(ac1) @@ -2805,7 +2805,7 @@ class TestOnlineAccount: assert_folders_configured(ac1) lp.sec("create a cloned ac1 and fetch contact history during configure") - ac1_clone = acfactory.new_cloned_configuring_account(ac1) + ac1_clone = acfactory.new_online_configuring_account(cloned_from=ac1) ac1_clone.set_config("fetch_existing_msgs", "1") acfactory.wait_configured(ac1_clone) ac1_clone.start_io() @@ -2851,7 +2851,7 @@ class TestOnlineAccount: assert ac1.direct_imap.idle_wait_for_seen() lp.sec("Clone online account and let it fetch the existing messages") - ac1_clone = acfactory.new_cloned_configuring_account(ac1) + ac1_clone = acfactory.new_online_configuring_account(cloned_from=ac1) ac1_clone.set_config("fetch_existing_msgs", "1") acfactory.wait_configured(ac1_clone) diff --git a/python/tests/test_lowlevel.py b/python/tests/test_lowlevel.py index 1ae8ff188..7be054d01 100644 --- a/python/tests/test_lowlevel.py +++ b/python/tests/test_lowlevel.py @@ -6,16 +6,16 @@ from deltachat import register_global_plugin from deltachat.hookspec import global_hookimpl from deltachat.capi import ffi from deltachat.capi import lib -from deltachat.testplugin import PendingConfigure +from deltachat.testplugin import ACSetup # from deltachat.account import EventLogger -class TestPendingConfigure: +class TestACSetup: def test_basic_states(self, acfactory, monkeypatch): - pc = PendingConfigure(init_time=0.0) + pc = ACSetup(init_time=0.0) acc = acfactory.get_unconfigured_account() monkeypatch.setattr(acc, "configure", lambda **kwargs: None) - pc.add_account(acc) + pc.start_configure(acc) assert pc._account2state[acc] == pc.CONFIGURING pc._configured_events.put((acc, True)) monkeypatch.setattr(pc, "init_direct_imap", lambda *args, **kwargs: None) @@ -26,15 +26,15 @@ class TestPendingConfigure: assert pc._account2state[acc] == pc.IDLEREADY def test_two_accounts_one_waited_all_started(self, monkeypatch, acfactory): - pc = PendingConfigure(init_time=0.0) + pc = ACSetup(init_time=0.0) monkeypatch.setattr(pc, "init_direct_imap", lambda *args, **kwargs: None) monkeypatch.setattr(pc, "_onconfigure_start_io", lambda *args, **kwargs: None) ac1 = acfactory.get_unconfigured_account() monkeypatch.setattr(ac1, "configure", lambda **kwargs: None) - pc.add_account(ac1) + pc.start_configure(ac1) ac2 = acfactory.get_unconfigured_account() monkeypatch.setattr(ac2, "configure", lambda **kwargs: None) - pc.add_account(ac2) + pc.start_configure(ac2) assert pc._account2state[ac1] == pc.CONFIGURING assert pc._account2state[ac2] == pc.CONFIGURING pc._configured_events.put((ac1, True))