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 timing to check Runtime #310

Merged
merged 4 commits into from
Jun 8, 2017
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
33 changes: 17 additions & 16 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,28 +45,33 @@ Lambda.prototype.setup = function (program) {
}

Lambda.prototype.run = function (program) {
if (['nodejs4.3', 'nodejs6.10'].indexOf(program.runtime) === -1) {
console.error(`Runtime [${program.runtime}] is not supported.`)
process.exit(254)
}

this._createSampleFile(program.eventFile, 'event.json')
var splitHandler = program.handler.split('.')
var filename = splitHandler[0] + '.js'
var handlername = splitHandler[1]
const splitHandler = program.handler.split('.')
const filename = splitHandler[0] + '.js'
const handlername = splitHandler[1]

// Set custom environment variables if program.configFile is defined
if (program.configFile) {
this._setRunTimeEnvironmentVars(program)
}

var handler = require(path.join(process.cwd(), filename))[handlername]
var event = require(path.join(process.cwd(), program.eventFile))
var context = require(path.join(process.cwd(), program.contextFile))
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))

this._runHandler(handler, event, program, context)
}

Lambda.prototype._runHandler = function (handler, event, program, context) {
var startTime = new Date()
var timeout = Math.min(program.timeout, 300) * 1000 // convert the timeout into milliseconds
Lambda.prototype._runHandler = (handler, event, program, context) => {
const startTime = new Date()
const timeout = Math.min(program.timeout, 300) * 1000 // convert the timeout into milliseconds

var callback = function (err, result) {
const callback = (err, result) => {
if (err) {
process.exitCode = 255
console.log('Error: ' + err)
Expand All @@ -82,15 +87,11 @@ Lambda.prototype._runHandler = function (handler, event, program, context) {
}
}

context.getRemainingTimeInMillis = function () {
var currentTime = new Date()
context.getRemainingTimeInMillis = () => {
const currentTime = new Date()
return timeout - (currentTime - startTime)
}

if (['nodejs4.3', 'nodejs6.10'].indexOf(program.runtime) === -1) {
console.error(`Runtime [${program.runtime}] is not supported.`)
process.exit(254)
}
handler(event, context, callback)
}

Expand Down
35 changes: 34 additions & 1 deletion test/node-lambda.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,21 @@ describe('bin/node-lambda', () => {
'--eventFile', 'event.json'
])
var stdoutString = ''
var stderrString = ''
run.stdout.on('data', (data) => {
stdoutString += data.toString().replace(/\r|\n/g, '')
})
run.stderr.on('data', (data) => {
stderrString += data.toString().replace(/\r|\n/g, '')
})

run.on('exit', (code) => {
assert.match(stdoutString, expectedValues.stdoutRegExp)
if (expectedValues.stdoutRegExp) {
assert.match(stdoutString, expectedValues.stdoutRegExp)
}
if (expectedValues.stderrRegExp) {
assert.match(stderrString, expectedValues.stderrRegExp)
}
assert.equal(code, expectedValues.exitCode)
done()
})
Expand Down Expand Up @@ -136,5 +145,29 @@ describe('bin/node-lambda', () => {
})
})
})

describe('node-lambda run (Runtime is not supported)', () => {
const eventObj = {
asyncTest: false,
callbackWaitsForEmptyEventLoop: true // True is the default value of Lambda
}

before(() => {
process.env.AWS_RUNTIME = 'test'
})
after(() => {
process.env.AWS_RUNTIME = 'nodejs6.10'
})

it('`node-lambda run` exitCode is `254` (callback(null))', (done) => {
_generateEventFile(Object.assign(eventObj, {
callbackCode: 'callback(null);'
}))
_testMain({
stderrRegExp: /^Runtime \[test\] is not supported\.$/,
exitCode: 254
}, done)
})
})
})
})