Skip to content

Commit

Permalink
perf: wait for local ddb to be ready instead of waiting for 1s
Browse files Browse the repository at this point in the history
  • Loading branch information
vladholubiev committed Aug 18, 2021
1 parent 6ea115d commit fb6d175
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 8 deletions.
4 changes: 2 additions & 2 deletions setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ module.exports = async function () {
try {
const {TableNames: tableNames} = await Promise.race([
dynamoDB.listTables({}),
new Promise(resolve => setTimeout(resolve, 1000))
waitForLocalhost(DEFAULT_PORT)
]);
await deleteTables(dynamoDB, tableNames); // cleanup leftovers
} catch (err) {
Expand All @@ -54,7 +54,7 @@ module.exports = async function () {

global.__DYNAMODB__ = await DynamoDbLocal.launch(port, null, options);

await waitForLocalhost({port: DEFAULT_PORT, useGet: true, path: '/'});
await waitForLocalhost(DEFAULT_PORT);
}
}

Expand Down
8 changes: 2 additions & 6 deletions wait-for-localhost.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,12 @@

const http = require('http');

const waitForLocalhost = options => {
options = {...options};

const waitForLocalhost = port => {
return new Promise(resolve => {
const retry = () => setTimeout(main, 200);

const method = options.useGet ? 'GET' : 'HEAD';

const main = () => {
const request = http.request({method, port: options.port, path: options.path}, response => {
const request = http.request({method: 'GET', port, path: '/'}, response => {
if (response.statusCode === 400) {
return resolve();
}
Expand Down

0 comments on commit fb6d175

Please sign in to comment.