Skip to content

Commit 2664b99

Browse files
committed
test: assert that local lambda invoke function runs
1 parent 4860576 commit 2664b99

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

tests/unit/local/lambda_service/test_local_lambda_http_service.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import threading
12
import json
23
from datetime import datetime
34
from unittest import TestCase
@@ -481,6 +482,38 @@ def test_invoke_request_handler_async_invocation_unsupported_function_returns_er
481482
lambda_error_responses_mock.not_implemented_locally.assert_called_once_with("Dry Run invocation not supported")
482483
self.assertEqual(result, "error response")
483484

485+
def test_event_invocation_runs_async_task(self):
486+
# Test that Event invocation type runs the function asynchronously
487+
handler_returned = threading.Event()
488+
finished = threading.Event()
489+
490+
def fake_invoke(*args, **kwargs):
491+
if handler_returned.wait(timeout=5):
492+
finished.set()
493+
494+
request_mock = Mock()
495+
request_mock.get_data.return_value = b"{}"
496+
request_mock.args = {}
497+
request_mock.headers = {"X-Amz-Invocation-Type": "Event"}
498+
local_lambda_http_service.request = request_mock
499+
500+
lambda_runner_mock = Mock()
501+
service = LocalLambdaHttpService(lambda_runner=lambda_runner_mock, port=3000, host="localhost")
502+
service._invoke_lambda = fake_invoke
503+
service.create()
504+
505+
response = service._invoke_request_handler(function_name="HelloWorld")
506+
507+
# Assert that first a 202 response is returned
508+
assert response.status_code == 202
509+
510+
# Then assert that invoke has not finished
511+
assert not finished.is_set()
512+
handler_returned.set()
513+
514+
# Finally assert that invoke has finished
515+
assert finished.wait(timeout=5), "Task never finished"
516+
484517
@patch("samcli.local.lambda_service.local_lambda_http_service.LocalLambdaHttpService.service_response")
485518
@patch("samcli.local.lambda_service.local_lambda_http_service.ThreadPoolExecutor")
486519
def test_invoke_request_handler_async_invocation_submits_to_executor(

0 commit comments

Comments
 (0)