From 801d636eb5cccfe167f81fd55a4fd737a2a81a85 Mon Sep 17 00:00:00 2001 From: holger krekel Date: Tue, 19 Apr 2022 10:25:01 +0200 Subject: [PATCH] enhance waiting for direct imap idle events to prevent randomness --- python/src/deltachat/direct_imap.py | 12 +++++++++++- python/tests/test_account.py | 24 ++++++++---------------- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/python/src/deltachat/direct_imap.py b/python/src/deltachat/direct_imap.py index 71c46b7b8..b8a2f2243 100644 --- a/python/src/deltachat/direct_imap.py +++ b/python/src/deltachat/direct_imap.py @@ -230,7 +230,15 @@ class DirectImap: self.account.log("imap-direct: idle_check returned {!r}".format(res)) return res - def idle_wait_for_seen(self): + def idle_wait_for_new_message(self, terminate=False): + while 1: + for item in self.idle_check(): + if item[1] in (b'EXISTS', b'RECENT'): + if terminate: + self.idle_done() + return item + + def idle_wait_for_seen(self, terminate=False): """ Return first message with SEEN flag from a running idle-stream REtiurn. """ @@ -239,6 +247,8 @@ class DirectImap: if item[1] == FETCH: if item[2][0] == FLAGS: if SEEN in item[2][1]: + if terminate: + self.idle_done() return item[0] def idle_done(self): diff --git a/python/tests/test_account.py b/python/tests/test_account.py index 3f7ae0515..66f3450b7 100644 --- a/python/tests/test_account.py +++ b/python/tests/test_account.py @@ -1013,7 +1013,7 @@ class TestOnlineAccount: assert ev.data1 == msg2.chat.id assert ev.data2 == 0 - ac2.direct_imap.idle_check(terminate=True) + ac2.direct_imap.idle_wait_for_new_message(terminate=True) lp.step("1") for i in range(2): ev = ac1._evtracker.get_matching("DC_EVENT_MSG_READ") @@ -1044,8 +1044,7 @@ class TestOnlineAccount: ac1.create_chat(ac2).send_text("Hello!") - # Wait for the message to arrive. - ac2.direct_imap.idle_check(terminate=True) + ac2.direct_imap.idle_wait_for_new_message(terminate=True) # Emulate moving of the message to DeltaChat folder by Sieve rule. # mailcow server contains this rule by default. @@ -1059,8 +1058,7 @@ class TestOnlineAccount: # Accept the contact request. msg.chat.accept() ac2.mark_seen_messages([msg]) - ac2.direct_imap.idle_wait_for_seen() - ac2.direct_imap.idle_done() + ac2.direct_imap.idle_wait_for_seen(terminate=True) fetch = list(ac2.direct_imap.conn.fetch("*", b'FLAGS').values()) flags = fetch[-1][b'FLAGS'] @@ -2211,7 +2209,7 @@ class TestOnlineAccount: ac1.direct_imap.idle_start() ac2.create_chat(ac1).send_text("Hi") - ac1.direct_imap.idle_check(terminate=False) + ac1.direct_imap.idle_wait_for_new_message(terminate=False) ac1.maybe_network() ac1._evtracker.wait_for_all_work_done() @@ -2223,7 +2221,7 @@ class TestOnlineAccount: ac2.create_chat(ac1).send_text("Hi 2") - ac1.direct_imap.idle_check(terminate=True) + ac1.direct_imap.idle_wait_for_new_message(terminate=True) ac1.maybe_network() ac1._evtracker.wait_for_connectivity_change(const.DC_CONNECTIVITY_CONNECTED, const.DC_CONNECTIVITY_WORKING) ac1._evtracker.wait_for_connectivity_change(const.DC_CONNECTIVITY_WORKING, const.DC_CONNECTIVITY_CONNECTED) @@ -2248,7 +2246,7 @@ class TestOnlineAccount: ac1.direct_imap.idle_start() ac2.create_chat(ac1).send_text("Hi") - ac1.direct_imap.idle_check(terminate=True) + ac1.direct_imap.idle_wait_for_new_message(terminate=True) ac1.maybe_network() while 1: @@ -2446,7 +2444,7 @@ class TestOnlineAccount: ac2.create_chat(ac1) sent_msg = chat1.send_text("hello") - imap2.idle_check(terminate=False) + imap2.idle_wait_for_new_message(terminate=False) msg = ac2._evtracker.wait_next_incoming_message() assert msg.text == "hello" @@ -2717,13 +2715,7 @@ class TestOnlineAccount: ac1.direct_imap.select_config_folder("inbox") ac1.direct_imap.idle_start() acfactory.get_accepted_chat(ac2, ac1).send_text("hello") - while True: - if len(ac1.direct_imap.idle_check(terminate=True)) > 1: - # If length is 1, it's [(b'OK', b'Still here')] - # Could happen on very slow network. - # - # More is usually [(1, b'EXISTS'), (1, b'RECENT')] - break + ac1.direct_imap.idle_wait_for_new_message(terminate=True) ac1.direct_imap.conn.move(["*"], folder) # "*" means "biggest UID in mailbox" lp.sec("Everything prepared, now see if DeltaChat finds the message (" + variant + ")")