Skip to content

Commit 0a6de37

Browse files
Multiple tests running simultaneously
Added more stress testing tests
1 parent 77c8ed6 commit 0a6de37

File tree

11 files changed

+408
-341
lines changed

11 files changed

+408
-341
lines changed

.vscode/launch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"name": "Debug Jest Tests",
3333
"type": "node",
3434
"request": "launch",
35-
"runtimeArgs": ["--inspect-brk", "${workspaceRoot}/node_modules/.bin/jest", "--runInBand"],
35+
"runtimeArgs": ["--inspect-brk", "${workspaceRoot}/node_modules/.bin/jest"],
3636
"envFile": "${workspaceFolder}/.env",
3737
"console": "integratedTerminal",
3838
"internalConsoleOptions": "neverOpen",

.vscode/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"jest.autoRun": {
77
"onStartup": []
88
},
9-
"jestrunner.runOptions": ["--runInBand", "--detectOpenHandles", "--watchAll=false"],
9+
"jestrunner.runOptions": ["--detectOpenHandles", "--watchAll=false"],
1010
"svg.preview.background": "transparent",
1111
"editor.formatOnSave": true,
1212
"[typescript]": {

jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module.exports = {
22
preset: 'ts-jest',
33
testEnvironment: 'node',
44
testMatch: ['**/test/**/*.test.ts'],
5-
maxWorkers: 1,
5+
maxWorkers: 8,
66
collectCoverageFrom: ['<rootDir>/src/**/*.ts', '!<rootDir>/src/index.ts', '!<rootDir>/src/types/**/*.ts'],
77
transform: {
88
'^.+\\.(ts|tsx)$': [

src/database.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,16 @@ export class Database {
127127
// public methods
128128
//
129129

130+
/**
131+
* Returns the configuration with which this database was opened.
132+
* The configuration is readonly and cannot be changed as there may
133+
* be multiple connections using the same configuration.
134+
* @returns {SQLiteCloudConfig} A configuration object
135+
*/
136+
public getConfiguration(): SQLiteCloudConfig {
137+
return JSON.parse(JSON.stringify(this.config))
138+
}
139+
130140
/** Enable verbose mode */
131141
public verbose(): this {
132142
this.config.verbose = true

test/compare.test.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,25 @@
44

55
import { Database } from '../src/index'
66
import { CHINOOK_DATABASE_URL, CHINOOK_DATABASE_FILE, CHINOOK_FIRST_TRACK, LONG_TIMEOUT } from './connection.test'
7-
import { PREPARE_TESTING_SQL, getTestingDatabase } from './database.test'
7+
import { PREPARE_TESTING_SQL, createTestDatabase } from './database.test'
88

99
// https://github.com/TryGhost/node-sqlite3/wiki/API
1010
import sqlite3 from 'sqlite3'
11-
import fs from 'fs'
1211
import { join } from 'path'
1312

1413
const INSERT_SQL = "INSERT INTO people (name, hobby, age) VALUES ('Fantozzi Ugo', 'Competitive unicorn farting', 42); "
1514
const TESTING_DATABASE_FILE = join(__dirname, 'assets/testing.db')
1615

1716
function getTestingDatabaseFile(): sqlite3.Database {
18-
fs.unlinkSync(TESTING_DATABASE_FILE)
19-
const testingFile = new sqlite3.Database(TESTING_DATABASE_FILE)
17+
// create a unique id for this test run based on current time with
18+
// enough precision to avoid duplicate ids and be human readable
19+
const id = new Date()
20+
.toISOString()
21+
.replace(/-|:|T|Z|\./g, '')
22+
.slice(0, -1)
23+
24+
const testingFilename = TESTING_DATABASE_FILE.replace('.db', `-${id}.db`)
25+
const testingFile = new sqlite3.Database(testingFilename)
2026
testingFile.exec(PREPARE_TESTING_SQL)
2127
return testingFile
2228
}
@@ -65,7 +71,7 @@ describe('compare.test.ts', () => {
6571
})
6672

6773
it('sqlitecloud: insert with plain sql', done => {
68-
const testingCloud = getTestingDatabase()
74+
const testingCloud = createTestDatabase()
6975

7076
// https://github.com/TryGhost/node-sqlite3/wiki/API#runsql--param---callback
7177
function onInsert(error: Error, results: any) {

test/connection.test.ts

Lines changed: 50 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,21 @@ export const LONG_TIMEOUT = 100 * 1000 // 100 seconds
2020
export function getChinoookConfig(): SQLiteCloudConfig {
2121
return parseConnectionString(CHINOOK_DATABASE_URL)
2222
}
23+
2324
export function getTestingConfig(): SQLiteCloudConfig {
24-
return parseConnectionString(TESTING_DATABASE_URL)
25+
const testingConfig = parseConnectionString(TESTING_DATABASE_URL)
26+
testingConfig.sqliteMode = true
27+
28+
// create a unique id for this test run based on current time with
29+
// enough precision to avoid duplicate ids and be human readable
30+
const id = new Date()
31+
.toISOString()
32+
.replace(/-|:|T|Z|\./g, '')
33+
.slice(0, -1)
34+
35+
testingConfig.createDatabase = true
36+
testingConfig.database = testingConfig.database?.replace('.db', `-${id}.db`)
37+
return testingConfig
2538
}
2639

2740
export const CHINOOK_FIRST_TRACK = {
@@ -37,24 +50,27 @@ export const CHINOOK_FIRST_TRACK = {
3750
}
3851

3952
describe('connection', () => {
40-
let connection: SQLiteCloudConnection
53+
let chinook: SQLiteCloudConnection
4154

42-
beforeEach(done => {
55+
beforeAll(() => {
4356
expect(CHINOOK_DATABASE_URL).toBeDefined()
4457
expect(TESTING_DATABASE_URL).toBeDefined()
45-
connection = new SQLiteCloudConnection(CHINOOK_DATABASE_URL + '?nonlinearizable=1', error => {
46-
expect(connection).toBeDefined()
58+
})
59+
60+
beforeEach(done => {
61+
chinook = new SQLiteCloudConnection(CHINOOK_DATABASE_URL + '?nonlinearizable=1', error => {
62+
expect(chinook).toBeDefined()
4763
done()
4864
})
4965
// connection.verbose()
5066
})
5167

5268
afterEach(() => {
53-
if (connection) {
54-
connection.close()
69+
if (chinook) {
70+
chinook.close()
5571
}
5672
// @ts-ignore
57-
connection = undefined
73+
chinook = undefined
5874
})
5975

6076
describe('connect', () => {
@@ -71,7 +87,7 @@ describe('connection', () => {
7187
expect(error).toBeNull()
7288
expect(conn.connected).toBe(true)
7389

74-
connection.sendCommands('TEST STRING', (error, results) => {
90+
chinook.sendCommands('TEST STRING', (error, results) => {
7591
conn.close()
7692
expect(conn.connected).toBe(false)
7793
done()
@@ -87,7 +103,7 @@ describe('connection', () => {
87103
expect(error).toBeNull()
88104
expect(conn.connected).toBe(true)
89105

90-
connection.sendCommands('TEST STRING', (error, results) => {
106+
chinook.sendCommands('TEST STRING', (error, results) => {
91107
conn.close()
92108
expect(conn.connected).toBe(false)
93109
done()
@@ -119,63 +135,63 @@ describe('connection', () => {
119135

120136
describe('send test commands', () => {
121137
it('should test integer', done => {
122-
connection.sendCommands('TEST INTEGER', (error, results) => {
138+
chinook.sendCommands('TEST INTEGER', (error, results) => {
123139
expect(error).toBeNull()
124140
expect(results).toBe(123456)
125141
done()
126142
})
127143
})
128144

129145
it('should test null', done => {
130-
connection.sendCommands('TEST NULL', (error, results) => {
146+
chinook.sendCommands('TEST NULL', (error, results) => {
131147
expect(error).toBeNull()
132148
expect(results).toBeNull()
133149
done()
134150
})
135151
})
136152

137153
it('should test float', done => {
138-
connection.sendCommands('TEST FLOAT', (error, results) => {
154+
chinook.sendCommands('TEST FLOAT', (error, results) => {
139155
expect(error).toBeNull()
140156
expect(results).toBe(3.1415926)
141157
done()
142158
})
143159
})
144160

145161
it('should test string', done => {
146-
connection.sendCommands('TEST STRING', (error, results) => {
162+
chinook.sendCommands('TEST STRING', (error, results) => {
147163
expect(error).toBeNull()
148164
expect(results).toBe('Hello World, this is a test string.')
149165
done()
150166
})
151167
})
152168

153169
it('should test zero string', done => {
154-
connection.sendCommands('TEST ZERO_STRING', (error, results) => {
170+
chinook.sendCommands('TEST ZERO_STRING', (error, results) => {
155171
expect(error).toBeNull()
156172
expect(results).toBe('Hello World, this is a zero-terminated test string.')
157173
done()
158174
})
159175
})
160176

161177
it('should test string0', done => {
162-
connection.sendCommands('TEST STRING0', (error, results) => {
178+
chinook.sendCommands('TEST STRING0', (error, results) => {
163179
expect(error).toBeNull()
164180
expect(results).toBe('')
165181
done()
166182
})
167183
})
168184

169185
it('should test command', done => {
170-
connection.sendCommands('TEST COMMAND', (error, results) => {
186+
chinook.sendCommands('TEST COMMAND', (error, results) => {
171187
expect(error).toBeNull()
172188
expect(results).toBe('PING')
173189
done()
174190
})
175191
})
176192

177193
it('should test json', done => {
178-
connection.sendCommands('TEST JSON', (error, results) => {
194+
chinook.sendCommands('TEST JSON', (error, results) => {
179195
expect(error).toBeNull()
180196
expect(results).toEqual({
181197
'msg-from': { class: 'soldier', name: 'Wixilav' },
@@ -193,7 +209,7 @@ describe('connection', () => {
193209
})
194210

195211
it('should test blob', done => {
196-
connection.sendCommands('TEST BLOB', (error, results) => {
212+
chinook.sendCommands('TEST BLOB', (error, results) => {
197213
expect(error).toBeNull()
198214
expect(typeof results).toBe('object')
199215
expect(results).toBeInstanceOf(Buffer)
@@ -204,7 +220,7 @@ describe('connection', () => {
204220
})
205221

206222
it('should test blob0', done => {
207-
connection.sendCommands('TEST BLOB0', (error, results) => {
223+
chinook.sendCommands('TEST BLOB0', (error, results) => {
208224
expect(error).toBeNull()
209225
expect(typeof results).toBe('object')
210226
expect(results).toBeInstanceOf(Buffer)
@@ -215,7 +231,7 @@ describe('connection', () => {
215231
})
216232

217233
it('should test error', done => {
218-
connection.sendCommands('TEST ERROR', (error, results) => {
234+
chinook.sendCommands('TEST ERROR', (error, results) => {
219235
expect(error).toBeDefined()
220236
expect(error).toBeInstanceOf(SQLiteCloudError)
221237

@@ -230,7 +246,7 @@ describe('connection', () => {
230246
})
231247

232248
it('should test exterror', done => {
233-
connection.sendCommands('TEST EXTERROR', (error, results) => {
249+
chinook.sendCommands('TEST EXTERROR', (error, results) => {
234250
expect(error).toBeDefined()
235251
expect(error).toBeInstanceOf(SQLiteCloudError)
236252

@@ -245,7 +261,7 @@ describe('connection', () => {
245261
})
246262

247263
it('should test array', done => {
248-
connection.sendCommands('TEST ARRAY', (error, results) => {
264+
chinook.sendCommands('TEST ARRAY', (error, results) => {
249265
expect(error).toBeNull()
250266
expect(Array.isArray(results)).toBe(true)
251267

@@ -260,7 +276,7 @@ describe('connection', () => {
260276
})
261277

262278
it('should test rowset', done => {
263-
connection.sendCommands('TEST ROWSET', (error, results) => {
279+
chinook.sendCommands('TEST ROWSET', (error, results) => {
264280
expect(results.numberOfRows).toBe(41)
265281
expect(results.numberOfColumns).toBe(2)
266282
expect(results.version == 1 || results.version == 2).toBeTruthy()
@@ -272,7 +288,7 @@ describe('connection', () => {
272288
it(
273289
'should test chunked rowset',
274290
done => {
275-
connection.sendCommands('TEST ROWSET_CHUNK', (error, results) => {
291+
chinook.sendCommands('TEST ROWSET_CHUNK', (error, results) => {
276292
expect(results.numberOfRows).toBe(147)
277293
expect(results.numberOfColumns).toBe(1)
278294
expect(results.columnsNames).toEqual(['key'])
@@ -289,7 +305,7 @@ describe('connection', () => {
289305
let completed = 0
290306

291307
for (let i = 0; i < numQueries; i++) {
292-
connection.sendCommands(`select ${i} as "count", 'hello' as 'string'`, (error, results) => {
308+
chinook.sendCommands(`select ${i} as "count", 'hello' as 'string'`, (error, results) => {
293309
expect(error).toBeNull()
294310
expect(results.numberOfColumns).toBe(2)
295311
expect(results.numberOfRows).toBe(1)
@@ -307,7 +323,7 @@ describe('connection', () => {
307323

308324
describe('send select commands', () => {
309325
it('should select long formatted string', done => {
310-
connection.sendCommands("USE DATABASE :memory:; select printf('%.*c', 1000, 'x') AS DDD", (error, results) => {
326+
chinook.sendCommands("USE DATABASE :memory:; select printf('%.*c', 1000, 'x') AS DDD", (error, results) => {
311327
expect(results.numberOfColumns).toBe(1)
312328
expect(results.numberOfRows).toBe(1)
313329
expect(results.version == 1 || results.version == 2).toBeTruthy()
@@ -321,7 +337,7 @@ describe('connection', () => {
321337
})
322338

323339
it('should select database', done => {
324-
connection.sendCommands('USE DATABASE chinook.db;', (error, results) => {
340+
chinook.sendCommands('USE DATABASE chinook.db;', (error, results) => {
325341
expect(results.numberOfColumns).toBeUndefined()
326342
expect(results.numberOfRows).toBeUndefined()
327343
expect(results.version).toBeUndefined()
@@ -330,23 +346,23 @@ describe('connection', () => {
330346
})
331347

332348
it('should select * from tracks limit 10 (no chunks)', done => {
333-
connection.sendCommands('SELECT * FROM tracks LIMIT 10;', (error, results) => {
349+
chinook.sendCommands('SELECT * FROM tracks LIMIT 10;', (error, results) => {
334350
expect(results.numberOfColumns).toBe(9)
335351
expect(results.numberOfRows).toBe(10)
336352
done()
337353
})
338354
})
339355

340356
it('should select * from tracks (with chunks)', done => {
341-
connection.sendCommands('SELECT * FROM tracks;', (error, results) => {
357+
chinook.sendCommands('SELECT * FROM tracks;', (error, results) => {
342358
expect(results.numberOfColumns).toBe(9)
343359
expect(results.numberOfRows).toBe(3503)
344360
done()
345361
})
346362
})
347363

348364
it('should select * from albums', done => {
349-
connection.sendCommands('SELECT * FROM albums;', (error, results) => {
365+
chinook.sendCommands('SELECT * FROM albums;', (error, results) => {
350366
expect(results.numberOfColumns).toBe(3)
351367
expect(results.numberOfRows).toBe(347)
352368
expect(results.version == 1 || results.version == 2).toBeTruthy()
@@ -363,7 +379,7 @@ describe('connection', () => {
363379
let completed = 0
364380
const startTime = Date.now()
365381
for (let i = 0; i < numQueries; i++) {
366-
connection.sendCommands('TEST STRING', (error, results) => {
382+
chinook.sendCommands('TEST STRING', (error, results) => {
367383
expect(results).toBe('Hello World, this is a test string.')
368384
if (++completed >= numQueries) {
369385
const queryMs = (Date.now() - startTime) / numQueries
@@ -384,7 +400,7 @@ describe('connection', () => {
384400
let completed = 0
385401
const startTime = Date.now()
386402
for (let i = 0; i < numQueries; i++) {
387-
connection.sendCommands('SELECT * FROM albums ORDER BY RANDOM() LIMIT 4;', (error, results) => {
403+
chinook.sendCommands('SELECT * FROM albums ORDER BY RANDOM() LIMIT 4;', (error, results) => {
388404
expect(error).toBeNull()
389405
expect(results.numberOfColumns).toBe(3)
390406
expect(results.numberOfRows).toBe(4)
@@ -407,7 +423,7 @@ describe('connection', () => {
407423
let completed = 0
408424
const startTime = Date.now()
409425
for (let i = 0; i < numQueries; i++) {
410-
connection.sendCommands(
426+
chinook.sendCommands(
411427
'SELECT * FROM albums ORDER BY RANDOM() LIMIT 16; SELECT * FROM albums ORDER BY RANDOM() LIMIT 12; SELECT * FROM albums ORDER BY RANDOM() LIMIT 8; SELECT * FROM albums ORDER BY RANDOM() LIMIT 4;',
412428
(error, results) => {
413429
// server only returns the last rowset?

0 commit comments

Comments
 (0)