From f46dd9dc9cf3774763c7bb6008490a420f582be5 Mon Sep 17 00:00:00 2001 From: Jye Cusch Date: Tue, 26 Oct 2021 14:37:52 +1100 Subject: [PATCH] fix: hide faas server run method --- nitric/faas.py | 12 ++++++------ tests/test_faas.py | 20 ++++++++++---------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/nitric/faas.py b/nitric/faas.py index 44fd51a..9a92f27 100644 --- a/nitric/faas.py +++ b/nitric/faas.py @@ -224,8 +224,8 @@ def compose_middleware(*middlewares: Union[Middleware, List[Middleware]]) -> Mid middlewares = [compose_middleware(m) if isinstance(m, list) else m for m in middlewares] async def handler(ctx, next_middleware=lambda ctx: ctx): - def reduceChain(acc_next, cur): - async def chainedMiddleware(context): + def reduce_chain(acc_next, cur): + async def chained_middleware(context): # Count the positional arguments to determine if the function is a handler or middleware. all_args = cur.__code__.co_argcount kwargs = len(cur.__defaults__) if cur.__defaults__ is not None else 0 @@ -240,9 +240,9 @@ async def chainedMiddleware(context): result = (await cur(context)) if asyncio.iscoroutinefunction(cur) else cur(context) return (await acc_next(result)) if asyncio.iscoroutinefunction(acc_next) else acc_next(result) - return chainedMiddleware + return chained_middleware - middleware_chain = functools.reduce(reduceChain, reversed(middlewares + [next_middleware])) + middleware_chain = functools.reduce(reduce_chain, reversed(middlewares + [next_middleware])) return await middleware_chain(ctx) return handler @@ -296,7 +296,7 @@ def start(self, *handlers: Union[Middleware, List[Middleware]]): if not self._any_handler and not self._http_handler and not self._event_handler: raise Exception("At least one handler function must be provided.") - asyncio.run(self.run()) + asyncio.run(self._run()) @property def _http_handler(self): @@ -306,7 +306,7 @@ def _http_handler(self): def _event_handler(self): return self.__event_handler if self.__event_handler else self._any_handler - async def run(self): + async def _run(self): """Register a new FaaS worker with the Membrane, using the provided function as the handler.""" channel = new_default_channel() client = FaasServiceStub(channel) diff --git a/tests/test_faas.py b/tests/test_faas.py index f014d7c..cae57bf 100644 --- a/tests/test_faas.py +++ b/tests/test_faas.py @@ -109,7 +109,7 @@ def test_start_starts_event_loop(self): mock_run.return_value = mock_run_coroutine with patch("nitric.faas.compose_middleware", mock_compose): - with patch("nitric.faas.FunctionServer.run", mock_run): + with patch("nitric.faas.FunctionServer._run", mock_run): with patch("asyncio.run", mock_asyncio_run): FunctionServer().start(mock_handler) @@ -139,7 +139,7 @@ async def mock_stream(self, request_iterator): with patch("nitric.faas.AsyncChannel", mock_async_channel_init), patch( "nitricapi.nitric.faas.v1.FaasServiceStub.trigger_stream", mock_stream ), patch("nitric.faas.new_default_channel", mock_grpc_channel): - await FunctionServer().http(mock_handler).run() + await FunctionServer().http(mock_handler)._run() # gRPC channel created mock_grpc_channel.assert_called_once() @@ -180,7 +180,7 @@ async def mock_stream(self, request_iterator): with patch("nitric.faas.AsyncChannel", mock_async_channel_init), patch( "nitricapi.nitric.faas.v1.FaasServiceStub.trigger_stream", mock_stream ), patch("nitric.faas.new_default_channel", mock_grpc_channel): - await FunctionServer().http(mock_http_handler).event(mock_event_handler).run() + await FunctionServer().http(mock_http_handler).event(mock_event_handler)._run() # accept the init response from server assert 1 == stream_calls @@ -215,7 +215,7 @@ async def mock_stream(self, request_iterator): with patch("nitric.faas.AsyncChannel", mock_async_channel_init), patch( "nitricapi.nitric.faas.v1.FaasServiceStub.trigger_stream", mock_stream ), patch("nitric.faas.new_default_channel", mock_grpc_channel): - await FunctionServer().http(mock_http_handler).event(mock_event_handler).run() + await FunctionServer().http(mock_http_handler).event(mock_event_handler)._run() # accept the init response from server assert 1 == stream_calls @@ -250,7 +250,7 @@ async def mock_stream(self, request_iterator): with patch("nitric.faas.AsyncChannel", mock_async_channel_init), patch( "nitricapi.nitric.faas.v1.FaasServiceStub.trigger_stream", mock_stream ), patch("nitric.faas.new_default_channel", mock_grpc_channel): - await FunctionServer().http(mock_http_handler).event(mock_event_handler).run() + await FunctionServer().http(mock_http_handler).event(mock_event_handler)._run() # accept the init response from server assert 1 == stream_calls @@ -285,7 +285,7 @@ async def mock_stream(self, request_iterator): with patch("nitric.faas.AsyncChannel", mock_async_channel_init), patch( "nitricapi.nitric.faas.v1.FaasServiceStub.trigger_stream", mock_stream ), patch("nitric.faas.new_default_channel", mock_grpc_channel): - await FunctionServer().http(mock_http_handler).event(mock_event_handler).run() + await FunctionServer().http(mock_http_handler).event(mock_event_handler)._run() # accept the init response from server assert 1 == stream_calls @@ -320,7 +320,7 @@ async def mock_stream(self, request_iterator): with patch("nitric.faas.AsyncChannel", mock_async_channel_init), patch( "nitricapi.nitric.faas.v1.FaasServiceStub.trigger_stream", mock_stream ), patch("nitric.faas.new_default_channel", mock_grpc_channel): - await FunctionServer().http(mock_http_handler).event(mock_event_handler).run() + await FunctionServer().http(mock_http_handler).event(mock_event_handler)._run() # accept the init response from server assert 1 == stream_calls @@ -349,7 +349,7 @@ async def mock_stream(self, request_iterator): with patch("nitric.faas.AsyncChannel", mock_async_channel_init), patch( "nitricapi.nitric.faas.v1.FaasServiceStub.trigger_stream", mock_stream ), patch("nitric.faas.new_default_channel", mock_grpc_channel): - await FunctionServer().event(mock_handler).run() + await FunctionServer().event(mock_handler)._run() # accept the trigger response from server assert 1 == stream_calls @@ -388,7 +388,7 @@ async def mock_stream(self, request_iterator): with patch("nitric.faas.AsyncChannel", mock_async_channel_init), patch( "nitricapi.nitric.faas.v1.FaasServiceStub.trigger_stream", mock_stream ), patch("nitric.faas.new_default_channel", mock_grpc_channel): - await FunctionServer().http(mock_handler).run() + await FunctionServer().http(mock_handler)._run() # accept the trigger response from server assert 1 == stream_calls @@ -429,7 +429,7 @@ async def mock_stream(self, request_iterator): with patch("nitric.faas.AsyncChannel", mock_async_channel_init), patch( "nitricapi.nitric.faas.v1.FaasServiceStub.trigger_stream", mock_stream ), patch("nitric.faas.new_default_channel", mock_grpc_channel): - await FunctionServer().http(mock_handler).run() + await FunctionServer().http(mock_handler)._run() # accept the trigger response from server assert 1 == stream_calls