From 2b5a2d7218984c1c75d3f76ce1529189f8724275 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20H=C3=A9rault?= Date: Thu, 5 May 2022 12:14:09 +0300 Subject: [PATCH] Revert "Revert "Revert "fixed hanging when python raises exception or return nothing""" --- .../python-runner/PythonRunner.js | 3 +- .../handler-runner/python-runner/invoke.py | 6 +-- .../invocations/InvocationsController.js | 6 ++- tests/integration/python/python2/handler.py | 9 ---- .../python/python2/python2.test.js | 46 ------------------- .../integration/python/python2/serverless.yml | 6 --- tests/integration/python/python3/handler.py | 9 ---- .../python/python3/python3.test.js | 46 ------------------- .../integration/python/python3/serverless.yml | 6 --- 9 files changed, 6 insertions(+), 131 deletions(-) diff --git a/src/lambda/handler-runner/python-runner/PythonRunner.js b/src/lambda/handler-runner/python-runner/PythonRunner.js index fe82e85ce..70ad2da84 100644 --- a/src/lambda/handler-runner/python-runner/PythonRunner.js +++ b/src/lambda/handler-runner/python-runner/PythonRunner.js @@ -123,8 +123,7 @@ export default class PythonRunner { const onLine = (line) => { try { const parsed = this._parsePayload(line.toString()) - - if (typeof parsed !== 'undefined') { + if (parsed) { this.handlerProcess.stdout.readline.removeListener('line', onLine) this.handlerProcess.stderr.removeListener('data', onErr) return accept(parsed) diff --git a/src/lambda/handler-runner/python-runner/invoke.py b/src/lambda/handler-runner/python-runner/invoke.py index 4d90531f7..9e279d40c 100644 --- a/src/lambda/handler-runner/python-runner/invoke.py +++ b/src/lambda/handler-runner/python-runner/invoke.py @@ -94,11 +94,7 @@ def log(self): input = json.loads(stdin.readline()) context = FakeLambdaContext(**input.get('context', {})) - - try: - result = handler(input['event'], context) - except Exception as e: - result = str(e) + result = handler(input['event'], context) data = { # just an identifier to distinguish between diff --git a/src/lambda/routes/invocations/InvocationsController.js b/src/lambda/routes/invocations/InvocationsController.js index af964ae56..e09c9a715 100644 --- a/src/lambda/routes/invocations/InvocationsController.js +++ b/src/lambda/routes/invocations/InvocationsController.js @@ -90,8 +90,10 @@ export default class InvocationsController { } // Checking if the result of the Lambda Invoke is a primitive string to wrap it. this is for future post-processing such as Step Functions Tasks - if (typeof result === 'string') { - result = `"${result}"` + if (result) { + if (typeof result === 'string') { + result = `"${result}"` + } } // result is actually the Payload. diff --git a/tests/integration/python/python2/handler.py b/tests/integration/python/python2/handler.py index dba004324..8064fe490 100644 --- a/tests/integration/python/python2/handler.py +++ b/tests/integration/python/python2/handler.py @@ -9,12 +9,3 @@ def hello(event, context): "body": json.dumps(body), "statusCode": 200, } - -def helloReturnEmptyString(event, context): - return "" - -def helloReturnNothing(event, context): - return - -def helloException(event, context): - raise Exception("hello-error") diff --git a/tests/integration/python/python2/python2.test.js b/tests/integration/python/python2/python2.test.js index a1183f064..5ab9a21b4 100644 --- a/tests/integration/python/python2/python2.test.js +++ b/tests/integration/python/python2/python2.test.js @@ -1,12 +1,9 @@ -import { config, Lambda } from 'aws-sdk' import { resolve } from 'path' import fetch from 'node-fetch' import { joinUrl, setup, teardown } from '../../_testHelpers/index.js' jest.setTimeout(60000) -const { stringify } = JSON - // Could not find 'Python 2' executable, skipping 'Python' tests. const _describe = process.env.PYTHON2_DETECTED ? describe : describe.skip @@ -39,47 +36,4 @@ _describe('Python 2 tests', () => { expect(json).toEqual(expected) }) }) - - config.update({ - accessKeyId: 'ABC', - secretAccessKey: 'SECRET', - region: 'us-east-1', - }) - - const lambda = new Lambda({ - apiVersion: '2015-03-31', - endpoint: 'http://localhost:3002', - }) - - // lambda invocation with some cases. - ;[ - { - description: 'should work with python 2 with empty string', - expected: { Payload: stringify(''), StatusCode: 200 }, - functionName: 'helloReturnEmptyString', - }, - { - description: 'should work with python 2 without return value', - expected: { Payload: '', StatusCode: 200 }, - functionName: 'helloReturnNothing', - }, - { - description: 'should work with python 2 raising exception', - expected: { Payload: stringify('hello-error'), StatusCode: 200 }, - functionName: 'helloException', - }, - ].forEach(({ description, expected, functionName }) => { - test(description, async () => { - const params = { - // ClientContext: undefined, - FunctionName: `python-2-tests-dev-${functionName}`, - InvocationType: 'RequestResponse', - // Payload: undefined, - } - - const response = await lambda.invoke(params).promise() - - expect(response).toEqual(expected) - }) - }) }) diff --git a/tests/integration/python/python2/serverless.yml b/tests/integration/python/python2/serverless.yml index f321e0249..8b8005c84 100644 --- a/tests/integration/python/python2/serverless.yml +++ b/tests/integration/python/python2/serverless.yml @@ -18,9 +18,3 @@ functions: method: get path: hello handler: handler.hello - helloReturnEmptyString: - handler: handler.helloReturnEmptyString - helloReturnNothing: - handler: handler.helloReturnNothing - helloException: - handler: handler.helloException diff --git a/tests/integration/python/python3/handler.py b/tests/integration/python/python3/handler.py index dbb1ce0bd..81e8de244 100644 --- a/tests/integration/python/python3/handler.py +++ b/tests/integration/python/python3/handler.py @@ -9,12 +9,3 @@ def hello(event, context): "body": json.dumps(body), "statusCode": 200, } - -def helloReturnEmptyString(event, context): - return "" - -def helloReturnNothing(event, context): - return - -def helloException(event, context): - raise Exception("hello-error") diff --git a/tests/integration/python/python3/python3.test.js b/tests/integration/python/python3/python3.test.js index 1322d24ef..4341a0d1e 100644 --- a/tests/integration/python/python3/python3.test.js +++ b/tests/integration/python/python3/python3.test.js @@ -1,4 +1,3 @@ -import { config, Lambda } from 'aws-sdk' import { platform } from 'os' import { resolve } from 'path' import fetch from 'node-fetch' @@ -6,8 +5,6 @@ import { joinUrl, setup, teardown } from '../../_testHelpers/index.js' jest.setTimeout(60000) -const { stringify } = JSON - // skipping 'Python 3' tests on Windows for now. // Could not find 'Python 3' executable, skipping 'Python' tests. const _describe = @@ -44,47 +41,4 @@ _describe('Python 3 tests', () => { expect(json).toEqual(expected) }) }) - - config.update({ - accessKeyId: 'ABC', - secretAccessKey: 'SECRET', - region: 'us-east-1', - }) - - const lambda = new Lambda({ - apiVersion: '2015-03-31', - endpoint: 'http://localhost:3002', - }) - - // lambda invocation with some cases. - ;[ - { - description: 'should work with python 3 with empty string', - expected: { Payload: stringify(''), StatusCode: 200 }, - functionName: 'helloReturnEmptyString', - }, - { - description: 'should work with python 3 without return value', - expected: { Payload: '', StatusCode: 200 }, - functionName: 'helloReturnNothing', - }, - { - description: 'should work with python 3 raising exception', - expected: { Payload: stringify('hello-error'), StatusCode: 200 }, - functionName: 'helloException', - }, - ].forEach(({ description, expected, functionName }) => { - test(description, async () => { - const params = { - // ClientContext: undefined, - FunctionName: `python-3-tests-dev-${functionName}`, - InvocationType: 'RequestResponse', - // Payload: undefined, - } - - const response = await lambda.invoke(params).promise() - - expect(response).toEqual(expected) - }) - }) }) diff --git a/tests/integration/python/python3/serverless.yml b/tests/integration/python/python3/serverless.yml index 1660bc907..ef1fbb548 100644 --- a/tests/integration/python/python3/serverless.yml +++ b/tests/integration/python/python3/serverless.yml @@ -18,9 +18,3 @@ functions: method: get path: hello handler: handler.hello - helloReturnEmptyString: - handler: handler.helloReturnEmptyString - helloReturnNothing: - handler: handler.helloReturnNothing - helloException: - handler: handler.helloException