Skip to content

Commit 7e31af4

Browse files
author
Chris Armstrong
committed
fix: timeout in startup tests
1 parent a32e4d7 commit 7e31af4

File tree

2 files changed

+55
-12
lines changed

2 files changed

+55
-12
lines changed

test/indexTest.js

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,55 @@ const Plugin = require("../index.js");
1010

1111
const serverlessMock = require("./serverlessMock");
1212

13+
function get(url) {
14+
return new Promise(function(resolve, reject) {
15+
http.get(url, function(incoming) {
16+
resolve(incoming);
17+
}).on('error', reject);
18+
});
19+
}
20+
21+
function getWithRetry(url, retryCount, previousError) {
22+
retryCount = retryCount || 0;
23+
if (retryCount >= 3) {
24+
return Promise.reject('Exceeded retry count for get of ' + url, previousError);
25+
}
26+
return get(url)
27+
.catch(function(error) {
28+
console.error('error getting ' + url + ', retrying');
29+
return new Promise(function(resolve) { setTimeout(resolve, 1000); })
30+
.then(function() {
31+
return getWithRetry(url, retryCount + 1, error);
32+
});
33+
});
34+
}
35+
1336
describe("Port function",function(){
37+
let service;
38+
before(function(){
39+
this.timeout(60000);
40+
service = new Plugin(serverlessMock, { stage: 'test' });
41+
return service.installHandler();
42+
})
43+
1444
it("Port should return number",function(){
15-
let service = new Plugin(serverlessMock, {});
1645
assert(typeof service.port, "number");
1746
});
1847

19-
it("Port value should be >= 0 and < 65536",function(done){
20-
let service = new Plugin(serverlessMock, {});
21-
http.get(`http://localhost:${service.port}/shell/`, function (response) {
22-
assert.equal(response.statusCode, 200);
23-
done();
24-
});
48+
it("Port value should be >= 0 and < 65536",function() {
49+
this.timeout(10000);
50+
return service.startHandler()
51+
.then(function() {
52+
console.log('started handler');
53+
return getWithRetry(`http://localhost:${service.port}/shell/`);
54+
})
55+
.then(function(response){
56+
assert.equal(response.statusCode, 200);
57+
});
58+
});
59+
60+
after(function(){
61+
return service.endHandler();
2562
});
2663
});
2764

@@ -41,7 +78,7 @@ describe("Check the dynamodb function",function(){
4178
let raw = new aws.DynamoDB(dynamoOptions);
4279
raw.should.be.type("object");
4380
});
44-
81+
4582
it("Should be an object",function(){
4683
let dynamoOptions = Plugin.prototype.dynamodbOptions;
4784
let doc = new aws.DynamoDB(dynamoOptions);
@@ -62,7 +99,7 @@ describe ("createTable functon",function(){
6299
const tbl = Plugin.prototype.createTable;
63100
assert.equal(typeof tbl, "function");
64101
});
65-
});
102+
});
66103

67104
describe ("Check the Seeder file",function(){
68105
it("Table name shoud be a string",function(){

test/serverlessMock.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
module.exports = {
2-
service: {},
2+
service: {
3+
custom: {
4+
dynamodb: {
5+
stages: ['test']
6+
}
7+
}
8+
},
39
cli: {
4-
log: () => {}
10+
log: (...args) => { console.log(...args); }
511
},
6-
custom: {}
12+
custom: {},
713
};

0 commit comments

Comments
 (0)