-
-
Notifications
You must be signed in to change notification settings - Fork 831
/
Copy pathcache.test.js
133 lines (120 loc) · 4.53 KB
/
cache.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
var sqlite3 = require('..');
var assert = require('assert');
var helper = require('./support/helper');
var util = require('util');
describe('cache', function() {
const filename = 'test/tmp/test_cache.db';
const dbs = [];
const open = async (filename) => await new Promise((resolve, reject) => {
new sqlite3.cached.Database(filename, function (err) {
if (err != null) return reject(err);
resolve(this);
});
});
const close = async (db) => await util.promisify(db.close.bind(db))();
beforeEach(async function () {
dbs.length = 0;
helper.ensureExists('test/tmp');
helper.deleteFile(filename);
});
afterEach(async function () {
await Promise.all(dbs.map(async (db) => await close(db)));
dbs.length = 0;
helper.deleteFile(filename);
});
it('should cache Database objects while opening', function(done) {
var opened1 = false, opened2 = false;
dbs.push(new sqlite3.cached.Database(filename, function(err) {
if (err) throw err;
opened1 = true;
if (opened1 && opened2) done();
}));
dbs.push(new sqlite3.cached.Database(filename, function(err) {
if (err) throw err;
opened2 = true;
if (opened1 && opened2) done();
}));
assert.equal(dbs[0], dbs[1]);
});
it('should cache Database objects after they are open', function(done) {
dbs.push(new sqlite3.cached.Database(filename, function(err) {
if (err) throw err;
process.nextTick(function() {
dbs.push(new sqlite3.cached.Database(filename, function(err) {
if (err) throw err;
done();
}));
assert.equal(dbs[0], dbs[1]);
});
}));
});
it('cached.Database() callback is called asynchronously', async function () {
await Promise.all([0, 1].map(() => new Promise((resolve, reject) => {
let callbackCalled = false;
dbs.push(new sqlite3.cached.Database(filename, (err) => {
callbackCalled = true;
if (err != null) return reject(err);
resolve();
}));
assert(!callbackCalled);
})));
});
it('cached.Database() callback is called with db as context', async function () {
await Promise.all([0, 1].map((i) => new Promise((resolve, reject) => {
dbs.push(new sqlite3.cached.Database(filename, function (err) {
if (err != null) return reject(err);
if (this !== dbs[i]) return reject(new Error('this !== dbs[i]'));
resolve();
}));
})));
});
it('db.close() callback is called asynchronously', async function () {
dbs.push(await open(filename));
dbs.push(await open(filename));
while (dbs.length > 0) {
await new Promise((resolve, reject) => {
let callbackCalled = false;
dbs.pop().close((err) => {
callbackCalled = true;
if (err != null) return reject(err);
resolve();
});
assert(!callbackCalled);
});
}
});
it('db.close() callback is called with db as context', async function () {
dbs.push(await open(filename));
dbs.push(await open(filename));
while (dbs.length > 0) {
await new Promise((resolve, reject) => {
const db = dbs.pop();
db.close(function (err) {
if (err) return reject(err);
if (this !== db) return reject(new Error('this !== db'));
resolve();
});
});
}
});
it('db.close() does not close other copies', async function () {
dbs.push(await open(filename));
dbs.push(await open(filename));
await close(dbs.pop());
assert(dbs[0].open);
});
it('db.close() closes the underlying Database after closing the last copy', async function () {
dbs.push(await open(filename));
dbs.push(await open(filename));
const db = dbs[0];
await close(dbs.pop());
await close(dbs.pop());
assert(!db.open);
});
it('cached.Database() returns an open Database after closing', async function () {
dbs.push(await open(filename));
await close(dbs.pop());
dbs.push(await open(filename));
assert(dbs[0].open);
});
});