2017-11-17 21:03:43 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const tape = require('tape')
|
|
|
|
const _ = require('lodash');
|
|
|
|
const makeKnex = require('../../knex')
|
|
|
|
const Bluebird = require('bluebird');
|
|
|
|
|
|
|
|
module.exports = (knexfile) => {
|
|
|
|
Object.keys(knexfile).forEach((key) => {
|
|
|
|
const dialect = knexfile[key].dialect || knexfile[key].client;
|
|
|
|
|
2018-06-29 10:47:06 +03:00
|
|
|
// TODO: FIX ORACLE AND MSSQL TO WORK THE SAME WAY WITH OTHER DIALECTS IF POSSIBLE
|
|
|
|
if (dialect !== 'sqlite3' && dialect !== 'oracledb' && dialect !== 'mssql') {
|
2017-11-17 21:03:43 +02:00
|
|
|
const knexConf = _.cloneDeep(knexfile[key]);
|
2018-02-07 11:17:17 +02:00
|
|
|
knexConf.connection.database = knexConf.connection.db = 'i-refuse-to-exist';
|
2017-11-17 21:03:43 +02:00
|
|
|
knexConf.acquireConnectionTimeout = 4000;
|
|
|
|
const knex = makeKnex(knexConf);
|
|
|
|
|
|
|
|
tape(dialect + ' - propagate error when DB does not exist', t => {
|
|
|
|
t.plan(1);
|
|
|
|
t.timeoutAfter(1000);
|
|
|
|
knex('accounts').select(1)
|
2018-06-29 10:47:06 +03:00
|
|
|
.then(res => {
|
2017-11-17 21:03:43 +02:00
|
|
|
t.fail(`Query should have failed, got: ${JSON.stringify(res)}`);
|
|
|
|
})
|
|
|
|
.catch(Bluebird.TimeoutError, e => {
|
|
|
|
t.fail(`Query should have failed with non timeout error`);
|
|
|
|
})
|
|
|
|
.catch(e => {
|
2018-02-07 11:17:17 +02:00
|
|
|
t.ok(e.message.indexOf('i-refuse-to-exist') > 0, `all good, failed as expected with msg: ${e.message}`);
|
2017-11-17 21:03:43 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
tape(dialect + ' - propagate error when DB does not exist for stream', t => {
|
|
|
|
t.plan(1);
|
|
|
|
t.timeoutAfter(1000);
|
|
|
|
|
|
|
|
knex.select(1)
|
|
|
|
.stream(stream => {})
|
|
|
|
.then(res => {
|
|
|
|
t.fail(`Stream query should have failed, got: ${JSON.stringify(res)}`);
|
|
|
|
})
|
|
|
|
.catch(Bluebird.TimeoutError, e => {
|
|
|
|
t.fail(`Stream query should have failed with non timeout error`);
|
|
|
|
})
|
|
|
|
.catch(e => {
|
2018-02-07 11:17:17 +02:00
|
|
|
t.ok(e.message.indexOf('i-refuse-to-exist') > 0, `all good, failed as expected with msg: ${e.message}`);
|
2017-11-17 21:03:43 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
tape.onFinish(() => {
|
|
|
|
knex.destroy();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dialect !== 'sqlite3') {
|
|
|
|
const knexConf = _.cloneDeep(knexfile[key]);
|
2018-02-07 11:17:17 +02:00
|
|
|
knexConf.acquireConnectionTimeout = 100;
|
|
|
|
knexConf.pool = { max: 1, min: 1 };
|
2017-11-17 21:03:43 +02:00
|
|
|
const knex = makeKnex(knexConf);
|
|
|
|
|
2017-11-19 18:43:28 +02:00
|
|
|
tape.onFinish(() => {
|
|
|
|
knex.destroy();
|
|
|
|
});
|
|
|
|
|
2017-11-17 21:03:43 +02:00
|
|
|
tape(dialect + ' - acquireConnectionTimeout works', t => {
|
|
|
|
t.plan(2);
|
|
|
|
t.timeoutAfter(1000);
|
|
|
|
|
2018-02-07 11:17:17 +02:00
|
|
|
// just hog the only connection.
|
2017-11-17 21:03:43 +02:00
|
|
|
knex.transaction(trx => {
|
2018-02-07 11:17:17 +02:00
|
|
|
// Don't return this promise! Also note that we use `knex` instead of `trx`
|
|
|
|
// here on purpose. The only reason this code is here, is that we can be
|
|
|
|
// certain `trx` has been created before this.
|
|
|
|
knex('accounts').select(1)
|
|
|
|
.then(() => {
|
|
|
|
t.fail('query should have stalled');
|
|
|
|
})
|
|
|
|
.catch(Bluebird.TimeoutError, e => {
|
|
|
|
t.pass('Got acquireTimeout error');
|
|
|
|
})
|
|
|
|
.catch(e => {
|
|
|
|
t.fail(`should have got acquire timeout error, but got ${e.message} instead.`);
|
|
|
|
})
|
|
|
|
.finally(() => {
|
|
|
|
trx.commit(); // release stuff
|
|
|
|
});
|
|
|
|
|
2017-11-17 21:03:43 +02:00
|
|
|
}).then(() => {
|
|
|
|
t.pass('transaction was resolved');
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
};
|