-
-
Notifications
You must be signed in to change notification settings - Fork 299
Expand file tree
/
Copy pathconfig.test.ts
More file actions
231 lines (176 loc) · 6.51 KB
/
Copy pathconfig.test.ts
File metadata and controls
231 lines (176 loc) · 6.51 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
import { vi } from 'vitest'
const CONFIG_ENV_KEYS = [
'MULTI_TENANT',
'IS_MULTITENANT',
'TENANT_POOL_CACHE_TTL_MS',
'TENANT_POOL_CACHE_HIT_LOG_SAMPLE_RATE',
'TENANT_POOL_CACHE_MISS_LOG_SAMPLE_RATE',
'DATABASE_POOL_DRAIN_TIMEOUT',
'DATABASE_HEALTHCHECK_UNSCOPED',
'DATABASE_ENGINE',
'REQUEST_HARD_LIMITS_ENABLED',
'STORAGE_S3_REQUEST_CHECKSUM_CALCULATION',
'STORAGE_S3_RESPONSE_CHECKSUM_VALIDATION',
] as const
type ConfigEnvKey = (typeof CONFIG_ENV_KEYS)[number]
const originalEnv = new Map<ConfigEnvKey, string | undefined>()
function setConfigEnv(env: Partial<Record<ConfigEnvKey, string>>) {
for (const key of CONFIG_ENV_KEYS) {
delete process.env[key]
}
process.env.MULTI_TENANT = 'true'
for (const [key, value] of Object.entries(env)) {
process.env[key] = value
}
}
describe('tenant pool cache config parsing', () => {
beforeAll(() => {
for (const key of CONFIG_ENV_KEYS) {
originalEnv.set(key, process.env[key])
}
})
afterEach(() => {
for (const key of CONFIG_ENV_KEYS) {
const value = originalEnv.get(key)
if (value === undefined) {
delete process.env[key]
} else {
process.env[key] = value
}
}
vi.resetModules()
})
test('defaults tenant pool cache settings', async () => {
setConfigEnv({})
const { getConfig } = await import('./config')
const config = getConfig({ reload: true })
expect(config.tenantPoolCacheTtlMs).toBe(1000 * 10)
expect(config.tenantPoolCacheHitLogSampleRate).toBe(0)
expect(config.tenantPoolCacheMissLogSampleRate).toBe(0)
expect(config.databasePoolDrainTimeout).toBe(30_000)
expect(config.requestHardLimitsEnabled).toBe(false)
})
test.each([
'postgres',
'oriole',
'multigres',
] as const)('parses the %s database engine', async (databaseEngine) => {
setConfigEnv({ DATABASE_ENGINE: databaseEngine })
const { getConfig } = await import('./config')
const config = getConfig({ reload: true })
expect(config.databaseEngine).toBe(databaseEngine)
})
test('parses request hard limits as disabled by default', async () => {
setConfigEnv({})
const { getConfig } = await import('./config')
const config = getConfig({ reload: true })
expect(config.requestHardLimitsEnabled).toBe(false)
})
test('enables request hard limits from env', async () => {
setConfigEnv({
REQUEST_HARD_LIMITS_ENABLED: 'true',
})
const { getConfig } = await import('./config')
const config = getConfig({ reload: true })
expect(config.requestHardLimitsEnabled).toBe(true)
})
test('does not force S3 checksum config by default', async () => {
setConfigEnv({})
const { getConfig } = await import('./config')
const config = getConfig({ reload: true })
expect(config.storageS3RequestChecksumCalculation).toBeUndefined()
expect(config.storageS3ResponseChecksumValidation).toBeUndefined()
})
test('parses split S3 checksum config independently', async () => {
setConfigEnv({
STORAGE_S3_REQUEST_CHECKSUM_CALCULATION: 'WHEN_SUPPORTED',
STORAGE_S3_RESPONSE_CHECKSUM_VALIDATION: 'WHEN_REQUIRED',
})
const { getConfig } = await import('./config')
const config = getConfig({ reload: true })
expect(config.storageS3RequestChecksumCalculation).toBe('WHEN_SUPPORTED')
expect(config.storageS3ResponseChecksumValidation).toBe('WHEN_REQUIRED')
})
test('parses database pool drain timeout in milliseconds', async () => {
setConfigEnv({
DATABASE_POOL_DRAIN_TIMEOUT: '45000',
})
const { getConfig } = await import('./config')
const config = getConfig({ reload: true })
expect(config.databasePoolDrainTimeout).toBe(45_000)
})
test('disables unscoped database healthchecks by default', async () => {
setConfigEnv({})
const { getConfig } = await import('./config')
const config = getConfig({ reload: true })
expect(config.databaseHealthcheckUnscoped).toBe(false)
})
test('enables unscoped database healthchecks from env', async () => {
setConfigEnv({ DATABASE_HEALTHCHECK_UNSCOPED: 'true' })
const { getConfig } = await import('./config')
const config = getConfig({ reload: true })
expect(config.databaseHealthcheckUnscoped).toBe(true)
})
test.each([
'0',
'-1',
'nope',
])('falls back to the default database pool drain timeout for %s', async (timeout) => {
setConfigEnv({
DATABASE_POOL_DRAIN_TIMEOUT: timeout,
})
const { getConfig } = await import('./config')
const config = getConfig({ reload: true })
expect(config.databasePoolDrainTimeout).toBe(30_000)
})
test('parses tenant pool cache ttl in milliseconds', async () => {
setConfigEnv({
TENANT_POOL_CACHE_TTL_MS: '30000',
})
const { getConfig } = await import('./config')
const config = getConfig({ reload: true })
expect(config.tenantPoolCacheTtlMs).toBe(30_000)
})
test.each([
'0',
'-1',
'nope',
])('falls back to the default tenant pool cache ttl for %s', async (ttl) => {
setConfigEnv({
TENANT_POOL_CACHE_TTL_MS: ttl,
})
const { getConfig } = await import('./config')
const config = getConfig({ reload: true })
expect(config.tenantPoolCacheTtlMs).toBe(1000 * 10)
})
test('parses fractional tenant pool cache log sample rates', async () => {
setConfigEnv({
TENANT_POOL_CACHE_HIT_LOG_SAMPLE_RATE: '0.25',
TENANT_POOL_CACHE_MISS_LOG_SAMPLE_RATE: '0.75',
})
const { getConfig } = await import('./config')
const config = getConfig({ reload: true })
expect(config.tenantPoolCacheHitLogSampleRate).toBe(0.25)
expect(config.tenantPoolCacheMissLogSampleRate).toBe(0.75)
})
test('clamps tenant pool cache log sample rates to zero and one', async () => {
setConfigEnv({
TENANT_POOL_CACHE_HIT_LOG_SAMPLE_RATE: '-0.5',
TENANT_POOL_CACHE_MISS_LOG_SAMPLE_RATE: '1.5',
})
const { getConfig } = await import('./config')
const config = getConfig({ reload: true })
expect(config.tenantPoolCacheHitLogSampleRate).toBe(0)
expect(config.tenantPoolCacheMissLogSampleRate).toBe(1)
})
test('falls back to default tenant pool cache log sample rates for invalid values', async () => {
setConfigEnv({
TENANT_POOL_CACHE_HIT_LOG_SAMPLE_RATE: 'nope',
TENANT_POOL_CACHE_MISS_LOG_SAMPLE_RATE: 'Infinity',
})
const { getConfig } = await import('./config')
const config = getConfig({ reload: true })
expect(config.tenantPoolCacheHitLogSampleRate).toBe(0)
expect(config.tenantPoolCacheMissLogSampleRate).toBe(0)
})
})