11import asyncio
22import threading
3- import time
4- from asyncio import Event
53from collections .abc import AsyncIterator
64
75import grpc
86import pytest
97from grpclib .server import Server
108
11- from tests .output_betterproto .simple_service import (
12- Request ,
13- Response ,
14- SimpleServiceBase ,
15- SimpleServiceSyncStub ,
16- )
9+ from tests .output_betterproto .simple_service import Request , Response , SimpleServiceBase , SimpleServiceSyncStub
1710
1811
1912class SimpleService (SimpleServiceBase ):
@@ -37,13 +30,15 @@ async def get_stream_stream(self, messages: "AsyncIterator[Request]") -> "AsyncI
3730
3831@pytest .mark .asyncio
3932async def test_sync_client ():
40- # The event will be set to close the server
41- close_server_event = Event () # TODO the event is used in two different asyncio loops
33+ start_server_event = threading . Event ()
34+ close_server_event = asyncio . Event ()
4235
4336 def start_server ():
4437 async def run_server ():
4538 server = Server ([SimpleService ()])
4639 await server .start ("127.0.0.1" , 1234 )
40+ start_server_event .set ()
41+
4742 await close_server_event .wait ()
4843 server .close ()
4944
@@ -56,9 +51,9 @@ async def run_server():
5651 server_thread .start ()
5752
5853 # Create a sync client
59- with grpc .insecure_channel ("localhost:1234" ) as channel :
60- time .sleep (1 ) # TODO not a good solution
54+ start_server_event .wait ()
6155
56+ with grpc .insecure_channel ("localhost:1234" ) as channel :
6257 client = SimpleServiceSyncStub (channel )
6358
6459 response = client .get_unary_unary (Request (value = 42 ))
0 commit comments