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

Replace asyncjs of _updateEventSources with Promise #316

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
37 changes: 23 additions & 14 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -614,11 +614,11 @@ Lambda.prototype._listEventSourceMappings = function (lambda, params, cb) {
})
}

Lambda.prototype._updateEventSources = function (lambda, functionName, existingEventSourceList, eventSourceList, cb) {
Lambda.prototype._updateEventSources = (lambda, functionName, existingEventSourceList, eventSourceList, cb) => {
if (eventSourceList == null) {
return cb(null, [])
}
var updateEventSourceList = []
const updateEventSourceList = []
// Checking new and update event sources
for (let i in eventSourceList) {
let isExisting = false
Expand Down Expand Up @@ -668,29 +668,38 @@ Lambda.prototype._updateEventSources = function (lambda, functionName, existingE
}
}

return async.map(updateEventSourceList, function (updateEventSource, _cb) {
return Promise.all(updateEventSourceList.map((updateEventSource) => {
switch (updateEventSource['type']) {
case 'create':
delete updateEventSource['type']
lambda.createEventSourceMapping(updateEventSource, function (err, data) {
return _cb(err, data)
return new Promise((resolve, reject) => {
lambda.createEventSourceMapping(updateEventSource, (err, data) => {
if (err) return reject(err)
resolve(data)
})
})
break
case 'update':
delete updateEventSource['type']
lambda.updateEventSourceMapping(updateEventSource, function (err, data) {
return _cb(err, data)
return new Promise((resolve, reject) => {
lambda.updateEventSourceMapping(updateEventSource, (err, data) => {
if (err) return reject(err)
resolve(data)
})
})
break
case 'delete':
delete updateEventSource['type']
lambda.deleteEventSourceMapping(updateEventSource, function (err, data) {
return _cb(err, data)
return new Promise((resolve, reject) => {
lambda.deleteEventSourceMapping(updateEventSource, (err, data) => {
if (err) return reject(err)
resolve(data)
})
})
break
}
}, function (err, results) {
return cb(err, results)
return Promise.resolve()
})).then((data) => {
cb(null, data)
}).catch((err) => {
cb(err)
})
}

Expand Down
78 changes: 45 additions & 33 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -874,50 +874,62 @@ describe('lib/main', function () {
)
})

it('simple test with mock (In case of new addition)', (done) => {
it('simple test with mock (In case of new addition)', () => {
program.eventSourceFile = 'event_sources.json'
const eventSourceList = lambda._eventSourceList(program)
lambda._updateEventSources(
awsLambda,
'functionName',
[],
eventSourceList.EventSourceMappings,
(err, results) => {
assert.isUndefined(err)
assert.deepEqual(results, [lambdaMockSettings.createEventSourceMapping])
done()
return new Promise((resolve) => {
lambda._updateEventSources(
awsLambda,
'functionName',
[],
eventSourceList.EventSourceMappings,
(err, results) => resolve({ err: err, results: results })
)
}).then((actual) => {
const expected = {
err: null,
results: [lambdaMockSettings.createEventSourceMapping]
}
)
assert.deepEqual(actual, expected)
})
})

it('simple test with mock (In case of deletion)', (done) => {
lambda._updateEventSources(
awsLambda,
'functionName',
lambdaMockSettings.listEventSourceMappings.EventSourceMappings,
{},
(err, results) => {
assert.isUndefined(err)
assert.deepEqual(results, [lambdaMockSettings.deleteEventSourceMapping])
done()
it('simple test with mock (In case of deletion)', () => {
return new Promise((resolve) => {
lambda._updateEventSources(
awsLambda,
'functionName',
lambdaMockSettings.listEventSourceMappings.EventSourceMappings,
{},
(err, results) => resolve({ err: err, results: results })
)
}).then((actual) => {
const expected = {
err: null,
results: [lambdaMockSettings.deleteEventSourceMapping]
}
)
assert.deepEqual(actual, expected)
})
})

it('simple test with mock (In case of update)', (done) => {
it('simple test with mock (In case of update)', () => {
program.eventSourceFile = 'event_sources.json'
const eventSourceList = lambda._eventSourceList(program)
lambda._updateEventSources(
awsLambda,
'functionName',
lambdaMockSettings.listEventSourceMappings.EventSourceMappings,
eventSourceList.EventSourceMappings,
(err, results) => {
assert.isUndefined(err)
assert.deepEqual(results, [lambdaMockSettings.updateEventSourceMapping])
done()
return new Promise((resolve) => {
lambda._updateEventSources(
awsLambda,
'functionName',
lambdaMockSettings.listEventSourceMappings.EventSourceMappings,
eventSourceList.EventSourceMappings,
(err, results) => resolve({ err: err, results: results })
)
}).then((actual) => {
const expected = {
err: null,
results: [lambdaMockSettings.updateEventSourceMapping]
}
)
assert.deepEqual(actual, expected)
})
})
})

Expand Down