forked from aws/aws-lambda-nodejs-runtime-interface-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStreamingContextTest.js
123 lines (106 loc) · 3.38 KB
/
StreamingContextTest.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
/**
* Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*/
'use strict';
require('should');
const StreamingContext = require('lambda-runtime/StreamingContext.js');
const { PassThrough } = require('stream');
const BeforeExitListener = require('lambda-runtime/BeforeExitListener.js');
class MockRapidClient {
constructor() {
this.sentErrors = [];
this.responseStream = new PassThrough();
this.responseStream.fail = (err, callback) => {
this.sentErrors.push({ err });
callback();
};
}
getStreamForInvocationResponse() {
return { request: this.responseStream, responseDone: undefined };
}
}
describe('StreamingContext', () => {
it('can set callbackWaitsForEmptyEventLoop', () => {
const ctx = StreamingContext.build();
ctx.callbackWaitsForEmptyEventLoop = true;
ctx.callbackWaitsForEmptyEventLoop.should.be.equal(true);
ctx.callbackWaitsForEmptyEventLoop = false;
ctx.callbackWaitsForEmptyEventLoop.should.be.equal(false);
});
it('can create stream', () => {
const id = '12-3-4-56';
const client = new MockRapidClient();
const ctx = StreamingContext.build(
client,
id,
() => {},
JSON.stringify({}),
);
const stream = ctx.createStream();
should(stream).not.be.empty();
});
it('cannot create stream more than once', () => {
const id = '12-3-4-56';
const client = new MockRapidClient();
const ctx = StreamingContext.build(
client,
id,
() => {},
JSON.stringify({}),
);
const stream = ctx.createStream();
should(stream).not.be.empty();
for (let i = 0; i < 5; i++) {
should(() => ctx.createStream()).throw({
message:
'Cannot create stream for the same StreamingContext more than once.',
});
}
});
[true, false].forEach((callbackWaitsForEmptyEventLoop) =>
[
{
error: new Error('too much sun'),
expected: 'too much sun',
},
{
error: 'too much sun',
expected: 'too much sun',
},
].forEach((v) =>
it(`can call next after fail (callbackWaitsForEmptyEventLoop: ${callbackWaitsForEmptyEventLoop}, error: ${typeof v.error})`, () => {
// This test will print "Invoke Error" to stderr which is to be expected.
let nextCalled = 0;
const ID = '12-3-4-56';
const client = new MockRapidClient();
const ctx = StreamingContext.build(
client,
ID,
() => nextCalled++,
JSON.stringify({}),
);
ctx.callbackWaitsForEmptyEventLoop = callbackWaitsForEmptyEventLoop;
const { scheduleNext, fail } = ctx.createStream();
fail(v.error, scheduleNext);
client.responseStream.fail(v.error, scheduleNext);
const verify = () => {
nextCalled.should.be.equal(1);
console.log('client.sentErrors', client.sentErrors);
console.log('client.invocationErrors', client.invocationErrors);
client.sentErrors.length.should.be.equal(1);
if (typeof v.error === 'string') {
client.sentErrors[0].err.should.be.equal(v.expected);
} else {
client.sentErrors[0].err.message.should.be.equal(v.expected);
}
};
if (v) {
BeforeExitListener.invoke();
setImmediate(() => verify());
} else {
verify();
}
}),
),
);
});