Skip to content

Commit

Permalink
Fix unhandled rejection warns
Browse files Browse the repository at this point in the history
  • Loading branch information
oguimbal committed Dec 10, 2020
1 parent 579e89c commit fdb4af5
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 26 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pg-mem",
"version": "1.4.2",
"version": "1.4.3",
"description": "A memory version of postgres",
"main": "index.js",
"scripts": {
Expand Down
25 changes: 15 additions & 10 deletions src/adapters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,22 @@ export class Adapters implements LibAdapters {
: valuesOrCallback;

const pgquery = this.adaptQuery(query, values);

return new Promise((done, err) => setTimeout(() => {
try {
const result = this.adaptResults(query, that.db.public.query(pgquery.text));
callback?.(null, result)
done(result);
} catch (e) {
callback(e);
err(e);
try {
const result = this.adaptResults(query, that.db.public.query(pgquery.text));
if (callback) {
setTimeout(() => callback(null, result), queryLatency ?? 0);
return null;
} else {
return new Promise(res => setTimeout(() => res(result), queryLatency ?? 0));
}
}, queryLatency ?? 0));
} catch (e) {
if (callback) {
setTimeout(() => callback(e), queryLatency ?? 0);
return null;
} else {
return new Promise((_, rej) => setTimeout(() => rej(e), queryLatency ?? 0));
}
}
}

private adaptResults(query: PgQuery, rows: QueryResult) {
Expand Down
62 changes: 48 additions & 14 deletions src/tests/irl-tests/mirrorbytes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import {
ManyToOne,
OneToMany,
} from 'typeorm';
import { typeOrm } from '../test-utils';
import { typeOrm, TypeormSetup } from '../test-utils';
import { DataType } from '../../interfaces';
import { v4 } from 'uuid';
import { typeormJoinsSample } from '../../../samples/typeorm/joins';
export abstract class External extends BaseEntity {
@PrimaryGeneratedColumn('uuid')
readonly id!: string;
Expand Down Expand Up @@ -73,24 +74,27 @@ export class Submission extends External {


describe('IRL tests', () => {
typeOrm('mirrobytes x typeorm', () => [User, Form, Submission], ({ mem }) => {
mem.registerExtension('uuid-ossp', (schema) => {

const setup: TypeormSetup = ({ mem }) => {
mem.registerExtension('uuid-ossp', (schema) => {
schema.registerFunction({
name: 'uuid_generate_v4',
returns: DataType.uuid,
implementation: v4,
name: 'uuid_generate_v4',
returns: DataType.uuid,
implementation: v4,
});
});
});

mem.registerExtension('citext', (schema) => {
mem.registerExtension('citext', (schema) => {
schema.registerFunction({
name: 'citext',
args: [DataType.text],
returns: DataType.text,
implementation: (arg: string) => arg.toLocaleLowerCase(),
name: 'citext',
args: [DataType.text],
returns: DataType.text,
implementation: (arg: string) => arg.toLocaleLowerCase(),
});
});
}, async () => {
});
};

typeOrm('mirrobytes x typeorm', () => [User, Form, Submission], setup, async () => {

// test creations
const user = await User.create({
Expand Down Expand Up @@ -127,4 +131,34 @@ describe('IRL tests', () => {
assert.exists(loaded_form);
expect(loaded.length).to.equal(1);
});



typeOrm('does not warns unhandled rejections', () => [User, Form, Submission], setup, async () => {
let unhandled = false;
const check = () => {
unhandled = true;
};
process.on('unhandledRejection', check);
try {
let threw = false;
try {
await User.find({ id: '' });
} catch (e) {
// nop
threw = true;
}

assert.isTrue(threw, 'Was expecting to throw...')

// just wait a bit until the unhandled exception has been logged
await new Promise(d => setTimeout(d, 1));

// this used to throw :(
assert.isFalse(unhandled, 'Unhandled exception raised !');

} finally {
process.off('unhandledRejection', check);
}
})
});
5 changes: 4 additions & 1 deletion src/tests/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,12 @@ interface TypeOrmTest {
one: (sql: string) => any;
none: (sql: string) => void;
}

export type TypeormSetup = ((mem: Omit<TypeOrmTest, 'db'>) => any) | null;

export async function typeOrm(title: string
, entities: () => Ctor<BaseEntity>[]
, setup: ((mem: Omit<TypeOrmTest, 'db'>) => any) | null
, setup: TypeormSetup
, fn: (data: TypeOrmTest) => Promise<any>) {
it(title, async () => {
const mem = newDb({
Expand Down

0 comments on commit fdb4af5

Please sign in to comment.