/
lambdaFunction.fixture.js
145 lines (117 loc) · 3.56 KB
/
lambdaFunction.fixture.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
'use strict'
exports.contextDoneHandler = function contextDoneHandler(event, context) {
context.done(null, 'foo')
}
exports.contextDoneHandlerDeferred = function contextDoneHandlerDeferred(
event,
context,
) {
setTimeout(() => context.done(null, 'foo'), 100)
}
exports.contextSucceedHandler = function contextSucceedHandler(event, context) {
context.succeed('foo')
}
exports.contextSucceedHandlerDeferred = function contextSucceedHandlerDeferred(
event,
context,
) {
setTimeout(() => context.succeed('foo'), 100)
}
exports.callbackHandler = function callbackHandler(event, context, callback) {
callback(null, 'foo')
}
exports.callbackHandlerDeferred = function callbackHandlerDeferred(
event,
context,
callback,
) {
setTimeout(() => callback(null, 'foo'), 100)
}
exports.promiseHandler = function promiseHandler() {
return Promise.resolve('foo')
}
exports.promiseHandlerDeferred = function promiseDeferred() {
return new Promise((resolve) => {
setTimeout(() => resolve('foo'), 100)
})
}
exports.asyncFunctionHandler = async function asyncFunctionHandler() {
return 'foo'
}
exports.asyncFunctionHandlerObject = async function asyncFunctionHandler() {
return {
foo: 'bar',
}
}
// we deliberately test the case where a 'callback' is defined
// in the handler, but a promise is being returned to protect from a
// potential naive implementation, e.g.
//
// const { promisify } = 'utils'
// const promisifiedHandler = handler.length === 3 ? promisify(handler) : handler
//
// if someone would return a promise, but also defines callback, without using it
// the handler would not be returning anything
exports.promiseWithDefinedCallbackHandler =
function promiseWithDefinedCallbackHandler(
event, // eslint-disable-line no-unused-vars
context, // eslint-disable-line no-unused-vars
callback, // eslint-disable-line no-unused-vars
) {
return Promise.resolve('Hello Promise!')
}
exports.contextSucceedWithContextDoneHandler =
function contextSucceedWithContextDoneHandler(event, context) {
context.succeed('Hello Context.succeed!')
context.done(null, 'Hello Context.done!')
}
exports.callbackWithContextDoneHandler =
function callbackWithContextDoneHandler(event, context, callback) {
callback(null, 'Hello Callback!')
context.done(null, 'Hello Context.done!')
}
exports.callbackWithPromiseHandler = function callbackWithPromiseHandler(
event,
context,
callback,
) {
callback(null, 'Hello Callback!')
return Promise.resolve('Hello Promise!')
}
exports.callbackInsidePromiseHandler = function callbackInsidePromiseHandler(
event,
context,
callback,
) {
return new Promise((resolve) => {
callback(null, 'Hello Callback!')
resolve('Hello Promise!')
})
}
exports.requestIdHandler = async function requestIdHandler(event, context) {
return context.awsRequestId
}
exports.remainingExecutionTimeHandler =
async function remainingExecutionTimeHandler(event, context) {
const first = context.getRemainingTimeInMillis()
await new Promise((resolve) => {
setTimeout(resolve, 100)
})
const second = context.getRemainingTimeInMillis()
await new Promise((resolve) => {
setTimeout(resolve, 200)
})
const third = context.getRemainingTimeInMillis()
return [first, second, third]
}
exports.defaultTimeoutHandler = async function defaultTimeoutHandler(
event,
context,
) {
return context.getRemainingTimeInMillis()
}
exports.executionTimeInMillisHandler = function executionTimeInMillisHandler() {
return new Promise((resolve) => {
setTimeout(resolve, 100)
})
}