Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion deltachat-rpc-client/src/deltachat_rpc_client/deltachat.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from typing import TYPE_CHECKING

from ._utils import AttrDict
from ._utils import AttrDict, futuremethod
from .account import Account

if TYPE_CHECKING:
Expand Down Expand Up @@ -39,6 +39,15 @@ def stop_io(self) -> None:
"""Stop the I/O of all accounts."""
self.rpc.stop_io_for_all_accounts()

@futuremethod
def background_fetch(self, timeout_in_seconds: int) -> None:
"""Run background fetch for all accounts."""
yield self.rpc.background_fetch.future(timeout_in_seconds)

def stop_background_fetch(self) -> None:
"""Stop ongoing background fetch."""
self.rpc.stop_background_fetch()

def maybe_network(self) -> None:
"""Indicate that the network conditions might have changed."""
self.rpc.maybe_network()
Expand Down
10 changes: 8 additions & 2 deletions deltachat-rpc-client/src/deltachat_rpc_client/pytestplugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,15 @@ def rpc(tmp_path) -> AsyncGenerator:


@pytest.fixture
def acfactory(rpc) -> AsyncGenerator:
def dc(rpc) -> DeltaChat:
"""Return account manager."""
return DeltaChat(rpc)


@pytest.fixture
def acfactory(dc) -> AsyncGenerator:
"""Return account factory fixture."""
return ACFactory(DeltaChat(rpc))
return ACFactory(dc)


@pytest.fixture
Expand Down
37 changes: 37 additions & 0 deletions deltachat-rpc-client/tests/test_something.py
Original file line number Diff line number Diff line change
Expand Up @@ -965,3 +965,40 @@ def test_immediate_autodelete(acfactory, direct_imap, log):
ev = ac1.wait_for_event(EventType.MSG_READ)
assert ev.chat_id == chat1.id
assert ev.msg_id == sent_msg.id


def test_background_fetch(acfactory, dc):
ac1, ac2 = acfactory.get_online_accounts(2)
ac1.stop_io()

ac1_chat = ac1.create_chat(ac2)

ac2_chat = ac2.create_chat(ac1)
ac2_chat.send_text("Hello!")

while True:
dc.background_fetch(300)
messages = ac1_chat.get_messages()
snapshot = messages[-1].get_snapshot()
if snapshot.text == "Hello!":
break

# Stopping background fetch immediately after starting
# does not result in any errors.
background_fetch_future = dc.background_fetch.future(300)
dc.stop_background_fetch()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this reliable? Core's stop_background_fetch() does nothing if background_fetch_interrupt_sender is None. Maybe sleep for a second before stopping?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we sleep for a second, then maybe background fetch has already stopped. I don't know how to test stop_background_fetch(), so did something.

background_fetch_future()

# Starting background fetch with zero timeout is ok,
# it should terminate immediately.
dc.background_fetch(0)

# Background fetch can still be used to send and receive messages.
ac2_chat.send_text("Hello again!")

while True:
dc.background_fetch(300)
messages = ac1_chat.get_messages()
snapshot = messages[-1].get_snapshot()
if snapshot.text == "Hello again!":
break