Repository navigation
Expand file tree
/
Copy pathr2-sql.test.ts
More file actions
193 lines (179 loc) · 7.54 KB
/
Copy pathr2-sql.test.ts
File metadata and controls
193 lines (179 loc) · 7.54 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
import { Database } from "bun:sqlite"
import { Effect, Fiber } from "effect"
import { TestClock } from "effect/testing"
import { expect, test } from "bun:test"
import { isDeterministicR2SqlError, queryR2SqlPages, R2SqlQueryError, type R2SqlData } from "./r2-sql"
test("includes the transport failure in the message used by sync logging", () => {
const cause = new DOMException("The operation timed out.", "TimeoutError")
const error = new R2SqlQueryError({ message: "Failed to run R2 SQL stats query", cause })
expect(error.message).toBe("Failed to run R2 SQL stats query: TimeoutError: The operation timed out.")
expect(error.cause).toBe(cause)
})
test("preserves an R2 response error and request details", () => {
const error = new R2SqlQueryError({ message: "R2 SQL stats query failed", status: 400, requestId: "test-request" })
expect(error.message).toBe("R2 SQL stats query failed")
expect(error.status).toBe(400)
expect(error.requestId).toBe("test-request")
})
// Execute the generated pagination SQL against real aggregate rows, including
// groups that share country/model prefixes across a page boundary.
for (const count of [0, 9999, 10000, 10001, 20005]) {
test(`reads all ${count} aggregate rows without losing or repeating groups`, async () => {
using db = new Database(":memory:")
db.run("CREATE TABLE aggregates (dimension TEXT, model TEXT, country TEXT, requests TEXT)")
db.transaction(() => {
const insert = db.prepare("INSERT INTO aggregates VALUES (?, ?, ?, ?)")
Array.from({ length: count }, (_, index) => {
insert.run(
index < 2 ? "geo" : "geo_model",
index < 2 ? null : `model'${Math.floor(index / 3)}`,
`country${index % 3}`,
String(index + 1),
)
})
})()
const pages: number[] = []
const rows = await Effect.runPromise(
queryR2SqlPages("SELECT * FROM aggregates", ["dimension", "model", "country"], (query) =>
Effect.sync(() => {
const rows = db.query(query).all() as R2SqlData[]
pages.push(rows.length)
return rows
}),
),
)
expect(rows).toHaveLength(count)
expect(new Set(rows.map((row) => row.requests)).size).toBe(count)
expect(rows.reduce((sum, row) => sum + Number(row.requests), 0)).toBe((count * (count + 1)) / 2)
expect(pages).toHaveLength(Math.floor(count / 10000) + 1)
expect(pages.at(-1)).toBe(count % 10000)
})
}
test("still rejects capped results without an aggregate cursor", async () => {
const result = await Effect.runPromise(
queryR2SqlPages("SELECT * FROM aggregates LIMIT 10000", undefined, () =>
Effect.succeed(Array.from({ length: 10000 }, () => ({}))),
).pipe(Effect.flip),
)
expect(result.message).toContain("10000 row limit")
})
test("propagates a later page failure instead of returning partial aggregates", async () => {
const calls: string[] = []
const error = new R2SqlQueryError({ message: "second page failed" })
const result = await Effect.runPromise(
queryR2SqlPages("SELECT * FROM aggregates", ["model"], (query) => {
calls.push(query)
return calls.length === 1
? Effect.succeed(Array.from({ length: 10000 }, (_, index) => ({ model: String(index) })))
: Effect.fail(error)
}).pipe(Effect.flip),
)
expect(result).toBe(error)
expect(calls).toHaveLength(2)
})
test("retries a transient timeout on the same page without repeating earlier rows", async () => {
const calls: string[] = []
const rows = await Effect.runPromise(
queryR2SqlPages("SELECT * FROM aggregates", ["model"], (query) => {
calls.push(query)
if (calls.length === 1)
return Effect.succeed(Array.from({ length: 10000 }, (_, index) => ({ model: String(index).padStart(5, "0") })))
if (calls.length === 2)
return Effect.fail(new R2SqlQueryError({ message: "query timeout", status: 400, code: 40005 }))
return Effect.succeed([{ model: "10000" }])
}),
)
expect(rows).toHaveLength(10001)
expect(new Set(rows.map((row) => row.model)).size).toBe(10001)
expect(calls).toHaveLength(3)
expect(calls[1]).toBe(calls[2])
expect(calls[0]).not.toBe(calls[1])
}, 10000)
for (const [name, error] of [
[
"an intermittent 60104 response",
new R2SqlQueryError({ message: "Unprocessable Entity", status: 422, code: 60104 }),
],
[
"a dropped connection",
new R2SqlQueryError({
message: "Failed to run R2 SQL stats query",
cause: new TypeError("The socket connection was closed unexpectedly"),
}),
],
] as const) {
test(`retries ${name} on the same page`, async () => {
const calls: string[] = []
const rows = await Effect.runPromise(
Effect.gen(function* () {
const running = yield* queryR2SqlPages("SELECT * FROM aggregates", undefined, (query) => {
calls.push(query)
return calls.length === 1 ? Effect.fail(error) : Effect.succeed([{ model: "a" }])
}).pipe(Effect.forkChild({ startImmediately: true }))
yield* Effect.yieldNow
yield* TestClock.adjust("5 seconds")
return yield* Fiber.join(running)
}).pipe(Effect.provide(TestClock.layer())),
)
expect(rows).toEqual([{ model: "a" }])
expect(calls).toHaveLength(2)
expect(calls[1]).toBe(calls[0])
})
}
test("does not retry the request timeout", async () => {
const calls: string[] = []
const error = new R2SqlQueryError({
message: "Failed to run R2 SQL stats query",
cause: new DOMException("The operation timed out.", "TimeoutError"),
})
const result = await Effect.runPromise(
queryR2SqlPages("SELECT * FROM aggregates", undefined, (query) => {
calls.push(query)
return Effect.fail(error)
}).pipe(Effect.flip),
)
expect(result).toBe(error)
expect(calls).toHaveLength(1)
})
test("bounds transient retries and preserves the final error", async () => {
const calls: string[] = []
const error = new R2SqlQueryError({ message: "unavailable", status: 503 })
const result = await Effect.runPromise(
queryR2SqlPages("SELECT * FROM aggregates", undefined, (query) => {
calls.push(query)
return Effect.fail(error)
}).pipe(Effect.flip),
)
expect(calls).toHaveLength(3)
expect(result).toBe(error)
}, 20000)
test("treats rejected queries, rejected credentials and row-limit results as deterministic", async () => {
const rowLimit = await Effect.runPromise(
queryR2SqlPages("SELECT * FROM aggregates LIMIT 10000", undefined, () =>
Effect.succeed(Array.from({ length: 10000 }, () => ({}))),
).pipe(Effect.flip),
)
expect(
[
rowLimit,
new R2SqlQueryError({ message: "syntax error", status: 400 }),
new R2SqlQueryError({ message: "unauthorized", status: 401 }),
new R2SqlQueryError({ message: "forbidden", status: 403 }),
].map(isDeterministicR2SqlError),
).toEqual([true, true, true, true])
})
test("leaves transient and unexplained failures retryable", () => {
expect(
[
new R2SqlQueryError({ message: "query timeout", status: 400, code: 40005 }),
new R2SqlQueryError({ message: "Unprocessable Entity", status: 422, code: 60104 }),
new R2SqlQueryError({ message: "rate limited", status: 429 }),
new R2SqlQueryError({ message: "unavailable", status: 503 }),
new R2SqlQueryError({ message: "Failed to run R2 SQL stats query", cause: new TypeError("connection closed") }),
new R2SqlQueryError({
message: "Failed to run R2 SQL stats query",
cause: new DOMException("The operation timed out.", "TimeoutError"),
}),
].map(isDeterministicR2SqlError),
).toEqual([false, false, false, false, false, false])
})