Skip to content

Commit 47337f6

Browse files
committed
feat(cucumber): use waiters in ec2 integ tests
1 parent 964ab42 commit 47337f6

File tree

1 file changed

+11
-34
lines changed
  • features/ec2/step_definitions

1 file changed

+11
-34
lines changed

features/ec2/step_definitions/ec2.js

Lines changed: 11 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,15 @@
1-
const { EC2 } = require("../../../clients/client-ec2");
1+
const { EC2, waitForVolumeAvailable } = require("../../../clients/client-ec2");
22
const { Before, Given, Then } = require("cucumber");
33

4-
/**
5-
* Waits for the volumeAvailable state by periodically calling the underlying
6-
* EC2.describeVolumes() operation every 5 seconds (at most 40 times)
7-
*/
8-
const waitForVolumeAvailable = (ec2, volumeId, callback) => {
9-
const maxAttempts = 40;
10-
let currentAttempt = 0;
11-
const delay = 5000;
12-
13-
const checkForVolumeAvailable = () => {
14-
currentAttempt++;
15-
ec2.describeVolumes({ VolumeIds: [volumeId] }, (err, data) => {
16-
if (currentAttempt > maxAttempts) {
17-
callback(new Error("waitForVolumeAvailable: max attempts exceeded"));
18-
} else if (data && data.Volumes) {
19-
if (data.Volumes[0].State === "available") {
20-
callback();
21-
} else if (data.Volumes[0].State === "deleted") {
22-
callback(new Error(`VolumeId ${data.Volumes[i].VolumeId} is in failure state`));
23-
} else {
24-
setTimeout(function () {
25-
checkForVolumeAvailable();
26-
}, delay);
27-
}
28-
} else {
29-
setTimeout(function () {
30-
checkForVolumeAvailable();
31-
}, delay);
32-
}
33-
});
34-
};
35-
checkForVolumeAvailable();
4+
const waitForVolumeAvailableCallback = (ec2, volumeId, callback) => {
5+
waitForVolumeAvailable({ client: ec2 }, { VolumeIds: [volumeId] }).then(
6+
function (data) {
7+
callback();
8+
},
9+
function (err) {
10+
callback(err);
11+
}
12+
);
3613
};
3714

3815
Before({ tags: "@ec2" }, function (scenario, callback) {
@@ -82,7 +59,7 @@ Given("I attempt to copy an encrypted snapshot across regions", function (callba
8259
}
8360
volId = data.VolumeId;
8461

85-
waitForVolumeAvailable(srcEc2, volId, function (err) {
62+
waitForVolumeAvailableCallback(srcEc2, volId, function (err) {
8663
if (err) {
8764
teardown();
8865
return callback(err);

0 commit comments

Comments
 (0)