mirror of
https://github.com/chatmail/core.git
synced 2026-05-20 15:26:30 +03:00
Merge remote-tracking branch 'upstream/link2xt/async-jsonrpc-client' into adb/async-jsonrpc-client
This commit is contained in:
@@ -1,13 +1,14 @@
|
||||
import asyncio
|
||||
import json
|
||||
import os
|
||||
from typing import List
|
||||
from typing import AsyncGenerator, List
|
||||
|
||||
import aiohttp
|
||||
import pytest_asyncio
|
||||
|
||||
from .account import Account
|
||||
from .deltachat import Deltachat
|
||||
from .rpc import Rpc, start_rpc_server
|
||||
from .rpc import start_rpc_server
|
||||
|
||||
|
||||
async def get_temp_credentials() -> dict:
|
||||
@@ -39,12 +40,13 @@ class ACFactory:
|
||||
|
||||
|
||||
@pytest_asyncio.fixture
|
||||
async def rpc(tmp_path) -> Rpc:
|
||||
return await start_rpc_server(
|
||||
env={**os.environ, "DC_ACCOUNTS_PATH": str(tmp_path / "accounts")}
|
||||
)
|
||||
async def rpc(tmp_path) -> AsyncGenerator:
|
||||
env = {**os.environ, "DC_ACCOUNTS_PATH": str(tmp_path / "accounts")}
|
||||
async with start_rpc_server(env=env) as rpc:
|
||||
yield rpc
|
||||
await asyncio.sleep(0.1) # avoid RuntimeError: Event loop is closed
|
||||
|
||||
|
||||
@pytest_asyncio.fixture
|
||||
async def acfactory(rpc) -> ACFactory:
|
||||
return ACFactory(Deltachat(rpc))
|
||||
async def acfactory(rpc) -> AsyncGenerator:
|
||||
yield ACFactory(Deltachat(rpc))
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import asyncio
|
||||
import json
|
||||
from typing import Any, Dict, Optional
|
||||
from contextlib import asynccontextmanager
|
||||
from typing import Any, AsyncGenerator, Dict, Optional
|
||||
|
||||
|
||||
class JsonRpcError(Exception):
|
||||
@@ -20,6 +21,8 @@ class Rpc:
|
||||
async def reader_loop(self) -> None:
|
||||
while True:
|
||||
line = await self.process.stdout.readline()
|
||||
if not line: # EOF
|
||||
break
|
||||
response = json.loads(line)
|
||||
if "id" in response:
|
||||
fut = self.request_events.pop(response["id"])
|
||||
@@ -67,7 +70,8 @@ class Rpc:
|
||||
return method
|
||||
|
||||
|
||||
async def start_rpc_server(*args, **kwargs) -> Rpc:
|
||||
@asynccontextmanager
|
||||
async def start_rpc_server(*args, **kwargs) -> AsyncGenerator:
|
||||
"""The given arguments will be passed to asyncio.create_subprocess_exec()"""
|
||||
proc = await asyncio.create_subprocess_exec(
|
||||
"deltachat-rpc-server",
|
||||
@@ -77,4 +81,7 @@ async def start_rpc_server(*args, **kwargs) -> Rpc:
|
||||
**kwargs
|
||||
)
|
||||
rpc = Rpc(proc)
|
||||
return rpc
|
||||
try:
|
||||
yield rpc
|
||||
finally:
|
||||
proc.terminate()
|
||||
|
||||
Reference in New Issue
Block a user