/
outbound.test.ts
174 lines (148 loc) · 5.13 KB
/
outbound.test.ts
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import * as aws from 'aws-sdk';
import { invokeFunction } from '../../lib/provider-framework/runtime/outbound';
jest.mock('aws-sdk', () => {
return {
Lambda: class {
public invoke() {
return { promise: () => mockInvoke() };
}
public waitFor() {
return { promise: () => mockWaitFor() };
}
},
};
});
let mockInvoke: () => Promise<aws.Lambda.InvocationResponse>;
const req: aws.Lambda.InvocationRequest = {
FunctionName: 'Whatever',
Payload: {
IsThisATest: 'Yes, this is a test',
AreYouSure: 'Yes, I am sure',
},
};
let invokeCount: number = 0;
let expectedFunctionStates: string[] = [];
let receivedFunctionStates: string[] = [];
const mockWaitFor = async (): Promise<aws.Lambda.GetFunctionResponse> => {
let state = expectedFunctionStates.pop();
while (state !== 'Active') {
receivedFunctionStates.push(state!);
// If it goes back to inactive it's failed
if (state === 'Inactive') throw new Error('Not today');
// If failed... it's failed
if (state === 'Failed') throw new Error('Broken');
// If pending, continue the loop, no other valid options
if (state !== 'Pending') throw new Error('State is confused');
state = expectedFunctionStates.pop();
}
receivedFunctionStates.push(state);
return {
Configuration: {
State: 'Active',
},
};
};
describe('invokeFunction tests', () => {
afterEach(() => {
invokeCount = 0;
expectedFunctionStates = [];
receivedFunctionStates = [];
});
// Success cases
test('Inactive function that reactivates does not throw error', async () => {
mockInvoke = async () => {
if (invokeCount == 0) {
invokeCount++;
throw new Error('Better luck next time');
}
invokeCount++;
return { Payload: req.Payload };
};
expectedFunctionStates.push('Active');
expectedFunctionStates.push('Pending');
expect(await invokeFunction(req)).toEqual({ Payload: req.Payload });
expect(invokeCount).toEqual(2);
expect(receivedFunctionStates).toEqual(['Pending', 'Active']);
});
test('Active function does not run waitFor or retry invoke', async () => {
mockInvoke = async () => {
if (invokeCount == 1) {
invokeCount++;
throw new Error('This should not happen in this test');
}
invokeCount++;
return { Payload: req.Payload };
};
expectedFunctionStates.push('Active');
expect(await invokeFunction(req)).toEqual({ Payload: req.Payload });
expect(invokeCount).toEqual(1);
expect(receivedFunctionStates).toEqual([]);
});
// Failure cases
test('Inactive function that goes back to inactive throws error', async () => {
mockInvoke = async () => {
if (invokeCount == 0) {
invokeCount++;
throw new Error('Better luck next time');
}
invokeCount++;
return { Payload: req.Payload };
};
expectedFunctionStates.push('Inactive');
expectedFunctionStates.push('Pending');
expectedFunctionStates.push('Pending');
await expect(invokeFunction(req)).rejects.toThrowError(new Error('Not today'));
expect(invokeCount).toEqual(1);
expect(receivedFunctionStates).toEqual(['Pending', 'Pending', 'Inactive']);
});
test('Inactive function that goes to failed throws error', async () => {
mockInvoke = async () => {
if (invokeCount == 0) {
invokeCount++;
throw new Error('Better luck next time');
}
invokeCount++;
return { Payload: req.Payload };
};
expectedFunctionStates.push('Failed');
expectedFunctionStates.push('Pending');
expectedFunctionStates.push('Pending');
await expect(invokeFunction(req)).rejects.toThrowError(new Error('Broken'));
expect(invokeCount).toEqual(1);
expect(receivedFunctionStates).toEqual(['Pending', 'Pending', 'Failed']);
});
test('Inactive function that returns other value throws error', async () => {
mockInvoke = async () => {
if (invokeCount == 0) {
invokeCount++;
throw new Error('Better luck next time');
}
invokeCount++;
return { Payload: req.Payload };
};
expectedFunctionStates.push('NewFunctionWhoDis');
expectedFunctionStates.push('Pending');
expectedFunctionStates.push('Pending');
await expect(invokeFunction(req)).rejects.toThrowError(new Error('State is confused'));
expect(invokeCount).toEqual(1);
expect(receivedFunctionStates).toEqual(['Pending', 'Pending', 'NewFunctionWhoDis']);
});
test('Wait for stops on terminal responses', async () => {
mockInvoke = async () => {
if (invokeCount == 0) {
invokeCount++;
throw new Error('Better luck next time');
}
invokeCount++;
return { Payload: req.Payload };
};
expectedFunctionStates.push('SomethingElse');
expectedFunctionStates.push('Pending');
expectedFunctionStates.push('Inactive');
expectedFunctionStates.push('Pending');
expectedFunctionStates.push('Pending');
await expect(invokeFunction(req)).rejects.toThrowError(new Error('Not today'));
expect(invokeCount).toEqual(1);
expect(receivedFunctionStates).toEqual(['Pending', 'Pending', 'Inactive']);
});
});