ueberdb2 1.4.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,68 @@
1
+ 'use strict';
2
+
3
+ const assert = require('assert').strict;
4
+ const {databases} = require('./lib/databases');
5
+ const mysql = require('../databases/mysql_db');
6
+
7
+ describe(__filename, function () {
8
+ beforeEach(async function () {
9
+ if (databases.mysql == null) return this.skip();
10
+ });
11
+
12
+ it('connect error is detected during init()', async function () {
13
+ // Use an invalid TCP port to force a connection error.
14
+ const db = new mysql.Database({...databases.mysql, port: 65536});
15
+ // An error is expected; prevent it from being logged.
16
+ db.logger = Object.setPrototypeOf({error() {}}, db.logger);
17
+ await assert.rejects(db.init());
18
+ });
19
+
20
+ it('query after fatal error works', async function () {
21
+ const db = new mysql.Database(databases.mysql);
22
+ await db.init();
23
+ // An error is expected; prevent it from being logged.
24
+ db.logger = Object.setPrototypeOf({error() {}}, db.logger);
25
+ // Sleep longer than the timeout to force a fatal error.
26
+ await assert.rejects(db._query({sql: 'DO SLEEP(1);', timeout: 1}), {fatal: true});
27
+ await assert.doesNotReject(db._query({sql: 'SELECT 1;'}));
28
+ await db.close();
29
+ });
30
+
31
+ it('query times out', async function () {
32
+ const db = new mysql.Database(databases.mysql);
33
+ await db.init();
34
+ // Timeout error messages are expected; prevent them from being logged.
35
+ db.logger = Object.setPrototypeOf({error() {}}, db.logger);
36
+ db.settings.queryTimeout = 100;
37
+ await assert.doesNotReject(db._query({sql: 'DO SLEEP(0.090);'}));
38
+ await assert.rejects(db._query({sql: 'DO SLEEP(0.110);'}));
39
+ await db.close();
40
+ });
41
+
42
+ it('queries run concurrently and are queued when pool is busy', async function () {
43
+ const connectionLimit = 10;
44
+ const db = new mysql.Database({...databases.mysql, connectionLimit});
45
+ await db.init();
46
+ // Set the query duration high enough to avoid flakiness on slow machines but low enough to keep
47
+ // the overall test duration short.
48
+ const queryDuration = 100;
49
+ db.settings.queryTimeout = queryDuration + 100;
50
+ const enqueueQuery = () => db._query({sql: `DO SLEEP(${queryDuration / 1000});`});
51
+
52
+ // Reduce test flakiness by using slow queries to warm up the pool's connections.
53
+ await Promise.all([...Array(connectionLimit)].map(enqueueQuery));
54
+
55
+ // Time how long it takes to run just under 2 * connectionLimit queries.
56
+ const nQueries = 2 * connectionLimit - 1;
57
+ const start = Date.now();
58
+ await Promise.all([...Array(nQueries)].map(enqueueQuery));
59
+ const duration = Date.now() - start;
60
+
61
+ const wantDurationLower = Math.ceil(nQueries / connectionLimit) * queryDuration;
62
+ assert(duration >= wantDurationLower, `took ${duration}ms, want >= ${wantDurationLower}ms`);
63
+ const wantDurationUpper = wantDurationLower + queryDuration;
64
+ assert(duration < wantDurationUpper, `took ${duration}ms, want < ${wantDurationUpper}ms`);
65
+
66
+ await db.close();
67
+ });
68
+ });
@@ -0,0 +1,17 @@
1
+ 'use strict';
2
+
3
+ const assert = require('assert').strict;
4
+ const {databases} = require('./lib/databases');
5
+ const ueberdb = require('../index');
6
+ const util = require('util');
7
+
8
+ describe(__filename, function () {
9
+ it('connection string instead of settings object', async function () {
10
+ const {user, password, host, database} = databases.postgres;
11
+ const db =
12
+ new ueberdb.Database('postgres', `postgres://${user}:${password}@${host}/${database}`);
13
+ await util.promisify(db.init.bind(db))();
14
+ await util.promisify(db.set.bind(db))('key', 'val');
15
+ assert.equal(await util.promisify(db.get.bind(db))('key'), 'val');
16
+ });
17
+ });