@contember/database 1.1.0-rc.3 → 1.2.0-alpha.3
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.
- package/dist/src/client/Connection.d.ts +11 -17
- package/dist/src/client/Connection.d.ts.map +1 -1
- package/dist/src/client/Connection.js +21 -20
- package/dist/src/client/Connection.js.map +1 -1
- package/dist/src/client/Pool.d.ts +125 -0
- package/dist/src/client/Pool.d.ts.map +1 -0
- package/dist/src/client/Pool.js +368 -0
- package/dist/src/client/Pool.js.map +1 -0
- package/dist/src/client/SingleConnection.d.ts +3 -2
- package/dist/src/client/SingleConnection.d.ts.map +1 -1
- package/dist/src/client/SingleConnection.js +16 -10
- package/dist/src/client/SingleConnection.js.map +1 -1
- package/dist/src/client/errors.d.ts +1 -1
- package/dist/src/client/errors.d.ts.map +1 -1
- package/dist/src/client/errors.js +7 -5
- package/dist/src/client/errors.js.map +1 -1
- package/dist/src/client/execution.d.ts +0 -1
- package/dist/src/client/execution.d.ts.map +1 -1
- package/dist/src/client/execution.js +1 -10
- package/dist/src/client/execution.js.map +1 -1
- package/dist/src/client/index.d.ts +1 -0
- package/dist/src/client/index.d.ts.map +1 -1
- package/dist/src/client/index.js +1 -0
- package/dist/src/client/index.js.map +1 -1
- package/dist/src/index.d.ts +1 -1
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +3 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/tsconfig.tsbuildinfo +1 -1
- package/dist/src/types.d.ts +4 -1
- package/dist/src/types.d.ts.map +1 -1
- package/dist/src/utils/asyncIterableTransaction.d.ts +7 -0
- package/dist/src/utils/asyncIterableTransaction.d.ts.map +1 -0
- package/dist/src/utils/asyncIterableTransaction.js +20 -0
- package/dist/src/utils/asyncIterableTransaction.js.map +1 -0
- package/dist/src/utils/createDatabaseIfNotExists.d.ts +2 -2
- package/dist/src/utils/createDatabaseIfNotExists.d.ts.map +1 -1
- package/dist/src/utils/createDatabaseIfNotExists.js +2 -2
- package/dist/src/utils/createDatabaseIfNotExists.js.map +1 -1
- package/dist/src/utils/index.d.ts +2 -0
- package/dist/src/utils/index.d.ts.map +1 -1
- package/dist/src/utils/index.js +2 -0
- package/dist/src/utils/index.js.map +1 -1
- package/dist/src/utils/pgClientFactory.d.ts +5 -0
- package/dist/src/utils/pgClientFactory.d.ts.map +1 -0
- package/dist/src/utils/pgClientFactory.js +12 -0
- package/dist/src/utils/pgClientFactory.js.map +1 -0
- package/dist/src/utils/sql.d.ts +1 -1
- package/dist/src/utils/sql.d.ts.map +1 -1
- package/dist/tests/cases/unit/pool.test.d.ts +2 -0
- package/dist/tests/cases/unit/pool.test.d.ts.map +1 -0
- package/dist/tests/cases/unit/pool.test.js +327 -0
- package/dist/tests/cases/unit/pool.test.js.map +1 -0
- package/dist/tests/tsconfig.tsbuildinfo +1 -1
- package/package.json +4 -4
- package/src/client/Connection.ts +29 -31
- package/src/client/Pool.ts +455 -0
- package/src/client/SingleConnection.ts +18 -11
- package/src/client/errors.ts +10 -6
- package/src/client/execution.ts +0 -9
- package/src/client/index.ts +1 -0
- package/src/index.ts +2 -0
- package/src/types.ts +4 -1
- package/src/utils/asyncIterableTransaction.ts +21 -0
- package/src/utils/createDatabaseIfNotExists.ts +5 -5
- package/src/utils/index.ts +2 -0
- package/src/utils/pgClientFactory.ts +10 -0
- package/tests/cases/unit/pool.test.ts +355 -0
package/src/client/execution.ts
CHANGED
|
@@ -9,7 +9,6 @@ import {
|
|
|
9
9
|
SerializationFailureError,
|
|
10
10
|
TransactionAbortedError,
|
|
11
11
|
UniqueViolationError,
|
|
12
|
-
ClientError,
|
|
13
12
|
} from './errors'
|
|
14
13
|
import { ClientErrorCodes } from './errorCodes'
|
|
15
14
|
|
|
@@ -83,11 +82,3 @@ export async function executeQuery<Row extends Record<string, any>>(
|
|
|
83
82
|
}
|
|
84
83
|
}
|
|
85
84
|
}
|
|
86
|
-
|
|
87
|
-
export const executeClientOperation = async <T>(cb: () => Promise<T>): Promise<T> => {
|
|
88
|
-
try {
|
|
89
|
-
return await cb()
|
|
90
|
-
} catch (e) {
|
|
91
|
-
throw new ClientError(e)
|
|
92
|
-
}
|
|
93
|
-
}
|
package/src/client/index.ts
CHANGED
package/src/index.ts
CHANGED
|
@@ -12,9 +12,11 @@ export * from './queryable'
|
|
|
12
12
|
export * from './Literal'
|
|
13
13
|
export * from './types'
|
|
14
14
|
export {
|
|
15
|
+
asyncIterableTransaction,
|
|
15
16
|
wrapIdentifier,
|
|
16
17
|
retryTransaction,
|
|
17
18
|
ConstraintHelper,
|
|
18
19
|
withDatabaseAdvisoryLock,
|
|
19
20
|
createDatabaseIfNotExists,
|
|
21
|
+
createPgClientFactory,
|
|
20
22
|
} from './utils'
|
package/src/types.ts
CHANGED
|
@@ -15,11 +15,14 @@ export interface Raw {
|
|
|
15
15
|
bindings: Value[]
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
-
export interface
|
|
18
|
+
export interface DatabaseConfig {
|
|
19
19
|
readonly host: string
|
|
20
20
|
readonly port: number
|
|
21
21
|
readonly user: string
|
|
22
22
|
readonly password: string
|
|
23
23
|
readonly database: string
|
|
24
24
|
readonly ssl?: boolean
|
|
25
|
+
readonly queryTimeoutMs?: number
|
|
26
|
+
readonly statementTimeoutMs?: number
|
|
27
|
+
readonly connectionTimeoutMs?: number
|
|
25
28
|
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { Client, Connection } from '../client/index.js'
|
|
2
|
+
|
|
3
|
+
interface AsyncIterableX<T, TReturn = any, TNext = undefined> {
|
|
4
|
+
[Symbol.asyncIterator](): AsyncIterator<T, TReturn, TNext>
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export async function* asyncIterableTransaction<T, TReturn = any, TNext = undefined>(db: Client, cb: (db2: Client<Connection.TransactionLike>) => AsyncIterableX<T, TReturn, TNext>): AsyncIterableX<T, TReturn, TNext> {
|
|
8
|
+
// eslint-disable-next-line promise/param-names
|
|
9
|
+
return yield* await new Promise(async resolveOuter => {
|
|
10
|
+
await db.transaction((db2: Client<Connection.TransactionLike>) => {
|
|
11
|
+
// eslint-disable-next-line promise/param-names
|
|
12
|
+
return new Promise<void>(resolveInner => {
|
|
13
|
+
resolveOuter((async function* () {
|
|
14
|
+
const result = yield* cb(db2)
|
|
15
|
+
resolveInner()
|
|
16
|
+
return result
|
|
17
|
+
})())
|
|
18
|
+
})
|
|
19
|
+
})
|
|
20
|
+
})
|
|
21
|
+
}
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { ClientError, ClientErrorCodes,
|
|
1
|
+
import { DatabaseConfig } from '../types'
|
|
2
|
+
import { ClientError, ClientErrorCodes, SingleConnection } from '../client'
|
|
3
3
|
import { wrapIdentifier } from './sql'
|
|
4
4
|
|
|
5
|
-
export const createDatabaseIfNotExists = async (db:
|
|
5
|
+
export const createDatabaseIfNotExists = async (db: DatabaseConfig, log: (message: string) => void) => {
|
|
6
6
|
try {
|
|
7
|
-
const connection = new
|
|
7
|
+
const connection = new SingleConnection(db, {})
|
|
8
8
|
await connection.query('SELECT 1')
|
|
9
9
|
await connection.end()
|
|
10
10
|
return
|
|
@@ -15,7 +15,7 @@ export const createDatabaseIfNotExists = async (db: DatabaseCredentials, log: (m
|
|
|
15
15
|
}
|
|
16
16
|
|
|
17
17
|
log(`Database ${db.database} does not exist, attempting to create it...`)
|
|
18
|
-
const connection = new
|
|
18
|
+
const connection = new SingleConnection({ ...db, database: 'postgres' }, {})
|
|
19
19
|
await connection.query(`CREATE DATABASE ${wrapIdentifier(db.database)}`)
|
|
20
20
|
await connection.end()
|
|
21
21
|
}
|
package/src/utils/index.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
export * from './assertNever'
|
|
2
|
+
export * from './asyncIterableTransaction'
|
|
2
3
|
export * from './ConstraintHelper'
|
|
3
4
|
export * from './createDatabaseIfNotExists'
|
|
4
5
|
export * from './sql'
|
|
5
6
|
export * from './retryTransaction'
|
|
6
7
|
export * from './withDatabaseAdvisoryLock'
|
|
8
|
+
export * from './pgClientFactory'
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Client as PgClient } from 'pg'
|
|
2
|
+
import { DatabaseConfig } from '../types'
|
|
3
|
+
|
|
4
|
+
export type PgClientFactory = () => PgClient
|
|
5
|
+
export const createPgClientFactory = ({ queryTimeoutMs, statementTimeoutMs, connectionTimeoutMs, ...config }: DatabaseConfig) => () => new PgClient({
|
|
6
|
+
query_timeout: queryTimeoutMs,
|
|
7
|
+
statement_timeout: statementTimeoutMs,
|
|
8
|
+
connectionTimeoutMillis: connectionTimeoutMs,
|
|
9
|
+
...config,
|
|
10
|
+
})
|
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
import { expect, it, beforeAll } from 'vitest'
|
|
2
|
+
import { ClientErrorCodes, Pool, PoolLogger } from '../../../src'
|
|
3
|
+
import { Client as PgClient } from 'pg'
|
|
4
|
+
import EventEmitter from 'events'
|
|
5
|
+
|
|
6
|
+
const createPoolLogger = () => {
|
|
7
|
+
const logger: PoolLogger & {messages: string; clear: () => void} = (message, pool) => {
|
|
8
|
+
logger.messages += `${pool.connecting}${pool.active}${pool.idle} ${pool.pending}: ${message}\n`
|
|
9
|
+
}
|
|
10
|
+
logger.messages = '\nCAI P\n'
|
|
11
|
+
logger.clear = () => logger.messages = 'CAI P\n'
|
|
12
|
+
return logger
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const timeout = async (ms = 1) => await new Promise(resolve => setTimeout(resolve, ms))
|
|
16
|
+
|
|
17
|
+
beforeAll(() => {
|
|
18
|
+
expect.addSnapshotSerializer({
|
|
19
|
+
test: val => typeof val === 'string',
|
|
20
|
+
print: val => String(val),
|
|
21
|
+
})
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
it('acquires and releases new connection from pool', async () => {
|
|
25
|
+
const logger = createPoolLogger()
|
|
26
|
+
const pool = new Pool(() => new PgClientMock() as unknown as PgClient, {
|
|
27
|
+
log: logger,
|
|
28
|
+
})
|
|
29
|
+
const connection = await pool.acquire()
|
|
30
|
+
await pool.release(connection)
|
|
31
|
+
await timeout()
|
|
32
|
+
expect(logger.messages).toMatchInlineSnapshot(`
|
|
33
|
+
CAI P
|
|
34
|
+
000 1: Item added to a queue.
|
|
35
|
+
000 1: Creating a new connection
|
|
36
|
+
100 1: Connection established
|
|
37
|
+
010 0: Queued item fulfilled with new connection.
|
|
38
|
+
010 0: Not connecting, queue is empty.
|
|
39
|
+
010 0: Releasing a connection.
|
|
40
|
+
001 0: Connection is idle and available.
|
|
41
|
+
`)
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
it('disposes connection when maxIdle is reached', async () => {
|
|
46
|
+
const logger = createPoolLogger()
|
|
47
|
+
const pool = new Pool(() => new PgClientMock() as unknown as PgClient, {
|
|
48
|
+
log: logger,
|
|
49
|
+
maxIdle: 0,
|
|
50
|
+
})
|
|
51
|
+
const connection = await pool.acquire()
|
|
52
|
+
await pool.release(connection)
|
|
53
|
+
await timeout()
|
|
54
|
+
expect(logger.messages).toMatchInlineSnapshot(`
|
|
55
|
+
CAI P
|
|
56
|
+
000 1: Item added to a queue.
|
|
57
|
+
000 1: Creating a new connection
|
|
58
|
+
100 1: Connection established
|
|
59
|
+
010 0: Queued item fulfilled with new connection.
|
|
60
|
+
010 0: Not connecting, queue is empty.
|
|
61
|
+
010 0: Releasing a connection.
|
|
62
|
+
000 0: Too many idle connections, connection disposed.
|
|
63
|
+
`)
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
it('disposes connection after max uses', async () => {
|
|
67
|
+
const logger = createPoolLogger()
|
|
68
|
+
const pool = new Pool(() => new PgClientMock() as unknown as PgClient, {
|
|
69
|
+
log: logger,
|
|
70
|
+
maxUses: 1,
|
|
71
|
+
})
|
|
72
|
+
const connection = await pool.acquire()
|
|
73
|
+
await pool.release(connection)
|
|
74
|
+
await timeout()
|
|
75
|
+
expect(logger.messages).toMatchInlineSnapshot(`
|
|
76
|
+
CAI P
|
|
77
|
+
000 1: Item added to a queue.
|
|
78
|
+
000 1: Creating a new connection
|
|
79
|
+
100 1: Connection established
|
|
80
|
+
010 0: Queued item fulfilled with new connection.
|
|
81
|
+
010 0: Not connecting, queue is empty.
|
|
82
|
+
010 0: Releasing a connection.
|
|
83
|
+
000 0: Not connecting, queue is empty.
|
|
84
|
+
000 0: Connection has reached max age or usage and was disposed.
|
|
85
|
+
`)
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
it('disposes errored connection', async () => {
|
|
89
|
+
const logger = createPoolLogger()
|
|
90
|
+
const pool = new Pool(() => new PgClientMock() as unknown as PgClient, {
|
|
91
|
+
log: logger,
|
|
92
|
+
maxIdle: 0,
|
|
93
|
+
})
|
|
94
|
+
const connection = await pool.acquire()
|
|
95
|
+
await pool.dispose(connection)
|
|
96
|
+
await timeout()
|
|
97
|
+
expect(logger.messages).toMatchInlineSnapshot(`
|
|
98
|
+
CAI P
|
|
99
|
+
000 1: Item added to a queue.
|
|
100
|
+
000 1: Creating a new connection
|
|
101
|
+
100 1: Connection established
|
|
102
|
+
010 0: Queued item fulfilled with new connection.
|
|
103
|
+
010 0: Not connecting, queue is empty.
|
|
104
|
+
010 0: Releasing and disposing a connection.
|
|
105
|
+
000 0: Not connecting, queue is empty.
|
|
106
|
+
000 0: Connection errored and was disposed.
|
|
107
|
+
`)
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
it('time outs when waiting for a connection', async () => {
|
|
112
|
+
const logger = createPoolLogger()
|
|
113
|
+
const clientMock = new PgClientMock()
|
|
114
|
+
clientMock.connections.push(createSuccessfulPromise(5))
|
|
115
|
+
const pool = new Pool(() => clientMock as unknown as PgClient, {
|
|
116
|
+
log: logger,
|
|
117
|
+
acquireTimeoutMs: 2,
|
|
118
|
+
})
|
|
119
|
+
await expect(pool.acquire.bind(pool)).rejects.toThrowError('Failed to acquire a connection')
|
|
120
|
+
await timeout(4)
|
|
121
|
+
|
|
122
|
+
expect(logger.messages).toMatchInlineSnapshot(`
|
|
123
|
+
CAI P
|
|
124
|
+
000 1: Item added to a queue.
|
|
125
|
+
000 1: Creating a new connection
|
|
126
|
+
100 0: Queued item timed out
|
|
127
|
+
100 0: Connection established
|
|
128
|
+
001 0: Connection is idle and available.
|
|
129
|
+
001 0: Not connecting, queue is empty.
|
|
130
|
+
`)
|
|
131
|
+
})
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
it('acquires idle connection from pool', async () => {
|
|
135
|
+
const logger = createPoolLogger()
|
|
136
|
+
const pool = new Pool(() => new PgClientMock() as unknown as PgClient, {
|
|
137
|
+
log: logger,
|
|
138
|
+
})
|
|
139
|
+
const connection = await pool.acquire()
|
|
140
|
+
await pool.release(connection)
|
|
141
|
+
logger.clear()
|
|
142
|
+
const connection2 = await pool.acquire()
|
|
143
|
+
await pool.release(connection2)
|
|
144
|
+
await timeout()
|
|
145
|
+
expect(logger.messages).toMatchInlineSnapshot(`
|
|
146
|
+
CAI P
|
|
147
|
+
010 0: Item fulfilled with an idle connection.
|
|
148
|
+
010 0: Releasing a connection.
|
|
149
|
+
001 0: Connection is idle and available.
|
|
150
|
+
`)
|
|
151
|
+
})
|
|
152
|
+
|
|
153
|
+
it('acquires multiple new connections from pool', async () => {
|
|
154
|
+
const logger = createPoolLogger()
|
|
155
|
+
const pool = new Pool(() => new PgClientMock() as unknown as PgClient, {
|
|
156
|
+
log: logger,
|
|
157
|
+
})
|
|
158
|
+
const conn1 = await pool.acquire()
|
|
159
|
+
const conn2 = await pool.acquire()
|
|
160
|
+
await pool.release(conn1)
|
|
161
|
+
await pool.release(conn2)
|
|
162
|
+
await timeout()
|
|
163
|
+
expect(logger.messages).toMatchInlineSnapshot(`
|
|
164
|
+
CAI P
|
|
165
|
+
000 1: Item added to a queue.
|
|
166
|
+
000 1: Creating a new connection
|
|
167
|
+
100 1: Connection established
|
|
168
|
+
010 0: Queued item fulfilled with new connection.
|
|
169
|
+
010 0: Not connecting, queue is empty.
|
|
170
|
+
010 1: Item added to a queue.
|
|
171
|
+
010 1: Creating a new connection
|
|
172
|
+
110 1: Connection established
|
|
173
|
+
020 0: Queued item fulfilled with new connection.
|
|
174
|
+
020 0: Not connecting, queue is empty.
|
|
175
|
+
020 0: Releasing a connection.
|
|
176
|
+
011 0: Connection is idle and available.
|
|
177
|
+
011 0: Releasing a connection.
|
|
178
|
+
002 0: Connection is idle and available.
|
|
179
|
+
`)
|
|
180
|
+
})
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
it('releases idle connection', async () => {
|
|
184
|
+
const logger = createPoolLogger()
|
|
185
|
+
const pool = new Pool(() => new PgClientMock() as unknown as PgClient, {
|
|
186
|
+
log: logger,
|
|
187
|
+
idleTimeoutMs: 2,
|
|
188
|
+
})
|
|
189
|
+
const conn1 = await pool.acquire()
|
|
190
|
+
await pool.release(conn1)
|
|
191
|
+
await timeout(4)
|
|
192
|
+
expect(logger.messages).toMatchInlineSnapshot(`
|
|
193
|
+
CAI P
|
|
194
|
+
000 1: Item added to a queue.
|
|
195
|
+
000 1: Creating a new connection
|
|
196
|
+
100 1: Connection established
|
|
197
|
+
010 0: Queued item fulfilled with new connection.
|
|
198
|
+
010 0: Not connecting, queue is empty.
|
|
199
|
+
010 0: Releasing a connection.
|
|
200
|
+
001 0: Connection is idle and available.
|
|
201
|
+
000 0: Idle connection disposed after timeout.
|
|
202
|
+
`)
|
|
203
|
+
})
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
it('waits for idle connection when pool is full', async () => {
|
|
207
|
+
const logger = createPoolLogger()
|
|
208
|
+
const pool = new Pool(() => new PgClientMock() as unknown as PgClient, {
|
|
209
|
+
maxConnections: 1,
|
|
210
|
+
log: logger,
|
|
211
|
+
})
|
|
212
|
+
const conn1 = await pool.acquire()
|
|
213
|
+
setTimeout(async () => {
|
|
214
|
+
await pool.release(conn1)
|
|
215
|
+
}, 20)
|
|
216
|
+
const conn2 = await pool.acquire()
|
|
217
|
+
await pool.release(conn2)
|
|
218
|
+
await timeout()
|
|
219
|
+
expect(logger.messages).toMatchInlineSnapshot(`
|
|
220
|
+
CAI P
|
|
221
|
+
000 1: Item added to a queue.
|
|
222
|
+
000 1: Creating a new connection
|
|
223
|
+
100 1: Connection established
|
|
224
|
+
010 0: Queued item fulfilled with new connection.
|
|
225
|
+
010 0: Not connecting, queue is empty.
|
|
226
|
+
010 1: Item added to a queue.
|
|
227
|
+
010 1: Not connecting, max connections reached.
|
|
228
|
+
010 1: Releasing a connection.
|
|
229
|
+
010 0: Queued item fulfilled with released connection.
|
|
230
|
+
010 0: Releasing a connection.
|
|
231
|
+
001 0: Connection is idle and available.
|
|
232
|
+
`)
|
|
233
|
+
})
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
it('tries to reconnect on recoverable error', async () => {
|
|
237
|
+
const logger = createPoolLogger()
|
|
238
|
+
const pgClientMock = new PgClientMock()
|
|
239
|
+
pgClientMock.on('error', () => {
|
|
240
|
+
|
|
241
|
+
})
|
|
242
|
+
const pool = new Pool(() => pgClientMock as unknown as PgClient, {
|
|
243
|
+
log: logger,
|
|
244
|
+
})
|
|
245
|
+
pgClientMock.connections.push(createRecoverableErrorPromise(), createRecoverableErrorPromise())
|
|
246
|
+
await pool.acquire()
|
|
247
|
+
await timeout()
|
|
248
|
+
expect(logger.messages).toMatchInlineSnapshot(`
|
|
249
|
+
CAI P
|
|
250
|
+
000 1: Item added to a queue.
|
|
251
|
+
000 1: Creating a new connection
|
|
252
|
+
100 1: Connection error occurred: too many connection
|
|
253
|
+
100 1: Recoverable error, retrying in a moment.
|
|
254
|
+
000 1: Retrying
|
|
255
|
+
000 1: Creating a new connection
|
|
256
|
+
100 1: Connection error occurred: too many connection
|
|
257
|
+
100 1: Recoverable error, retrying in a moment.
|
|
258
|
+
000 1: Retrying
|
|
259
|
+
000 1: Creating a new connection
|
|
260
|
+
100 1: Connection established
|
|
261
|
+
010 0: Queued item fulfilled with new connection.
|
|
262
|
+
010 0: Not connecting, queue is empty.
|
|
263
|
+
`)
|
|
264
|
+
})
|
|
265
|
+
|
|
266
|
+
|
|
267
|
+
it('fails to reconnect on recoverable error', async () => {
|
|
268
|
+
const logger = createPoolLogger()
|
|
269
|
+
const pgClientMock = new PgClientMock()
|
|
270
|
+
pgClientMock.on('error', () => {
|
|
271
|
+
|
|
272
|
+
})
|
|
273
|
+
const pool = new Pool(() => pgClientMock as unknown as PgClient, {
|
|
274
|
+
log: logger,
|
|
275
|
+
reconnectIntervalMs: 5,
|
|
276
|
+
acquireTimeoutMs: 10,
|
|
277
|
+
})
|
|
278
|
+
pool.on('error', () => {
|
|
279
|
+
})
|
|
280
|
+
pgClientMock.connections.push(createRecoverableErrorPromise(), createRecoverableErrorPromise())
|
|
281
|
+
await expect(async () => await pool.acquire()).rejects.toThrowError('Failed to acquire a connection. Last error: too many connection')
|
|
282
|
+
await timeout(2)
|
|
283
|
+
expect(logger.messages).toMatchInlineSnapshot(`
|
|
284
|
+
CAI P
|
|
285
|
+
000 1: Item added to a queue.
|
|
286
|
+
000 1: Creating a new connection
|
|
287
|
+
100 1: Connection error occurred: too many connection
|
|
288
|
+
100 1: Recoverable error, retrying in a moment.
|
|
289
|
+
000 1: Retrying
|
|
290
|
+
000 1: Creating a new connection
|
|
291
|
+
100 1: Connection error occurred: too many connection
|
|
292
|
+
100 1: Recoverable error, max retries reached.
|
|
293
|
+
000 0: Queued item timed out
|
|
294
|
+
`)
|
|
295
|
+
})
|
|
296
|
+
|
|
297
|
+
it('fails to reconnect on unrecoverable error', async () => {
|
|
298
|
+
const logger = createPoolLogger()
|
|
299
|
+
const pgClientMock = new PgClientMock()
|
|
300
|
+
const pool = new Pool(() => pgClientMock as unknown as PgClient, {
|
|
301
|
+
log: logger,
|
|
302
|
+
acquireTimeoutMs: 3,
|
|
303
|
+
})
|
|
304
|
+
pgClientMock.connections.push(createErrorPromise())
|
|
305
|
+
pool.on('error', () => {
|
|
306
|
+
|
|
307
|
+
})
|
|
308
|
+
const poolError = new Promise(resolve => pool.once('error', e => {
|
|
309
|
+
resolve(e)
|
|
310
|
+
}))
|
|
311
|
+
|
|
312
|
+
await expect(pool.acquire.bind(pool)).rejects.toThrowError('Failed to acquire a connection')
|
|
313
|
+
await expect(poolError).resolves.toEqual(new Error('my err'))
|
|
314
|
+
|
|
315
|
+
await timeout(4)
|
|
316
|
+
expect(logger.messages).toMatchInlineSnapshot(`
|
|
317
|
+
CAI P
|
|
318
|
+
000 1: Item added to a queue.
|
|
319
|
+
000 1: Creating a new connection
|
|
320
|
+
100 1: Connection error occurred: my err
|
|
321
|
+
000 1: Connecting failed, emitting error
|
|
322
|
+
000 0: Queued item timed out
|
|
323
|
+
`)
|
|
324
|
+
})
|
|
325
|
+
|
|
326
|
+
|
|
327
|
+
|
|
328
|
+
const createSuccessfulPromise = (timeout = 1) => () => new Promise<void>(resolve => {
|
|
329
|
+
setTimeout(resolve, timeout)
|
|
330
|
+
})
|
|
331
|
+
|
|
332
|
+
const createRecoverableErrorPromise = () => () => new Promise<void>((resolve, reject) => {
|
|
333
|
+
setTimeout(() => {
|
|
334
|
+
const err: Error & {code?: string} = new Error('too many connection')
|
|
335
|
+
err.code = ClientErrorCodes.TOO_MANY_CONNECTIONS
|
|
336
|
+
reject(err)
|
|
337
|
+
}, 1)
|
|
338
|
+
})
|
|
339
|
+
|
|
340
|
+
const createErrorPromise = () => () => new Promise<void>((resolve, reject) => {
|
|
341
|
+
setTimeout(() => reject(new Error('my err')), 1)
|
|
342
|
+
})
|
|
343
|
+
|
|
344
|
+
const PgClientMock = class extends EventEmitter {
|
|
345
|
+
public connections: (() => Promise<void>)[] = []
|
|
346
|
+
|
|
347
|
+
async connect() {
|
|
348
|
+
const connection = this.connections.shift()
|
|
349
|
+
return connection ? connection() : createSuccessfulPromise()
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
async end() {
|
|
353
|
+
return createSuccessfulPromise()
|
|
354
|
+
}
|
|
355
|
+
}
|