-
-
Notifications
You must be signed in to change notification settings - Fork 841
Expand file tree
/
Copy pathsqlite-cache-store-tests.js
More file actions
270 lines (225 loc) · 6.6 KB
/
Copy pathsqlite-cache-store-tests.js
File metadata and controls
270 lines (225 loc) · 6.6 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
'use strict'
const { test } = require('node:test')
const { notEqual, strictEqual, deepStrictEqual } = require('node:assert')
const { rm } = require('node:fs/promises')
const FakeTimers = require('@sinonjs/fake-timers')
const { cacheStoreTests, writeBody, compareGetResults } = require('./cache-store-test-utils.js')
const { runtimeFeatures } = require('../../lib/util/runtime-features.js')
const SqliteCacheStore = require('../../lib/cache/sqlite-cache-store.js')
cacheStoreTests(SqliteCacheStore, { skip: runtimeFeatures.has('sqlite') === false })
test('SqliteCacheStore works nicely with multiple stores', { skip: runtimeFeatures.has('sqlite') === false }, async (t) => {
const SqliteCacheStore = require('../../lib/cache/sqlite-cache-store.js')
const sqliteLocation = 'cache-interceptor.sqlite'
const storeA = new SqliteCacheStore({
location: sqliteLocation
})
const storeB = new SqliteCacheStore({
location: sqliteLocation
})
t.after(async () => {
storeA.close()
storeB.close()
await rm(sqliteLocation)
})
/**
* @type {import('../../types/cache-interceptor.d.ts').default.CacheKey}
*/
const key = {
origin: 'localhost',
path: '/',
method: 'GET',
headers: {}
}
/**
* @type {import('../../types/cache-interceptor.d.ts').default.CacheValue}
*/
const value = {
statusCode: 200,
statusMessage: '',
headers: { foo: 'bar' },
cachedAt: Date.now(),
staleAt: Date.now() + 10000,
deleteAt: Date.now() + 20000
}
const body = [Buffer.from('asd'), Buffer.from('123')]
{
const writable = storeA.createWriteStream(key, value)
notEqual(writable, undefined)
writeBody(writable, body)
}
// Make sure we got the expected response from store a
{
const result = storeA.get(structuredClone(key))
notEqual(result, undefined)
await compareGetResults(result, value, body)
}
// Make sure we got the expected response from store b
{
const result = storeB.get(structuredClone(key))
notEqual(result, undefined)
await compareGetResults(result, value, body)
}
})
test('SqliteCacheStore maxEntries', { skip: runtimeFeatures.has('sqlite') === false }, async () => {
const SqliteCacheStore = require('../../lib/cache/sqlite-cache-store.js')
const store = new SqliteCacheStore({
maxCount: 10
})
for (let i = 0; i < 20; i++) {
/**
* @type {import('../../types/cache-interceptor.d.ts').default.CacheKey}
*/
const key = {
origin: 'localhost',
path: '/' + i,
method: 'GET',
headers: {}
}
/**
* @type {import('../../types/cache-interceptor.d.ts').default.CacheValue}
*/
const value = {
statusCode: 200,
statusMessage: '',
headers: { foo: 'bar' },
cachedAt: Date.now(),
staleAt: Date.now() + 10000,
deleteAt: Date.now() + 20000
}
const body = ['asd', '123']
const writable = store.createWriteStream(key, value)
notEqual(writable, undefined)
writeBody(writable, body)
}
strictEqual(store.size <= 11, true)
})
test('SqliteCacheStore two writes', { skip: runtimeFeatures.has('sqlite') === false }, async () => {
const SqliteCacheStore = require('../../lib/cache/sqlite-cache-store.js')
const store = new SqliteCacheStore({
maxCount: 10
})
/**
* @type {import('../../types/cache-interceptor.d.ts').default.CacheKey}
*/
const key = {
origin: 'localhost',
path: '/',
method: 'GET',
headers: {}
}
/**
* @type {import('../../types/cache-interceptor.d.ts').default.CacheValue}
*/
const value = {
statusCode: 200,
statusMessage: '',
headers: { foo: 'bar' },
cachedAt: Date.now(),
staleAt: Date.now() + 10000,
deleteAt: Date.now() + 20000
}
const body = ['asd', '123']
{
const writable = store.createWriteStream(key, value)
notEqual(writable, undefined)
writeBody(writable, body)
}
{
const writable = store.createWriteStream(key, value)
notEqual(writable, undefined)
writeBody(writable, body)
}
})
test('SqliteCacheStore skips expired entry to find non-expired match', { skip: runtimeFeatures.has('sqlite') === false }, async (t) => {
const SqliteCacheStore = require('../../lib/cache/sqlite-cache-store.js')
const clock = FakeTimers.install({
shouldClearNativeTimers: true
})
t.after(() => clock.uninstall())
const store = new SqliteCacheStore({
maxCount: 100
})
const keyA = {
origin: 'localhost',
path: '/',
method: 'GET',
headers: { 'x-vary': 'a' }
}
const valueA = {
statusCode: 200,
statusMessage: '',
headers: { foo: 'bar' },
vary: { 'x-vary': 'a' },
cacheControlDirectives: {},
cachedAt: Date.now(),
staleAt: Date.now() + 1000,
deleteAt: Date.now() + 1000
}
const bodyA = [Buffer.from('first')]
{
const writable = store.createWriteStream(keyA, valueA)
notEqual(writable, undefined)
writeBody(writable, bodyA)
}
// Advance past valueA's deleteAt
clock.tick(2000)
const keyB = {
origin: 'localhost',
path: '/',
method: 'GET',
headers: { 'x-vary': 'b' }
}
const valueB = {
statusCode: 200,
statusMessage: '',
headers: { foo: 'baz' },
vary: { 'x-vary': 'b' },
cacheControlDirectives: {},
cachedAt: Date.now(),
staleAt: Date.now() + 10000,
deleteAt: Date.now() + 10000
}
const bodyB = [Buffer.from('second')]
{
const writable = store.createWriteStream(keyB, valueB)
notEqual(writable, undefined)
writeBody(writable, bodyB)
}
// The expired entry (valueA) is sorted first by deleteAt ASC; the
// store must skip it and still return the matching valueB.
const result = store.get(structuredClone(keyB))
notEqual(result, undefined)
await compareGetResults(result, valueB, bodyB)
})
test('SqliteCacheStore write & read', { skip: runtimeFeatures.has('sqlite') === false }, async () => {
const SqliteCacheStore = require('../../lib/cache/sqlite-cache-store.js')
const store = new SqliteCacheStore({
maxCount: 10
})
/**
* @type {import('../../types/cache-interceptor.d.ts').default.CacheKey}
*/
const key = {
origin: 'localhost',
path: '/',
method: 'GET',
headers: {}
}
/**
* @type {import('../../types/cache-interceptor.d.ts').default.CacheValue & { body: Buffer }}
*/
const value = {
statusCode: 200,
statusMessage: '',
headers: { foo: 'bar' },
cacheControlDirectives: { 'max-stale': 0 },
cachedAt: Date.now(),
staleAt: Date.now() + 10000,
deleteAt: Date.now() + 20000,
body: Buffer.from('asd'),
etag: undefined,
vary: undefined
}
store.set(key, value)
deepStrictEqual(store.get(key), value)
})