-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnection-ws.test.ts
More file actions
514 lines (464 loc) · 17.8 KB
/
connection-ws.test.ts
File metadata and controls
514 lines (464 loc) · 17.8 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
/**
* connection-ws.test.ts - test connection via socket.io based gateway
*/
import { SQLiteCloudConnection } from '../src/drivers/connection'
import { SQLiteCloudWebsocketConnection } from '../src/drivers/connection-ws'
import { SQLiteCloudCommand } from '../src/drivers/types'
import { SQLiteCloudError } from '../src/index'
import { EXPECT_SPEED_MS, getChinookConfig, getChinookWebsocketConnection, LONG_TIMEOUT, WARN_SPEED_MS } from './shared'
describe('connection-ws', () => {
let chinook: SQLiteCloudConnection
beforeEach(() => {
chinook = getChinookWebsocketConnection()
})
afterEach(() => {
chinook?.close()
// @ts-ignore
chinook = undefined
})
describe('connect', () => {
it('should connect', () => {
// ...in beforeEach
})
it('should connect with config object string', done => {
const configObj = getChinookConfig()
configObj.usewebsocket = true
let connection: SQLiteCloudWebsocketConnection | null = null
connection = new SQLiteCloudWebsocketConnection(configObj, error => {
expect(error).toBeNull()
connection?.close()
done()
})
})
it('should not connect with incorrect credentials', done => {
const configObj = getChinookConfig()
configObj.connectionstring?.replace(configObj.password as string, 'wrongpassword')
configObj.password = 'wrongpassword'
configObj.usewebsocket = true
// should attempt connection and return error
const connection = new SQLiteCloudWebsocketConnection(configObj, error => {
expect(error).toBeDefined()
done()
connection?.close()
})
})
/* TODO RESTORE TEST
it('should connect with connection string', done => {
if (CHINOOK_DATABASE_URL.indexOf('localhost') > 0) {
// skip this test when running locally since it requires a self-signed certificate
done()
}
let conn: SQLiteCloudWebsocketConnection | null = null
conn = new SQLiteCloudWebsocketConnection(CHINOOK_DATABASE_URL, error => {
expect(error).toBeNull()
// @ts-ignore
this.sendCommands('TEST STRING', (error, results) => {
expect(error).toBeNull()
conn?.close()
expect(conn?.connected).toBe(false)
done()
})
})
expect(conn).toBeDefined()
})
})
*/
it(
'should catch connect_error on connection errors',
done => {
const configObj = getChinookConfig()
configObj.usewebsocket = true
configObj.host = 'non.existing.host.name'
let connection: SQLiteCloudWebsocketConnection | null = null
connection = new SQLiteCloudWebsocketConnection(configObj, error => {
try {
expect(error).toBeDefined()
expect(error?.message).toContain('Error: getaddrinfo ENOTFOUND non.existing.host.name')
connection?.close()
} finally {
done()
}
})
},
LONG_TIMEOUT
)
// skip this test since it requires a paused free node
it.skip(
'should catch connect_error when node is scaled down',
done => {
const configObj = getChinookConfig("sqlitecloud://paused-node")
configObj.usewebsocket = true
let connection: SQLiteCloudWebsocketConnection | null = null
connection = new SQLiteCloudWebsocketConnection(configObj, error => {
try {
expect(error).toBeDefined()
const m = error?.message
expect(error?.message.startsWith('Your free node has been paused due to inactivity. To resume usage, please restart your node from your dashboard: https://dashboard.sqlitecloud.io')).toBe(true)
connection?.close()
} finally {
done()
}
})
},
LONG_TIMEOUT
)
describe('send test commands', () => {
it('should test integer', done => {
chinook.sendCommands('TEST INTEGER', (error, results) => {
expect(error).toBeNull()
expect(results).toBe(123456)
done()
})
})
it('should test null', done => {
chinook.sendCommands('TEST NULL', (error, results) => {
expect(error).toBeNull()
expect(results).toBeNull()
done()
})
})
it('should test float', done => {
chinook.sendCommands('TEST FLOAT', (error, results) => {
expect(error).toBeNull()
expect(results).toBe(3.1415926)
done()
})
})
it('should test string', done => {
chinook.sendCommands('TEST STRING', (error, results) => {
expect(error).toBeNull()
expect(results).toBe('Hello World, this is a test string.')
done()
})
})
it('should test zero string', done => {
chinook.sendCommands('TEST ZERO_STRING', (error, results) => {
expect(error).toBeNull()
expect(results).toBe('Hello World, this is a zero-terminated test string.')
done()
})
})
it('should test string0', done => {
chinook.sendCommands('TEST STRING0', (error, results) => {
expect(error).toBeNull()
expect(results).toBe('')
done()
})
})
it('should test command', done => {
chinook.sendCommands('TEST COMMAND', (error, results) => {
expect(error).toBeNull()
expect(results).toBe('PING')
done()
})
})
it('should test json', done => {
chinook.sendCommands('TEST JSON', (error, results) => {
expect(error).toBeNull()
expect(results).toEqual({
'msg-from': { class: 'soldier', name: 'Wixilav' },
'msg-to': { class: 'supreme-commander', name: '[Redacted]' },
'msg-type': ['0xdeadbeef', 'irc log'],
'msg-log': [
'soldier: Boss there is a slight problem with the piece offering to humans',
'supreme-commander: Explain yourself soldier!',
"soldier: Well they don't seem to move anymore...",
'supreme-commander: Oh snap, I came here to see them twerk!'
]
})
done()
})
})
it('should test blob', done => {
chinook.sendCommands('TEST BLOB', (error, results) => {
expect(error).toBeNull()
expect(typeof results).toBe('object')
expect(results).toBeInstanceOf(Buffer)
const bufferrowset = results as any as Buffer
expect(bufferrowset.length).toBe(1000)
done()
})
})
it('should test blob0', done => {
chinook.sendCommands('TEST BLOB0', (error, results) => {
expect(error).toBeNull()
expect(typeof results).toBe('object')
expect(results).toBeInstanceOf(Buffer)
const bufferrowset = results as any as Buffer
expect(bufferrowset.length).toBe(0)
done()
})
})
it('should test error', done => {
chinook.sendCommands('TEST ERROR', (error, results) => {
expect(error).toBeDefined()
expect(error).toBeInstanceOf(SQLiteCloudError)
const sqliteCloudError = error as SQLiteCloudError
expect(sqliteCloudError.message).toBe('This is a test error message with a devil error code.')
expect(sqliteCloudError.errorCode).toBe('66666')
expect(sqliteCloudError.externalErrorCode).toBe('0')
expect(sqliteCloudError.offsetCode).toBe(-1)
done()
})
})
it('should test exterror', done => {
chinook.sendCommands('TEST EXTERROR', (error, results) => {
expect(error).toBeDefined()
expect(error).toBeInstanceOf(SQLiteCloudError)
const sqliteCloudError = error as SQLiteCloudError
expect(sqliteCloudError.message).toBe('This is a test error message with an extcode and a devil error code.')
expect(sqliteCloudError.errorCode).toBe('66666')
expect(sqliteCloudError.externalErrorCode).toBe('333')
expect(sqliteCloudError.offsetCode).toBe(-1)
done()
})
})
it('should test array', done => {
chinook.sendCommands('TEST ARRAY', (error, results) => {
expect(error).toBeNull()
expect(Array.isArray(results)).toBe(true)
const arrayrowset = results as any as Array<any>
expect(arrayrowset.length).toBe(5)
expect(arrayrowset[0].Result).toBe('Hello World')
expect(arrayrowset[1].Result).toBe(123456)
expect(arrayrowset[2].Result).toBe(3.1415)
expect(arrayrowset[3].Result).toBeNull()
done()
})
})
it('should test rowset', done => {
chinook.sendCommands('TEST ROWSET', (error, results) => {
expect(error).toBeNull()
expect(results.numberOfRows).toBeGreaterThan(20)
expect(results.numberOfColumns).toBe(2)
expect(results.version == 1 || results.version == 2).toBeTruthy()
expect(results.columnsNames).toEqual(['key', 'value'])
done()
})
})
it(
'should test chunked rowset',
done => {
// this operation sends 150 packets, so we need to increase the timeout
const database = getChinookWebsocketConnection(undefined, { timeout: 60 * 1000 })
database.sendCommands('TEST ROWSET_CHUNK', (error, results) => {
expect(error).toBeNull()
expect(results.numberOfRows).toBe(147)
expect(results.numberOfColumns).toBe(1)
expect(results.columnsNames).toEqual(['key'])
database.close()
done()
})
},
LONG_TIMEOUT
)
})
describe('operations', () => {
it(
'should serialize operations',
done => {
const numQueries = 20
let completed = 0
for (let i = 0; i < numQueries; i++) {
chinook.sendCommands(`select ${i} as "count", 'hello' as 'string'`, (error, results) => {
expect(error).toBeNull()
expect(results.numberOfColumns).toBe(2)
expect(results.numberOfRows).toBe(1)
expect(results.version == 1 || results.version == 2).toBeTruthy()
expect(results.columnsNames).toEqual(['count', 'string'])
expect(results.getItem(0, 0)).toBe(i)
if (++completed >= numQueries) {
done()
}
})
}
},
LONG_TIMEOUT
)
/* TODO RESTORE TEST
it('should apply short timeout', done => {
// apply shorter timeout
const configObj = parseconnectionstring(CHINOOK_DATABASE_URL + '?timeout=20')
configObj.websocketOptions = { useWebsocket: true }
const database = new SQLiteCloudConnection(configObj, error => {
if (error) {
expect(error).toBeInstanceOf(SQLiteCloudError)
expect((error as any).message).toBe('Request timed out')
done()
database.close()
} else {
// this operation sends 150 packets and cannot complete in 20ms
database.sendCommands('TEST ROWSET_CHUNK', (error, results) => {
expect(error).toBeInstanceOf(SQLiteCloudError)
expect((error as any).message).toBe('Request timed out')
done()
database.close()
})
}
})
})
*/
})
describe('send select commands', () => {
it('should LIST METADATA', done => {
chinook.sendCommands('LIST METADATA;', (error, results) => {
expect(error).toBeNull()
expect(results.numberOfColumns).toBe(8)
expect(results.numberOfRows).toBeGreaterThanOrEqual(32)
done()
})
})
it('should select results with no colum names', done => {
chinook.sendCommands("select 42, 'hello'", (error, results) => {
expect(error).toBeNull()
expect(results.numberOfColumns).toBe(2)
expect(results.numberOfRows).toBe(1)
expect(results.version == 1 || results.version == 2).toBeTruthy()
expect(results.columnsNames).toEqual(['42', "'hello'"]) // column name should be hello, not 'hello'
expect(results.getItem(0, 0)).toBe(42)
expect(results.getItem(0, 1)).toBe('hello')
done()
})
})
it('should select long formatted string', done => {
chinook.sendCommands("USE DATABASE :memory:; select printf('%.*c', 1000, 'x') AS DDD", (error, results) => {
expect(error).toBeNull()
expect(results.numberOfColumns).toBe(1)
expect(results.numberOfRows).toBe(1)
expect(results.version == 1 || results.version == 2).toBeTruthy()
const stringrowset = results.getItem(0, 0) as string
expect(stringrowset.startsWith('xxxxxxxxxxxxx')).toBeTruthy()
expect(stringrowset).toHaveLength(1000)
done()
})
})
it('should select database', done => {
chinook.sendCommands('USE DATABASE chinook.sqlite;', (error, results) => {
expect(error).toBeNull()
expect(results.numberOfColumns).toBeUndefined()
expect(results.numberOfRows).toBeUndefined()
expect(results.version).toBeUndefined()
done()
})
})
it('should select * from tracks limit 10 (no chunks)', done => {
chinook.sendCommands('SELECT * FROM tracks LIMIT 10;', (error, results) => {
expect(error).toBeNull()
expect(results.numberOfColumns).toBe(9)
expect(results.numberOfRows).toBe(10)
done()
})
})
it('should select * from tracks (with chunks)', done => {
chinook.sendCommands('SELECT * FROM tracks;', (error, results) => {
expect(error).toBeNull()
expect(results.numberOfColumns).toBe(9)
expect(results.numberOfRows).toBeGreaterThan(3000) // 3503 tracks but we sometimes test deleting rows
done()
})
})
it('should select * from albums', done => {
chinook.sendCommands('SELECT * FROM albums;', (error, results) => {
expect(error).toBeNull()
expect(results.numberOfColumns).toBe(3)
expect(results.numberOfRows).toBe(347)
expect(results.version == 1 || results.version == 2).toBeTruthy()
done()
})
})
it('should select without bindings', done => {
const command = { query: 'SELECT * FROM albums' } as SQLiteCloudCommand
chinook.sendCommands(command, (error, results) => {
expect(error).toBeNull()
expect(results.numberOfColumns).toBeGreaterThan(0)
done()
})
})
it('should select with bindings', done => {
const command: SQLiteCloudCommand = { query: 'SELECT * FROM albums WHERE albumId = ?', parameters: [1] }
chinook.sendCommands(command, (error, results) => {
expect(error).toBeNull()
expect(results.numberOfColumns).toBe(3)
done()
})
})
})
describe('connection stress testing', () => {
it(
'20x test string',
done => {
const numQueries = 20
let completed = 0
const startTime = Date.now()
for (let i = 0; i < numQueries; i++) {
chinook.sendCommands('TEST STRING', (error, results) => {
expect(error).toBeNull()
expect(results).toBe('Hello World, this is a test string.')
if (++completed >= numQueries) {
const queryMs = (Date.now() - startTime) / numQueries
if (queryMs > WARN_SPEED_MS) {
console.log(`${numQueries}x test string, ${queryMs.toFixed(0)}ms per query`)
expect(queryMs).toBeLessThan(EXPECT_SPEED_MS)
}
done()
}
})
}
},
LONG_TIMEOUT
)
it(
'20x individual selects',
done => {
const numQueries = 20
let completed = 0
const startTime = Date.now()
for (let i = 0; i < numQueries; i++) {
chinook.sendCommands('SELECT * FROM albums ORDER BY RANDOM() LIMIT 4;', (error, results) => {
expect(error).toBeNull()
expect(results.numberOfColumns).toBe(3)
expect(results.numberOfRows).toBe(4)
if (++completed >= numQueries) {
const queryMs = (Date.now() - startTime) / numQueries
if (queryMs > WARN_SPEED_MS) {
console.log(`${numQueries}x individual selects, ${queryMs.toFixed(0)}ms per query`)
expect(queryMs).toBeLessThan(EXPECT_SPEED_MS)
}
done()
}
})
}
},
LONG_TIMEOUT
)
it(
'10x batched selects',
done => {
const numQueries = 10
let completed = 0
const startTime = Date.now()
for (let i = 0; i < numQueries; i++) {
chinook.sendCommands(
'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;',
(error, results) => {
expect(error).toBeNull()
// server only returns the last rowset?
expect(results.numberOfColumns).toBe(3)
expect(results.numberOfRows).toBe(4)
if (++completed >= numQueries) {
const queryMs = (Date.now() - startTime) / numQueries
if (queryMs > WARN_SPEED_MS) {
console.log(`${numQueries}x batched selects, ${queryMs.toFixed(0)}ms per query`)
expect(queryMs).toBeLessThan(EXPECT_SPEED_MS)
}
done()
}
}
)
}
},
LONG_TIMEOUT
)
})
})
})