mirror of
https://github.com/Significant-Gravitas/AutoGPT.git
synced 2026-02-11 23:35:25 -05:00
Using sync code in the async route often introduces a blocking event-loop code that impacts stability. The current RPC system only provides a synchronous client to call the service endpoints. The scope of this PR is to provide an entirely decoupled signature between client and server, allowing the client can mix & match async & sync options on the client code while not changing the async/sync nature of the server. ### Changes 🏗️ * Add support for flexible async/sync RPC client. * Migrate scheduler client to all-async client. ### Checklist 📋 #### For code changes: - [x] I have clearly listed my changes in the PR description - [x] I have made a test plan - [x] I have tested my changes according to the test plan: - [x] Scheduler route test. - [x] Modified service_test.py - [x] Run normal agent executions
63 lines
1.4 KiB
Python
63 lines
1.4 KiB
Python
import pytest
|
|
|
|
from backend.util.service import (
|
|
AppService,
|
|
AppServiceClient,
|
|
endpoint_to_async,
|
|
expose,
|
|
get_service_client,
|
|
)
|
|
|
|
TEST_SERVICE_PORT = 8765
|
|
|
|
|
|
class ServiceTest(AppService):
|
|
def __init__(self):
|
|
super().__init__()
|
|
|
|
def cleanup(self):
|
|
pass
|
|
|
|
@classmethod
|
|
def get_port(cls) -> int:
|
|
return TEST_SERVICE_PORT
|
|
|
|
@expose
|
|
def add(self, a: int, b: int) -> int:
|
|
return a + b
|
|
|
|
@expose
|
|
def subtract(self, a: int, b: int) -> int:
|
|
return a - b
|
|
|
|
@expose
|
|
def fun_with_async(self, a: int, b: int) -> int:
|
|
async def add_async(a: int, b: int) -> int:
|
|
return a + b
|
|
|
|
return self.run_and_wait(add_async(a, b))
|
|
|
|
|
|
class ServiceTestClient(AppServiceClient):
|
|
@classmethod
|
|
def get_service_type(cls):
|
|
return ServiceTest
|
|
|
|
add = ServiceTest.add
|
|
subtract = ServiceTest.subtract
|
|
fun_with_async = ServiceTest.fun_with_async
|
|
|
|
add_async = endpoint_to_async(ServiceTest.add)
|
|
subtract_async = endpoint_to_async(ServiceTest.subtract)
|
|
|
|
|
|
@pytest.mark.asyncio(loop_scope="session")
|
|
async def test_service_creation(server):
|
|
with ServiceTest():
|
|
client = get_service_client(ServiceTestClient)
|
|
assert client.add(5, 3) == 8
|
|
assert client.subtract(10, 4) == 6
|
|
assert client.fun_with_async(5, 3) == 8
|
|
assert await client.add_async(5, 3) == 8
|
|
assert await client.subtract_async(10, 4) == 6
|