Skip to content

Commit

Permalink
Forward error to done()
Browse files Browse the repository at this point in the history
  • Loading branch information
johanneswuerbach committed Jan 14, 2019
1 parent eb771b9 commit eb3cd7b
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 4 deletions.
4 changes: 2 additions & 2 deletions lib/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ function poolConnect(ctx, db, config) {
resolve({
client,
useCount: client.$useCount,
done() {
done(err) {
client.end = end;
done();
done(err);
npm.events.disconnect(ctx, client);
client.removeListener('error', onError);
}
Expand Down
5 changes: 3 additions & 2 deletions lib/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ function Database(cn, dc, config) {
})
.catch(error => {
ctx.disconnect();

return $p.reject(error);
});
};
Expand Down Expand Up @@ -1252,7 +1253,7 @@ function Database(cn, dc, config) {
*
* db.task('my-task', t => {
* // t.ctx = task context object
*
*
* return t.one('SELECT id FROM Users WHERE name = $1', 'John')
* .then(user => {
* return t.any('SELECT * FROM Events WHERE userId = $1', user.id);
Expand Down Expand Up @@ -1413,7 +1414,7 @@ function Database(cn, dc, config) {
*
* db.tx('my-transaction', t => {
* // t.ctx = transaction context object
*
*
* return t.one('INSERT INTO Users(name, age) VALUES($1, $2) RETURNING id', ['Mike', 25])
* .then(user => {
* return t.batch([
Expand Down
58 changes: 58 additions & 0 deletions test/dbSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,64 @@ describe('Connection', () => {
expect(error).toEqual(new Error($text.poolDestroyed));
});
});

describe('db side closing of the connection pool', () => {
const singleCN = JSON.parse(JSON.stringify(dbHeader.cn)); // dumb connection cloning;
singleCN.max = 1;
const dbSingleCN = pgp(singleCN);

let error;

beforeEach(done => {
dbSingleCN.connect()
.then(obj => {

const q = obj.any('SELECT pg_sleep(1);');

// Terminate all connections after a short delay
promise.delay(100).then(() => {
return db.query(
'SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE pid <> pg_backend_pid();'
);
})
.then(() => {
return q;
})
.catch(reason => {
error = reason;
})
.finally(() => {
obj.done();
done();
});
});
});
it('returns the postgres error', () => {
expect(error instanceof Error).toBe(true);
expect(error.code).toEqual('57P01');
expect(error.message).toEqual('terminating connection due to administrator command');
});

it('releases the client from the pool', (done) => {
let result;

dbSingleCN.query('SELECT \'1\';')
.then((data) => {
result = data;
})
.catch(reason => {
error = reason;
})
.then(() => {
expect(error).toBeNull();
expect(isResult(result)).toBe(true);
expect(result.rows.length > 0).toBe(true);

done();
});

});
});
});

describe('Direct Connection', () => {
Expand Down

2 comments on commit eb3cd7b

@vitaly-t
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just removed the tests completely, because they are unreliable. But left the feature in.

@johanneswuerbach
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we are also still getting various errors so the error handling is still not working 100%.

Examples

image

and double releases with brianc/node-pg-pool#115 applied
image

Please sign in to comment.