@contember/database 1.2.0-alpha.18 → 1.2.0-alpha.19

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.
Files changed (34) hide show
  1. package/dist/src/client/Connection.d.ts.map +1 -1
  2. package/dist/src/client/Connection.js +0 -3
  3. package/dist/src/client/Connection.js.map +1 -1
  4. package/dist/src/client/EventManager.d.ts +1 -4
  5. package/dist/src/client/EventManager.d.ts.map +1 -1
  6. package/dist/src/client/EventManager.js +0 -2
  7. package/dist/src/client/EventManager.js.map +1 -1
  8. package/dist/src/client/PgClient.d.ts +1 -1
  9. package/dist/src/client/PgClient.d.ts.map +1 -1
  10. package/dist/src/client/Pool.d.ts +12 -10
  11. package/dist/src/client/Pool.d.ts.map +1 -1
  12. package/dist/src/client/Pool.js +45 -37
  13. package/dist/src/client/Pool.js.map +1 -1
  14. package/dist/src/client/errors.d.ts +3 -1
  15. package/dist/src/client/errors.d.ts.map +1 -1
  16. package/dist/src/client/errors.js +3 -2
  17. package/dist/src/client/errors.js.map +1 -1
  18. package/dist/src/tsconfig.tsbuildinfo +1 -1
  19. package/dist/src/utils/Mutex.js +2 -2
  20. package/dist/src/utils/Mutex.js.map +1 -1
  21. package/dist/tests/cases/unit/createConnectionMockAlt.js +2 -2
  22. package/dist/tests/cases/unit/createConnectionMockAlt.js.map +1 -1
  23. package/dist/tests/cases/unit/pool.test.js +49 -12
  24. package/dist/tests/cases/unit/pool.test.js.map +1 -1
  25. package/dist/tests/tsconfig.tsbuildinfo +1 -1
  26. package/package.json +3 -3
  27. package/src/client/Connection.ts +0 -3
  28. package/src/client/EventManager.ts +0 -4
  29. package/src/client/PgClient.ts +1 -1
  30. package/src/client/Pool.ts +56 -39
  31. package/src/client/errors.ts +10 -2
  32. package/src/utils/Mutex.ts +1 -1
  33. package/tests/cases/unit/createConnectionMockAlt.ts +1 -1
  34. package/tests/cases/unit/pool.test.ts +50 -12
@@ -1,7 +1,7 @@
1
1
  import { expect, it, beforeAll } from 'vitest'
2
2
  import { ClientErrorCodes, Pool, PoolLogger } from '../../../src'
3
3
  import { Client as PgClient } from 'pg'
4
- import EventEmitter from 'events'
4
+ import EventEmitter from 'node:events'
5
5
 
6
6
  const createPoolLogger = () => {
7
7
  const logger: PoolLogger & {messages: string; clear: () => void} = (message, pool) => {
@@ -283,14 +283,12 @@ it('fails to reconnect on recoverable error', async () => {
283
283
  const pool = new Pool(() => pgClientMock as unknown as PgClient, {
284
284
  log: logger,
285
285
  logError: () => null,
286
- reconnectIntervalMs: 5,
287
- acquireTimeoutMs: 10,
288
- })
289
- pool.on('error', () => {
286
+ reconnectIntervalMs: 20,
287
+ acquireTimeoutMs: 30,
290
288
  })
291
289
  pgClientMock.connections.push(createRecoverableErrorPromise(), createRecoverableErrorPromise())
292
290
  await expect(async () => await pool.acquire()).rejects.toThrowError('Failed to acquire a connection. Last error: too many connection')
293
- await timeout(2)
291
+ await timeout(20)
294
292
  expect(logger.messages).toMatchInlineSnapshot(`
295
293
  CAI P
296
294
  000 1: Item added to a queue.
@@ -300,8 +298,10 @@ it('fails to reconnect on recoverable error', async () => {
300
298
  000 1: Retrying
301
299
  000 1: Creating a new connection
302
300
  100 1: Connection error occurred: too many connection
303
- 100 1: Recoverable error, max retries reached.
304
- 000 0: Queued item timed out
301
+ 100 1: Recoverable error, retrying in a moment.
302
+ 100 0: Queued item timed out
303
+ 000 0: Retrying
304
+ 000 0: Not connecting, queue is empty.
305
305
  `)
306
306
  })
307
307
 
@@ -313,12 +313,9 @@ it('fails to reconnect on unrecoverable error', async () => {
313
313
  const pool = new Pool(() => pgClientMock as unknown as PgClient, {
314
314
  log: logger,
315
315
  logError: () => null,
316
- })
317
- pool.on('error', () => {
318
-
319
316
  })
320
317
  pgClientMock.connections.push(createErrorPromise())
321
- await expect(async () => await pool.acquire()).rejects.toThrowError('Database client error: my err')
318
+ await expect(async () => await pool.acquire()).rejects.toThrowError('Database client connection error: my err')
322
319
  await timeout()
323
320
  expect(logger.messages).toMatchInlineSnapshot(`
324
321
  CAI P
@@ -359,3 +356,44 @@ const PgClientMock = class extends EventEmitter {
359
356
  return createSuccessfulPromise()
360
357
  }
361
358
  }
359
+
360
+
361
+ it('rate limit', async () => {
362
+ const logger = createPoolLogger()
363
+ const pool = new Pool(() => new PgClientMock() as unknown as PgClient, {
364
+ rateLimitCount: 1,
365
+ rateLimitPeriodMs: 50,
366
+ log: logger,
367
+ logError: () => null,
368
+ })
369
+ const conn1 = await pool.acquire()
370
+ setTimeout(async () => {
371
+ await pool.release(conn1)
372
+ }, 20)
373
+ const conn2 = await pool.acquire()
374
+ await pool.dispose(conn2)
375
+ await timeout(10)
376
+ const conn3 = await pool.acquire()
377
+ expect(logger.messages).toMatchInlineSnapshot(`
378
+ CAI P
379
+ 000 1: Item added to a queue.
380
+ 000 1: Creating a new connection
381
+ 100 1: Connection established
382
+ 010 0: Queued item fulfilled with new connection.
383
+ 010 0: Not connecting, queue is empty.
384
+ 010 1: Item added to a queue.
385
+ 010 1: Not connecting, rate limit reached.
386
+ 010 1: Releasing a connection.
387
+ 010 0: Queued item fulfilled with released connection.
388
+ 010 0: Releasing and disposing a connection.
389
+ 000 0: Not connecting, queue is empty.
390
+ 000 0: Connection errored and was disposed.
391
+ 000 1: Item added to a queue.
392
+ 000 1: Not connecting, rate limit reached.
393
+ 000 1: Rate limit renewed.
394
+ 000 1: Creating a new connection
395
+ 100 1: Connection established
396
+ 010 0: Queued item fulfilled with new connection.
397
+ 010 0: Not connecting, queue is empty.
398
+ `)
399
+ })