-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathconcurrency.test.js
More file actions
207 lines (170 loc) · 5.85 KB
/
concurrency.test.js
File metadata and controls
207 lines (170 loc) · 5.85 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import test from "ava";
import crypto from 'crypto';
import fs from 'fs';
test.beforeEach(async (t) => {
const [db, errorType, path] = await connect();
await db.exec(`
DROP TABLE IF EXISTS users;
CREATE TABLE users (id TEXT PRIMARY KEY, name TEXT, email TEXT)
`);
const aliceId = generateUUID();
const bobId = generateUUID();
await db.exec(
`INSERT INTO users (id, name, email) VALUES ('${aliceId}', 'Alice', 'alice@example.org')`
);
await db.exec(
`INSERT INTO users (id, name, email) VALUES ('${bobId}', 'Bob', 'bob@example.com')`
);
t.context = {
db,
errorType,
aliceId,
bobId,
path
};
});
test("Concurrent reads", async (t) => {
const db = t.context.db;
const stmt = await db.prepare("SELECT * FROM users WHERE id = ?");
const promises = [];
for (let i = 0; i < 100; i++) {
promises.push(stmt.get(t.context.aliceId));
promises.push(stmt.get(t.context.bobId));
}
const results = await Promise.all(promises);
for (let i = 0; i < results.length; i++) {
const result = results[i];
t.truthy(result);
t.is(typeof result.name, 'string');
t.is(typeof result.email, 'string');
}
cleanup(t.context);
});
test("Concurrent writes", async (t) => {
const db = t.context.db;
await db.exec(`
DROP TABLE IF EXISTS concurrent_users;
CREATE TABLE concurrent_users (
id TEXT PRIMARY KEY,
name TEXT,
email TEXT
)
`);
const stmt = await db.prepare("INSERT INTO concurrent_users(id, name, email) VALUES (:id, :name, :email)");
const promises = [];
for (let i = 0; i < 50; i++) {
promises.push(stmt.run({
id: generateUUID(),
name: `User${i}`,
email: `user${i}@example.com`
}));
}
await Promise.all(promises);
const countStmt = await db.prepare("SELECT COUNT(*) as count FROM concurrent_users");
const result = await countStmt.get();
t.is(result.count, 50);
cleanup(t.context);
});
test("Concurrent transaction isolation", async (t) => {
const db = t.context.db;
await db.exec(`
DROP TABLE IF EXISTS transaction_users;
CREATE TABLE transaction_users (
id TEXT PRIMARY KEY,
name TEXT,
email TEXT
)
`);
const aliceId = generateUUID();
const bobId = generateUUID();
await db.exec(`
INSERT INTO transaction_users (id, name, email) VALUES
('${aliceId}', 'Alice', 'alice@example.org'),
('${bobId}', 'Bob', 'bob@example.com')
`);
const updateUser = db.transaction(async (id, name, email) => {
const stmt = await db.prepare("UPDATE transaction_users SET name = :name, email = :email WHERE id = :id");
await stmt.run({ id, name, email });
});
const promises = [];
for (let i = 0; i < 10; i++) {
promises.push(updateUser(aliceId, `Alice${i}`, `alice${i}@example.org`));
promises.push(updateUser(bobId, `Bob${i}`, `bob${i}@example.com`));
}
await Promise.all(promises);
const stmt = await db.prepare("SELECT * FROM transaction_users ORDER BY name");
const results = await stmt.all();
t.is(results.length, 2);
t.truthy(results[0].name.startsWith('Alice'));
t.truthy(results[1].name.startsWith('Bob'));
cleanup(t.context);
});
test("Concurrent reads and writes", async (t) => {
const db = t.context.db;
await db.exec(`
DROP TABLE IF EXISTS mixed_users;
CREATE TABLE mixed_users (
id TEXT PRIMARY KEY,
name TEXT,
email TEXT
)
`);
const aliceId = generateUUID();
await db.exec(`
INSERT INTO mixed_users (id, name, email) VALUES
('${aliceId}', 'Alice', 'alice@example.org')
`);
const readStmt = await db.prepare("SELECT * FROM mixed_users WHERE id = ?");
const writeStmt = await db.prepare("INSERT INTO mixed_users(id, name, email) VALUES (:id, :name, :email)");
const promises = [];
for (let i = 0; i < 20; i++) {
promises.push(readStmt.get(aliceId));
writeStmt.run({
id: generateUUID(),
name: `User${i}`,
email: `user${i}@example.com`
});
}
await Promise.all(promises);
const countStmt = await db.prepare("SELECT COUNT(*) as count FROM mixed_users");
const result = await countStmt.get();
t.is(result.count, 21); // 1 initial + 20 new records
await cleanup(t.context);
});
test("Concurrent operations with timeout should handle busy database", async (t) => {
const timeout = 1000;
const path = `test-${crypto.randomBytes(8).toString('hex')}.db`;
const [conn1] = await connect(path);
const [conn2] = await connect(path, { timeout });
await conn1.exec("CREATE TABLE t(id TEXT PRIMARY KEY, x INTEGER)");
await conn1.exec("BEGIN IMMEDIATE");
await conn1.exec(`INSERT INTO t VALUES ('${generateUUID()}', 1)`);
const start = Date.now();
try {
await conn2.exec(`INSERT INTO t VALUES ('${generateUUID()}', 2)`);
t.fail("Should have thrown SQLITE_BUSY error");
} catch (e) {
t.is(e.code, "SQLITE_BUSY");
const end = Date.now();
const elapsed = end - start;
t.true(elapsed > timeout / 2, "Timeout should be respected");
}
conn1.close();
conn2.close();
// FIXME: Fails on Windows because file is still busy.
// fs.unlinkSync(path);
});
const connect = async (path_opt, options = {}) => {
const path = path_opt ?? `test-${crypto.randomBytes(8).toString('hex')}.db`;
const x = await import("libsql/promise");
const db = new x.default(process.env.LIBSQL_DATABASE ?? path, options);
return [db, x.SqliteError, path];
};
const cleanup = async (context) => {
context.db.close();
// FIXME: Fails on Windows because file is still busy.
// fs.unlinkSync(context.path);
};
const generateUUID = () => {
return crypto.randomUUID();
};