Skip to content

Commit 1738921

Browse files
committed
Rework dynamodb-stream integration tests 🔧
1 parent 178b52c commit 1738921

File tree

1 file changed

+39
-80
lines changed

1 file changed

+39
-80
lines changed

tests/serverless-plugins-integration/test-dynamodb-streams.js

Lines changed: 39 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -10,65 +10,39 @@ const client = new DynamoDB({
1010
endpoint: 'http://localhost:8000'
1111
});
1212

13-
const putItems = async () => {
14-
// Dynamodb-local doesn't create stream until table isn't empty
15-
await Promise.all([
16-
client
17-
.putItem({
18-
Item: {id: {S: `Bug`}},
19-
TableName: 'MyFirstTable'
20-
})
21-
.promise(),
22-
client
23-
.putItem({
24-
Item: {id: {S: `Bug`}},
25-
TableName: 'MySecondTable'
26-
})
27-
.promise(),
28-
client
29-
.putItem({
30-
Item: {id: {S: `Bug`}},
31-
TableName: 'MyThirdTable'
32-
})
33-
.promise(),
34-
client
35-
.putItem({
36-
Item: {id: {S: `Bug`}},
37-
TableName: 'MyFourthTable'
38-
})
39-
.promise()
40-
]);
41-
// wait stream get a new iterator
13+
// Dynamodb-local doesn't create stream until table isn't empty
14+
const unemptyTables = () =>
15+
Promise.all(
16+
['MyFirstTable', 'MySecondTable', 'MyThirdTable', 'MyFourthTable'].map(TableName =>
17+
client
18+
.putItem({
19+
Item: {id: {S: 'Stub'}},
20+
TableName
21+
})
22+
.promise()
23+
)
24+
);
25+
26+
const putItems = () =>
27+
Promise.all(
28+
['First', 'Second', 'Third', 'Fourth'].map(order =>
29+
client
30+
.putItem({
31+
Item: {id: {S: `My${order}Id`}},
32+
TableName: `My${order}Table`
33+
})
34+
.promise()
35+
)
36+
);
37+
38+
let setupInProgress = true;
39+
const populateTables = async () => {
40+
await unemptyTables();
4241
await new Promise(resolve => {
43-
setTimeout(resolve, 1000);
42+
setTimeout(resolve, 1200);
4443
});
45-
46-
await Promise.all([
47-
client
48-
.putItem({
49-
Item: {id: {S: `MyFirstId`}},
50-
TableName: 'MyFirstTable'
51-
})
52-
.promise(),
53-
client
54-
.putItem({
55-
Item: {id: {S: `MySecondId`}},
56-
TableName: 'MySecondTable'
57-
})
58-
.promise(),
59-
client
60-
.putItem({
61-
Item: {id: {S: `MyThirdId`}},
62-
TableName: 'MyThirdTable'
63-
})
64-
.promise(),
65-
client
66-
.putItem({
67-
Item: {id: {S: `MyFourthId`}},
68-
TableName: 'MyFourthTable'
69-
})
70-
.promise()
71-
]);
44+
setupInProgress = false;
45+
await putItems();
7246
};
7347

7448
const serverless = spawn(
@@ -81,43 +55,28 @@ const serverless = spawn(
8155
);
8256

8357
const set = new Set();
58+
let invocationCount = 0;
8459
serverless.stdout.pipe(
8560
new Writable({
8661
write(chunk, enc, cb) {
8762
const output = chunk.toString();
8863

8964
if (/Starting Offline Dynamodb Streams/.test(output)) {
90-
putItems();
65+
populateTables(); // will run in the background
9166
}
9267

68+
if (setupInProgress) return cb(); // do not consider lambda executions before we post the real items
69+
9370
const matches = /offline: \(λ: (.*)\) RequestId: .* Duration: .* ms {2}Billed Duration: .* ms/g.exec(
9471
output
9572
);
9673

97-
if (matches) set.add(matches[1]);
98-
99-
if (set.size === 4) serverless.kill();
100-
cb();
101-
}
102-
})
103-
);
104-
serverless.stdout.pipe(
105-
new Writable({
106-
write(chunk, enc, cb) {
107-
const output = chunk.toString();
108-
109-
if (/Starting Offline Dynamodb Streams/.test(output)) {
110-
putItems();
74+
if (matches) {
75+
invocationCount++;
76+
set.add(matches[1]);
11177
}
11278

113-
this.count =
114-
(this.count || 0) +
115-
(
116-
output.match(
117-
/offline: \(λ: .*\) RequestId: .* Duration: .* ms {2}Billed Duration: .* ms/g
118-
) || []
119-
).length;
120-
if (this.count === 4) serverless.kill();
79+
if (set.size === 3 && invocationCount === 4) serverless.kill(); // myPromiseHandler is mapped to two tables
12180
cb();
12281
}
12382
})

0 commit comments

Comments
 (0)