-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[tests] handler decomposition (#125)
Why === Striking a balance between test handler reuse and colocating the handler specifiers with the tests that use them. What changed ============ - Decomposing monolithic server handler specifiers into directly DI-ing reusable components - For tests that need bespoke handlers, defining those alongside the methods that use them. Test plan ========= CI
- Loading branch information
1 parent
88f4347
commit 502278f
Showing
7 changed files
with
171 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
from typing import Any, AsyncGenerator, AsyncIterator, Iterator | ||
|
||
import grpc | ||
import grpc.aio | ||
|
||
from replit_river.rpc import ( | ||
rpc_method_handler, | ||
stream_method_handler, | ||
subscription_method_handler, | ||
upload_method_handler, | ||
) | ||
from tests.conftest import HandlerMapping, deserialize_request, serialize_response | ||
|
||
|
||
async def rpc_handler(request: str, context: grpc.aio.ServicerContext) -> str: | ||
return f"Hello, {request}!" | ||
|
||
|
||
basic_rpc_method: HandlerMapping = { | ||
("test_service", "rpc_method"): ( | ||
"rpc", | ||
rpc_method_handler(rpc_handler, deserialize_request, serialize_response), | ||
) | ||
} | ||
|
||
|
||
async def upload_handler( | ||
request: Iterator[str] | AsyncIterator[str], context: Any | ||
) -> str: | ||
uploaded_data = [] | ||
if isinstance(request, AsyncIterator): | ||
async for data in request: | ||
uploaded_data.append(data) | ||
else: | ||
for data in request: | ||
uploaded_data.append(data) | ||
return f"Uploaded: {', '.join(uploaded_data)}" | ||
|
||
|
||
basic_upload: HandlerMapping = { | ||
("test_service", "upload_method"): ( | ||
"upload", | ||
upload_method_handler(upload_handler, deserialize_request, serialize_response), | ||
), | ||
} | ||
|
||
|
||
async def subscription_handler( | ||
request: str, context: grpc.aio.ServicerContext | ||
) -> AsyncGenerator[str, None]: | ||
for i in range(5): | ||
yield f"Subscription message {i} for {request}" | ||
|
||
|
||
basic_subscription: HandlerMapping = { | ||
("test_service", "subscription_method"): ( | ||
"subscription", | ||
subscription_method_handler( | ||
subscription_handler, deserialize_request, serialize_response | ||
), | ||
), | ||
} | ||
|
||
|
||
async def stream_handler( | ||
request: Iterator[str] | AsyncIterator[str], | ||
context: grpc.aio.ServicerContext, | ||
) -> AsyncGenerator[str, None]: | ||
if isinstance(request, AsyncIterator): | ||
async for data in request: | ||
yield f"Stream response for {data}" | ||
else: | ||
for data in request: | ||
yield f"Stream response for {data}" | ||
|
||
|
||
basic_stream: HandlerMapping = { | ||
("test_service", "stream_method"): ( | ||
"stream", | ||
stream_method_handler(stream_handler, deserialize_request, serialize_response), | ||
), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.