|
| 1 | +import * as aws from 'aws-sdk'; |
| 2 | +import { invokeFunction } from '../../lib/provider-framework/runtime/outbound'; |
| 3 | + |
| 4 | +jest.mock('aws-sdk', () => { |
| 5 | + return { |
| 6 | + Lambda: class { |
| 7 | + public invoke() { |
| 8 | + return { promise: () => mockInvoke() }; |
| 9 | + } |
| 10 | + |
| 11 | + public waitFor() { |
| 12 | + return { promise: () => mockWaitFor() }; |
| 13 | + } |
| 14 | + }, |
| 15 | + }; |
| 16 | +}); |
| 17 | + |
| 18 | +let mockInvoke: () => Promise<aws.Lambda.InvocationResponse>; |
| 19 | + |
| 20 | +const req: aws.Lambda.InvocationRequest = { |
| 21 | + FunctionName: 'Whatever', |
| 22 | + Payload: { |
| 23 | + IsThisATest: 'Yes, this is a test', |
| 24 | + AreYouSure: 'Yes, I am sure', |
| 25 | + }, |
| 26 | +}; |
| 27 | + |
| 28 | +let invokeCount: number = 0; |
| 29 | +let expectedFunctionStates: string[] = []; |
| 30 | +let receivedFunctionStates: string[] = []; |
| 31 | + |
| 32 | +const mockWaitFor = async (): Promise<aws.Lambda.GetFunctionResponse> => { |
| 33 | + let state = expectedFunctionStates.pop(); |
| 34 | + while (state !== 'Active') { |
| 35 | + receivedFunctionStates.push(state!); |
| 36 | + // If it goes back to inactive it's failed |
| 37 | + if (state === 'Inactive') throw new Error('Not today'); |
| 38 | + // If failed... it's failed |
| 39 | + if (state === 'Failed') throw new Error('Broken'); |
| 40 | + // If pending, continue the loop, no other valid options |
| 41 | + if (state !== 'Pending') throw new Error('State is confused'); |
| 42 | + state = expectedFunctionStates.pop(); |
| 43 | + } |
| 44 | + receivedFunctionStates.push(state); |
| 45 | + return { |
| 46 | + Configuration: { |
| 47 | + State: 'Active', |
| 48 | + }, |
| 49 | + }; |
| 50 | +}; |
| 51 | + |
| 52 | +describe('invokeFunction tests', () => { |
| 53 | + afterEach(() => { |
| 54 | + invokeCount = 0; |
| 55 | + expectedFunctionStates = []; |
| 56 | + receivedFunctionStates = []; |
| 57 | + }); |
| 58 | + |
| 59 | + // Success cases |
| 60 | + test('Inactive function that reactivates does not throw error', async () => { |
| 61 | + mockInvoke = async () => { |
| 62 | + if (invokeCount == 0) { |
| 63 | + invokeCount++; |
| 64 | + throw new Error('Better luck next time'); |
| 65 | + } |
| 66 | + invokeCount++; |
| 67 | + return { Payload: req.Payload }; |
| 68 | + }; |
| 69 | + |
| 70 | + expectedFunctionStates.push('Active'); |
| 71 | + expectedFunctionStates.push('Pending'); |
| 72 | + |
| 73 | + expect(await invokeFunction(req)).toEqual({ Payload: req.Payload }); |
| 74 | + expect(invokeCount).toEqual(2); |
| 75 | + expect(receivedFunctionStates).toEqual(['Pending', 'Active']); |
| 76 | + }); |
| 77 | + |
| 78 | + test('Active function does not run waitFor or retry invoke', async () => { |
| 79 | + mockInvoke = async () => { |
| 80 | + if (invokeCount == 1) { |
| 81 | + invokeCount++; |
| 82 | + throw new Error('This should not happen in this test'); |
| 83 | + } |
| 84 | + invokeCount++; |
| 85 | + return { Payload: req.Payload }; |
| 86 | + }; |
| 87 | + |
| 88 | + expectedFunctionStates.push('Active'); |
| 89 | + |
| 90 | + expect(await invokeFunction(req)).toEqual({ Payload: req.Payload }); |
| 91 | + expect(invokeCount).toEqual(1); |
| 92 | + expect(receivedFunctionStates).toEqual([]); |
| 93 | + }); |
| 94 | + |
| 95 | + // Failure cases |
| 96 | + test('Inactive function that goes back to inactive throws error', async () => { |
| 97 | + mockInvoke = async () => { |
| 98 | + if (invokeCount == 0) { |
| 99 | + invokeCount++; |
| 100 | + throw new Error('Better luck next time'); |
| 101 | + } |
| 102 | + invokeCount++; |
| 103 | + return { Payload: req.Payload }; |
| 104 | + }; |
| 105 | + |
| 106 | + expectedFunctionStates.push('Inactive'); |
| 107 | + expectedFunctionStates.push('Pending'); |
| 108 | + expectedFunctionStates.push('Pending'); |
| 109 | + |
| 110 | + await expect(invokeFunction(req)).rejects.toThrowError(new Error('Not today')); |
| 111 | + expect(invokeCount).toEqual(1); |
| 112 | + expect(receivedFunctionStates).toEqual(['Pending', 'Pending', 'Inactive']); |
| 113 | + }); |
| 114 | + |
| 115 | + test('Inactive function that goes to failed throws error', async () => { |
| 116 | + mockInvoke = async () => { |
| 117 | + if (invokeCount == 0) { |
| 118 | + invokeCount++; |
| 119 | + throw new Error('Better luck next time'); |
| 120 | + } |
| 121 | + invokeCount++; |
| 122 | + return { Payload: req.Payload }; |
| 123 | + }; |
| 124 | + |
| 125 | + expectedFunctionStates.push('Failed'); |
| 126 | + expectedFunctionStates.push('Pending'); |
| 127 | + expectedFunctionStates.push('Pending'); |
| 128 | + |
| 129 | + await expect(invokeFunction(req)).rejects.toThrowError(new Error('Broken')); |
| 130 | + expect(invokeCount).toEqual(1); |
| 131 | + expect(receivedFunctionStates).toEqual(['Pending', 'Pending', 'Failed']); |
| 132 | + }); |
| 133 | + |
| 134 | + test('Inactive function that returns other value throws error', async () => { |
| 135 | + mockInvoke = async () => { |
| 136 | + if (invokeCount == 0) { |
| 137 | + invokeCount++; |
| 138 | + throw new Error('Better luck next time'); |
| 139 | + } |
| 140 | + invokeCount++; |
| 141 | + return { Payload: req.Payload }; |
| 142 | + }; |
| 143 | + |
| 144 | + expectedFunctionStates.push('NewFunctionWhoDis'); |
| 145 | + expectedFunctionStates.push('Pending'); |
| 146 | + expectedFunctionStates.push('Pending'); |
| 147 | + |
| 148 | + await expect(invokeFunction(req)).rejects.toThrowError(new Error('State is confused')); |
| 149 | + expect(invokeCount).toEqual(1); |
| 150 | + expect(receivedFunctionStates).toEqual(['Pending', 'Pending', 'NewFunctionWhoDis']); |
| 151 | + }); |
| 152 | + |
| 153 | + test('Wait for stops on terminal responses', async () => { |
| 154 | + mockInvoke = async () => { |
| 155 | + if (invokeCount == 0) { |
| 156 | + invokeCount++; |
| 157 | + throw new Error('Better luck next time'); |
| 158 | + } |
| 159 | + invokeCount++; |
| 160 | + return { Payload: req.Payload }; |
| 161 | + }; |
| 162 | + |
| 163 | + expectedFunctionStates.push('SomethingElse'); |
| 164 | + expectedFunctionStates.push('Pending'); |
| 165 | + expectedFunctionStates.push('Inactive'); |
| 166 | + expectedFunctionStates.push('Pending'); |
| 167 | + expectedFunctionStates.push('Pending'); |
| 168 | + |
| 169 | + await expect(invokeFunction(req)).rejects.toThrowError(new Error('Not today')); |
| 170 | + expect(invokeCount).toEqual(1); |
| 171 | + expect(receivedFunctionStates).toEqual(['Pending', 'Pending', 'Inactive']); |
| 172 | + }); |
| 173 | +}); |
| 174 | + |
0 commit comments