Skip to content

Commit

Permalink
fix(amplify-dynamodb-simulator): detect errors logged to stderr (#6769)
Browse files Browse the repository at this point in the history
fix: This commit updates the dynamodb simulator to detect errors logged to stderr by the Java process. Currently, only a single error type is detected, but others could be added as well.
  • Loading branch information
cjihrig committed Mar 2, 2021
1 parent b82d630 commit 72b7c0a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
5 changes: 5 additions & 0 deletions packages/amplify-dynamodb-simulator/__test__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,9 @@ describe('emulator operations', () => {
emulators.push(emu);
expect(emu.port).toBe(port);
});

it('reports on invalid dbPath values', async () => {
expect.assertions(1);
await expect(ddbSimulator.launch({ dbPath: 'dynamodb-data' })).rejects.toThrow('invalid directory for database creation');
});
});
36 changes: 29 additions & 7 deletions packages/amplify-dynamodb-simulator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,19 +144,39 @@ async function launch(givenOptions = {}, retry = 0, startTime = Date.now()) {

// define this now so we can use it later to remove a listener.
let prematureExit;
let waiter;
// This is a fairly complex set of logic to retry starting
// the emulator if it fails to start. We need this logic due
// to possible race conditions between when we find an open
// port and bind to it. This situation is particularly common
// in jest where each test file is running in it's own process
// each competing for the open port.
try {
const waiter = wait(opts.startTimeout);
waiter = wait(opts.startTimeout);
await Promise.race([
new Promise(accept => {
new Promise((accept, reject) => {
let stdout = '';
let stderr = '';

function readStderrBuffer(buffer) {
stderr += buffer.toString();

// Check stderr for any known errors.
if (/^Invalid directory for database creation.$/.test(stderr)) {
proc.stdout.removeListener('data', readStdoutBuffer);
proc.stderr.removeListener('data', readStderrBuffer);
const err = new Error('invalid directory for database creation');
err.code = 'bad_config';
reject(err);
}
}

function readStdoutBuffer(buffer) {
if (buffer.toString().indexOf(opts.port) !== -1) {
stdout += buffer.toString();

if (stdout.indexOf(opts.port) !== -1) {
proc.stdout.removeListener('data', readStdoutBuffer);
proc.stderr.removeListener('data', readStderrBuffer);
log.info('Emulator has started but need to verify socket');
accept(
waitPort({
Expand All @@ -167,6 +187,7 @@ async function launch(givenOptions = {}, retry = 0, startTime = Date.now()) {
);
}
}
proc.stderr.on('data', readStderrBuffer);
proc.stdout.on('data', readStdoutBuffer);
}),
waiter.promise.then(startingTimeout),
Expand All @@ -181,10 +202,6 @@ async function launch(givenOptions = {}, retry = 0, startTime = Date.now()) {
proc.on('exit', prematureExit);
}),
]);
// eventually the process will exit... ensure our logic only
// will run on _premature_ exits.
proc.removeListener('exit', prematureExit);
waiter.cancel();

log.info('Successfully launched emulator on', {
port,
Expand All @@ -201,6 +218,11 @@ async function launch(givenOptions = {}, retry = 0, startTime = Date.now()) {
return wait(retryInterval).promise.then(() => launch(givenOptions, retry + 1, startTime));
}
throw err;
} finally {
waiter && waiter.cancel();
if (typeof prematureExit === 'function') {
proc.removeListener('exit', prematureExit);
}
}

return new Emulator(proc, opts);
Expand Down

0 comments on commit 72b7c0a

Please sign in to comment.