8000 Add option to disable run multiple by abetomo · Pull Request #372 · motdotla/node-lambda · GitHub
[go: up one dir, main page]

Skip to content
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
3 changes: 3 additions & 0 deletions bin/node-lambda
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ const AWS_DLQ_TARGET_ARN = (() => {
return undefined
})()
const PROXY = process.env.PROXY || process.env.http_proxy || ''
const ENABLE_RUN_MULTIPLE_EVENTS = true

program
.command('deploy')
Expand Down Expand Up @@ -126,6 +127,8 @@ program
.option('-f, --configFile [' + CONFIG_FILE + ']',
'Path to file holding secret environment variables (e.g. "deploy.env")', CONFIG_FILE)
.option('-x, --contextFile [' + CONTEXT_FILE + ']', 'Context JSON File', CONTEXT_FILE)
.option('-M, --enableRunMultipleEvents [' + ENABLE_RUN_MULTIPLE_EVENTS + ']', 'Enable run multiple events',
ENABLE_RUN_MULTIPLE_EVENTS)
.action((prg) => lambda.run(prg))

program
Expand Down
12 changes: 9 additions & 3 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,17 @@ class Lambda {
const handler = require(path.join(process.cwd(), filename))[handlername]
const event = require(path.join(process.cwd(), program.eventFile))
const context = require(path.join(process.cwd(), program.contextFile))
const enableRunMultipleEvents = (() => {
if (typeof program.enableRunMultipleEvents === 'boolean') {
return program.enableRunMultipleEvents
}
return program.enableRunMultipleEvents === 'true'
})()

if (!Array.isArray(event)) {
return this._runHandler(handler, event, program, context)
if (Array.isArray(event) && enableRunMultipleEvents === true) {
return this._runMultipleHandlers(event)
}
this._runMultipleHandlers(event)
this._runHandler(handler, event, program, context)
}

_runHandler (handler, event, program, context) {
Expand Down
47 changes: 47 additions & 0 deletions test/node-lambda.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,53 @@ describe('bin/node-lambda', () => {
}, done)
})
})

describe('node-lambda run (disable Multiple events))', () => {
const eventObj = [{
asyncTest: false,
callbackWaitsForEmptyEventLoop: true,
callbackCode: 'callback(null);',
no: 1
}, {
asyncTest: false,
callbackWaitsForEmptyEventLoop: true,
callbackCode: 'callback(null);',
no: 2
}, {
asyncTest: false,
callbackWaitsForEmptyEventLoop: true,
callbackCode: 'callback(null);',
no: 3
}]

it('`node-lambda run` exitCode is `0`', function (done) {
this.timeout(10000) // give it time to multiple executions

_generateEventFile(eventObj)
const run = spawn('node', [
nodeLambdaPath, 'run',
'--handler', 'index.handler',
'--eventFile', 'event.json',
'-M', 'false'
])
let stdoutString = ''
run.stdout.on('data', (da 6D53 ta) => {
stdoutString += data.toString().replace(/\r|\n/g, '')
})

run.on('exit', (code) => {
const expected = 'Running index.handler==================================event ' +
'[ { asyncTest: false, callbackWaitsForEmptyEventLoop: true, callbackCode: \'callback(null);\', no: 1 }, ' +
'{ asyncTest: false, callbackWaitsForEmptyEventLoop: true, callbackCode: \'callback(null);\', no: 2 }, ' +
'{ asyncTest: false, callbackWaitsForEmptyEventLoop: true, callbackCode: \'callback(null);\', no: 3 } ]' +
'==================================Stopping index.handlerSuccess:'

assert.equal(stdoutString, expected)
assert.equal(code, 0)
done()
})
})
})
})

describe('node-lambda --version', () => {
Expand Down
0