some renaming and some docstrings

This commit is contained in:
holger krekel
2022-05-03 12:56:24 +02:00
parent f631ec3a7c
commit f5157392b6
3 changed files with 44 additions and 34 deletions

View File

@@ -139,6 +139,8 @@ def testprocess(request):
class TestProcess: class TestProcess:
""" A pytest session-scoped instance to help with managing "live" account configurations.
"""
def __init__(self, pytestconfig): def __init__(self, pytestconfig):
self.pytestconfig = pytestconfig self.pytestconfig = pytestconfig
@@ -211,7 +213,11 @@ def data(request):
return Data() 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" CONFIGURING = "CONFIGURING"
CONFIGURED = "CONFIGURED" CONFIGURED = "CONFIGURED"
IDLEREADY = "IDLEREADY" IDLEREADY = "IDLEREADY"
@@ -222,18 +228,20 @@ class PendingConfigure:
self._imap_cleaned = set() self._imap_cleaned = set()
self.init_time = init_time 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: class PendingTracker:
@account_hookimpl @account_hookimpl
def ac_configure_completed(this, success): 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") account.add_account_plugin(PendingTracker(), name="pending_tracker")
self._account2state[acc] = self.CONFIGURING self._account2state[account] = self.CONFIGURING
acc.configure(reconfigure=reconfigure) account.configure(reconfigure=reconfigure)
print("started configure on pending", acc) print("started configure on pending", account)
def wait_one_configured(self, account): def wait_one_configured(self, account):
""" wait until this account has successfully configured. """
if self._account2state[account] == self.CONFIGURING: if self._account2state[account] == self.CONFIGURING:
while 1: while 1:
acc = self._pop_config_success() acc = self._pop_config_success()
@@ -243,7 +251,11 @@ class PendingConfigure:
acc._evtracker.consume_events() acc._evtracker.consume_events()
def bring_online(self): 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) print("wait_all_configured finds accounts=", self._account2state)
for acc, state in self._account2state.items(): for acc, state in self._account2state.items():
@@ -280,6 +292,7 @@ class PendingConfigure:
acc.add_account_plugin(logger, name=acc._logid) acc.add_account_plugin(logger, name=acc._logid)
def init_direct_imap(self, acc): def init_direct_imap(self, acc):
""" idempotent function for initializing direct_imap."""
from deltachat.direct_imap import DirectImap from deltachat.direct_imap import DirectImap
if not hasattr(acc, "direct_imap"): if not hasattr(acc, "direct_imap"):
acc.direct_imap = DirectImap(acc) acc.direct_imap = DirectImap(acc)
@@ -304,13 +317,12 @@ class ACFactory:
self.init_time = time.time() self.init_time = time.time()
self.tmpdir = tmpdir self.tmpdir = tmpdir
self.pytestconfig = request.config self.pytestconfig = request.config
self.testprocess = testprocess
self.data = data self.data = data
self._liveconfig_producer = testprocess.get_liveconfig_producer() self._liveconfig_producer = testprocess.get_liveconfig_producer()
self._finalizers = [] self._finalizers = []
self._accounts = [] self._accounts = []
self._pending_configure = PendingConfigure(self.init_time) self._acsetup = ACSetup(self.init_time)
self._preconfigured_keys = ["alice", "bob", "charlie", self._preconfigured_keys = ["alice", "bob", "charlie",
"dom", "elena", "fiona"] "dom", "elena", "fiona"]
self.set_logging_default(False) self.set_logging_default(False)
@@ -338,7 +350,7 @@ class ACFactory:
if "e2ee_enabled" not in configdict: if "e2ee_enabled" not in configdict:
configdict["e2ee_enabled"] = "1" configdict["e2ee_enabled"] = "1"
if self.testprocess.pytestconfig.getoption("--strict-tls"): if self.pytestconfig.getoption("--strict-tls"):
# Enable strict certificate checks for online accounts # Enable strict certificate checks for online accounts
configdict["imap_certificate_checks"] = str(const.DC_CERTCK_STRICT) configdict["imap_certificate_checks"] = str(const.DC_CERTCK_STRICT)
configdict["smtp_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) configdict.update(kwargs)
ac = self.prepare_account_from_liveconfig(configdict) ac = self.prepare_account_from_liveconfig(configdict)
self._pending_configure.add_account(ac) self._acsetup.start_configure(ac)
return ac return ac
def prepare_account_from_liveconfig(self, configdict): def prepare_account_from_liveconfig(self, configdict):
@@ -413,15 +425,13 @@ class ACFactory:
self._preconfigure_key(ac, configdict["addr"]) self._preconfigure_key(ac, configdict["addr"])
return ac return ac
def new_cloned_configuring_account(self, account): def wait_configured(self, account):
return self.new_online_configuring_account(cloned_from=account) """ Wait until the specified account has successfully completed configure. """
self._acsetup.wait_one_configured(account)
def wait_configured(self, acc):
self._pending_configure.wait_one_configured(acc)
def bring_accounts_online(self): def bring_accounts_online(self):
print("bringing accounts online") print("bringing accounts online")
self._pending_configure.bring_online() self._acsetup.bring_online()
print("all accounts online") print("all accounts online")
def get_online_accounts(self, num): def get_online_accounts(self, num):

View File

@@ -726,7 +726,7 @@ class TestOnlineAccount:
def test_one_account_send_bcc_setting(self, acfactory, lp): def test_one_account_send_bcc_setting(self, acfactory, lp):
ac1 = acfactory.new_online_configuring_account() ac1 = acfactory.new_online_configuring_account()
ac2 = 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() acfactory.bring_accounts_online()
# test if sent messages are copied to it via BCC. # 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.""" """Test that message marked as seen on one device is marked as seen on another."""
ac1 = acfactory.new_online_configuring_account() ac1 = acfactory.new_online_configuring_account()
ac2 = 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() acfactory.bring_accounts_online()
ac1.set_config("bcc_self", "1") ac1.set_config("bcc_self", "1")
@@ -1531,7 +1531,7 @@ class TestOnlineAccount:
def test_no_old_msg_is_fresh(self, acfactory, lp): def test_no_old_msg_is_fresh(self, acfactory, lp):
ac1 = acfactory.new_online_configuring_account() ac1 = acfactory.new_online_configuring_account()
ac2 = 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() acfactory.bring_accounts_online()
ac1.set_config("e2ee_enabled", "0") ac1.set_config("e2ee_enabled", "0")
@@ -1895,7 +1895,7 @@ class TestOnlineAccount:
# before ther setup message is send. DC does not read old messages # before ther setup message is send. DC does not read old messages
# as of Jul2019 # as of Jul2019
ac1 = acfactory.new_online_configuring_account() 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() acfactory.bring_accounts_online()
lp.sec("trigger ac setup message and return setupcode") lp.sec("trigger ac setup message and return setupcode")
@@ -1916,7 +1916,7 @@ class TestOnlineAccount:
def test_ac_setup_message_twice(self, acfactory, lp): def test_ac_setup_message_twice(self, acfactory, lp):
ac1 = acfactory.new_online_configuring_account() 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() acfactory.bring_accounts_online()
lp.sec("trigger ac setup message but ignore") lp.sec("trigger ac setup message but ignore")
@@ -2400,7 +2400,7 @@ class TestOnlineAccount:
lp.sec("ac3 reinstalls DC and generates a new key") lp.sec("ac3 reinstalls DC and generates a new key")
ac3.stop_io() ac3.stop_io()
acfactory.remove_preconfigured_keys() acfactory.remove_preconfigured_keys()
ac4 = acfactory.new_cloned_configuring_account(ac3) ac4 = acfactory.new_online_configuring_account(cloned_from=ac3)
acfactory.wait_configured(ac4) acfactory.wait_configured(ac4)
# Create contacts to make sure incoming messages are not treated as contact requests # Create contacts to make sure incoming messages are not treated as contact requests
chat41 = ac4.create_chat(ac1) chat41 = ac4.create_chat(ac1)
@@ -2787,7 +2787,7 @@ class TestOnlineAccount:
# would also find the "Sent" folder, but it would be too late: # 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 # The sentbox thread, started by `start_io()`, would have seen that there is no
# ConfiguredSentboxFolder and do nothing. # ConfiguredSentboxFolder and do nothing.
acfactory._pending_configure.add_account(ac1, reconfigure=True) acfactory._acsetup.start_configure(ac1, reconfigure=True)
acfactory.bring_accounts_online() acfactory.bring_accounts_online()
assert_folders_configured(ac1) assert_folders_configured(ac1)
@@ -2805,7 +2805,7 @@ class TestOnlineAccount:
assert_folders_configured(ac1) assert_folders_configured(ac1)
lp.sec("create a cloned ac1 and fetch contact history during configure") 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") ac1_clone.set_config("fetch_existing_msgs", "1")
acfactory.wait_configured(ac1_clone) acfactory.wait_configured(ac1_clone)
ac1_clone.start_io() ac1_clone.start_io()
@@ -2851,7 +2851,7 @@ class TestOnlineAccount:
assert ac1.direct_imap.idle_wait_for_seen() assert ac1.direct_imap.idle_wait_for_seen()
lp.sec("Clone online account and let it fetch the existing messages") 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") ac1_clone.set_config("fetch_existing_msgs", "1")
acfactory.wait_configured(ac1_clone) acfactory.wait_configured(ac1_clone)

View File

@@ -6,16 +6,16 @@ from deltachat import register_global_plugin
from deltachat.hookspec import global_hookimpl from deltachat.hookspec import global_hookimpl
from deltachat.capi import ffi from deltachat.capi import ffi
from deltachat.capi import lib from deltachat.capi import lib
from deltachat.testplugin import PendingConfigure from deltachat.testplugin import ACSetup
# from deltachat.account import EventLogger # from deltachat.account import EventLogger
class TestPendingConfigure: class TestACSetup:
def test_basic_states(self, acfactory, monkeypatch): def test_basic_states(self, acfactory, monkeypatch):
pc = PendingConfigure(init_time=0.0) pc = ACSetup(init_time=0.0)
acc = acfactory.get_unconfigured_account() acc = acfactory.get_unconfigured_account()
monkeypatch.setattr(acc, "configure", lambda **kwargs: None) monkeypatch.setattr(acc, "configure", lambda **kwargs: None)
pc.add_account(acc) pc.start_configure(acc)
assert pc._account2state[acc] == pc.CONFIGURING assert pc._account2state[acc] == pc.CONFIGURING
pc._configured_events.put((acc, True)) pc._configured_events.put((acc, True))
monkeypatch.setattr(pc, "init_direct_imap", lambda *args, **kwargs: None) monkeypatch.setattr(pc, "init_direct_imap", lambda *args, **kwargs: None)
@@ -26,15 +26,15 @@ class TestPendingConfigure:
assert pc._account2state[acc] == pc.IDLEREADY assert pc._account2state[acc] == pc.IDLEREADY
def test_two_accounts_one_waited_all_started(self, monkeypatch, acfactory): 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, "init_direct_imap", lambda *args, **kwargs: None)
monkeypatch.setattr(pc, "_onconfigure_start_io", lambda *args, **kwargs: None) monkeypatch.setattr(pc, "_onconfigure_start_io", lambda *args, **kwargs: None)
ac1 = acfactory.get_unconfigured_account() ac1 = acfactory.get_unconfigured_account()
monkeypatch.setattr(ac1, "configure", lambda **kwargs: None) monkeypatch.setattr(ac1, "configure", lambda **kwargs: None)
pc.add_account(ac1) pc.start_configure(ac1)
ac2 = acfactory.get_unconfigured_account() ac2 = acfactory.get_unconfigured_account()
monkeypatch.setattr(ac2, "configure", lambda **kwargs: None) monkeypatch.setattr(ac2, "configure", lambda **kwargs: None)
pc.add_account(ac2) pc.start_configure(ac2)
assert pc._account2state[ac1] == pc.CONFIGURING assert pc._account2state[ac1] == pc.CONFIGURING
assert pc._account2state[ac2] == pc.CONFIGURING assert pc._account2state[ac2] == pc.CONFIGURING
pc._configured_events.put((ac1, True)) pc._configured_events.put((ac1, True))