Skip to content

Commit

Permalink
fix: hide faas server run method
Browse files Browse the repository at this point in the history
  • Loading branch information
jyecusch committed Oct 26, 2021
1 parent a76d357 commit f46dd9d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
12 changes: 6 additions & 6 deletions nitric/faas.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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):
Expand All @@ -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)
Expand Down
20 changes: 10 additions & 10 deletions tests/test_faas.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit f46dd9d

Please sign in to comment.