mirror of
https://github.com/chatmail/core.git
synced 2026-05-08 17:36:29 +03:00
Turn start_rpc_server into a context manager
This commit is contained in:
@@ -7,7 +7,7 @@ import deltachat_rpc_client as dc
|
|||||||
|
|
||||||
|
|
||||||
async def main():
|
async def main():
|
||||||
rpc = await dc.start_rpc_server()
|
async with dc.start_rpc_server() as rpc:
|
||||||
deltachat = dc.Deltachat(rpc)
|
deltachat = dc.Deltachat(rpc)
|
||||||
system_info = await deltachat.get_system_info()
|
system_info = await deltachat.get_system_info()
|
||||||
logging.info("Running deltachat core %s", system_info["deltachat_core_version"])
|
logging.info("Running deltachat core %s", system_info["deltachat_core_version"])
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import asyncio
|
|||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
|
from contextlib import asynccontextmanager
|
||||||
|
|
||||||
import aiohttp
|
import aiohttp
|
||||||
|
|
||||||
@@ -73,6 +74,7 @@ class Rpc:
|
|||||||
return method
|
return method
|
||||||
|
|
||||||
|
|
||||||
|
@asynccontextmanager
|
||||||
async def start_rpc_server(*args, **kwargs):
|
async def start_rpc_server(*args, **kwargs):
|
||||||
proc = await asyncio.create_subprocess_exec(
|
proc = await asyncio.create_subprocess_exec(
|
||||||
"deltachat-rpc-server",
|
"deltachat-rpc-server",
|
||||||
@@ -82,7 +84,10 @@ async def start_rpc_server(*args, **kwargs):
|
|||||||
**kwargs
|
**kwargs
|
||||||
)
|
)
|
||||||
rpc = Rpc(proc)
|
rpc = Rpc(proc)
|
||||||
return rpc
|
try:
|
||||||
|
yield rpc
|
||||||
|
finally:
|
||||||
|
proc.terminate()
|
||||||
|
|
||||||
|
|
||||||
async def new_online_account():
|
async def new_online_account():
|
||||||
|
|||||||
@@ -10,9 +10,10 @@ from deltachat_rpc_client import Deltachat
|
|||||||
|
|
||||||
@pytest_asyncio.fixture
|
@pytest_asyncio.fixture
|
||||||
async def rpc(tmp_path):
|
async def rpc(tmp_path):
|
||||||
return await deltachat_rpc_client.start_rpc_server(
|
async with deltachat_rpc_client.start_rpc_server(
|
||||||
env={**os.environ, "DC_ACCOUNTS_PATH": str(tmp_path / "accounts")}
|
env={**os.environ, "DC_ACCOUNTS_PATH": str(tmp_path / "accounts")}
|
||||||
)
|
) as rpc:
|
||||||
|
yield rpc
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
|
|||||||
Reference in New Issue
Block a user