Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: child processes run mode #1527

Merged
merged 2 commits into from
Aug 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,6 @@ export default class ChildProcessRunner {
},
)

let message

try {
message = new Promise((res, rej) => {
childProcess.on('message', (data) => {
if (data.error) rej(data.error)
else res(data)
})
})
} finally {
childProcess.kill()
}

childProcess.send({
context,
event,
Expand All @@ -63,12 +50,21 @@ export default class ChildProcessRunner {
let result

try {
result = await message
result = await new Promise((res, rej) => {
childProcess.on('message', (data) => {
if (data.error) {
rej(data.error)
return
}
res(data)
})
})
} catch (err) {
// TODO
log.error(err)

throw err
} finally {
childProcess.kill()
}

return result
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import assert from 'node:assert'
import { dirname, resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
import { setup, teardown } from '../../../_testHelpers/index.js'
import { BASE_URL } from '../../../config.js'

const __dirname = dirname(fileURLToPath(import.meta.url))

describe('run mode with child processes', function desc() {
beforeEach(() =>
setup({
servicePath: resolve(__dirname),
}),
)

afterEach(() => teardown())

it('should create a new lambda instance', async () => {
const url = new URL('/dev/foo', BASE_URL)

const responses = await Promise.all(
Array.from(Array(10).keys()).map(() => fetch(url)),
)

responses.forEach((response) => {
assert.equal(response.status, 200)
})

const jsons = await Promise.all(
responses.map((response) => response.json()),
)

jsons.forEach((json) => {
assert.deepEqual(json, 1)
})
})
})
26 changes: 26 additions & 0 deletions tests/lambda-run-mode/child-processes/instances/serverless.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
service: run-mode-tests

configValidationMode: error

plugins:
- ../../../../

custom:
serverless-offline:
useChildProcesses: true

provider:
memorySize: 128
name: aws
region: us-east-1 # default
runtime: nodejs16.x
stage: dev
versionFunctions: false

functions:
foo:
events:
- http:
method: get
path: foo
handler: src/handler.foo
20 changes: 20 additions & 0 deletions tests/lambda-run-mode/child-processes/instances/src/handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict'

const { promisify } = require('node:util')

const setTimeoutPromise = promisify(setTimeout)

const { stringify } = JSON

let counter = 0

exports.foo = async function foo() {
counter += 1

await setTimeoutPromise(1000, 'result')

return {
body: stringify(counter),
statusCode: 200,
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "commonjs"
}