forked from vapor/postgres-nio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAsyncTests.swift
340 lines (282 loc) · 13.2 KB
/
AsyncTests.swift
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
import Logging
import XCTest
import PostgresNIO
#if canImport(Network)
import NIOTransportServices
#endif
import NIOPosix
import NIOCore
final class AsyncPostgresConnectionTests: XCTestCase {
func test1kRoundTrips() async throws {
let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
defer { XCTAssertNoThrow(try eventLoopGroup.syncShutdownGracefully()) }
let eventLoop = eventLoopGroup.next()
try await withTestConnection(on: eventLoop) { connection in
for _ in 0..<1_000 {
let rows = try await connection.query("SELECT version()", logger: .psqlTest)
var iterator = rows.makeAsyncIterator()
let firstRow = try await iterator.next()
XCTAssertEqual(try firstRow?.decode(String.self, context: .default).contains("PostgreSQL"), true)
let done = try await iterator.next()
XCTAssertNil(done)
}
}
}
func testSelect10kRows() async throws {
let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
defer { XCTAssertNoThrow(try eventLoopGroup.syncShutdownGracefully()) }
let eventLoop = eventLoopGroup.next()
let start = 1
let end = 10000
try await withTestConnection(on: eventLoop) { connection in
let rows = try await connection.query("SELECT generate_series(\(start), \(end));", logger: .psqlTest)
var counter = 0
for try await element in rows.decode(Int.self, context: .default) {
XCTAssertEqual(element, counter + 1)
counter += 1
}
XCTAssertEqual(counter, end)
}
}
func testSelectActiveConnection() async throws {
let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
defer { XCTAssertNoThrow(try eventLoopGroup.syncShutdownGracefully()) }
let eventLoop = eventLoopGroup.next()
let query: PostgresQuery = """
SELECT
pid
,datname
,usename
,application_name
,client_hostname
,client_port
,backend_start
,query_start
,query
,state
FROM pg_stat_activity
WHERE state = 'active';
"""
try await withTestConnection(on: eventLoop) { connection in
let rows = try await connection.query(query, logger: .psqlTest)
var counter = 0
for try await element in rows.decode((Int, String, String, String, String?, Int, Date, Date, String, String).self) {
XCTAssertEqual(element.1, env("POSTGRES_DB") ?? "test_database")
XCTAssertEqual(element.2, env("POSTGRES_USER") ?? "test_username")
XCTAssertEqual(element.8, query.sql)
XCTAssertEqual(element.9, "active")
counter += 1
}
XCTAssertGreaterThanOrEqual(counter, 1)
}
}
func testSelectTimeoutWhileLongRunningQuery() async throws {
let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
defer { XCTAssertNoThrow(try eventLoopGroup.syncShutdownGracefully()) }
let eventLoop = eventLoopGroup.next()
let start = 1
let end = 10000000
try await withTestConnection(on: eventLoop) { connection -> () in
try await connection.query("SET statement_timeout=1000;", logger: .psqlTest)
let rows = try await connection.query("SELECT generate_series(\(start), \(end));", logger: .psqlTest)
var counter = 0
do {
for try await element in rows.decode(Int.self, context: .default) {
XCTAssertEqual(element, counter + 1)
counter += 1
}
XCTFail("Expected to get cancelled while reading the query")
} catch {
guard let error = error as? PSQLError else { return XCTFail("Unexpected error type") }
XCTAssertEqual(error.code, .server)
XCTAssertEqual(error.serverInfo?[.severity], "ERROR")
}
XCTAssertFalse(connection.isClosed, "Connection should survive!")
for num in 0..<10 {
for try await decoded in try await connection.query("SELECT \(num);", logger: .psqlTest).decode(Int.self) {
XCTAssertEqual(decoded, num)
}
}
}
}
func testConnectionSurvives1kQueriesWithATypo() async throws {
let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
defer { XCTAssertNoThrow(try eventLoopGroup.syncShutdownGracefully()) }
let eventLoop = eventLoopGroup.next()
let start = 1
let end = 10000
try await withTestConnection(on: eventLoop) { connection -> () in
for _ in 0..<1000 {
do {
try await connection.query("SELECT generte_series(\(start), \(end));", logger: .psqlTest)
XCTFail("Expected to throw from the request")
} catch {
guard let error = error as? PSQLError else { return XCTFail("Unexpected error type: \(error)") }
XCTAssertEqual(error.code, .server)
XCTAssertEqual(error.serverInfo?[.severity], "ERROR")
}
}
// the connection survived all of this, we can still run normal queries:
for num in 0..<10 {
for try await decoded in try await connection.query("SELECT \(num);", logger: .psqlTest).decode(Int.self) {
XCTAssertEqual(decoded, num)
}
}
}
}
func testSelect10times10kRows() async throws {
let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
defer { XCTAssertNoThrow(try eventLoopGroup.syncShutdownGracefully()) }
let eventLoop = eventLoopGroup.next()
let start = 1
let end = 10000
try await withTestConnection(on: eventLoop) { connection in
await withThrowingTaskGroup(of: Void.self) { taskGroup in
for _ in 0..<10 {
taskGroup.addTask {
try await connection.query("SELECT generate_series(\(start), \(end));", logger: .psqlTest)
}
}
}
}
}
func testBindMaximumParameters() async throws {
let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
defer { XCTAssertNoThrow(try eventLoopGroup.syncShutdownGracefully()) }
let eventLoop = eventLoopGroup.next()
try await withTestConnection(on: eventLoop) { connection in
// Max binds limit is UInt16.max which is 65535 which is 3 * 5 * 17 * 257
// Max columns limit is 1664, so we will only make 5 * 257 columns which is less
// Then we will insert 3 * 17 rows
// In the insertion, there will be a total of 3 * 17 * 5 * 257 == UInt16.max bindings
// If the test is successful, it means Postgres supports UInt16.max bindings
let columnsCount = 5 * 257
let rowsCount = 3 * 17
let createQuery = PostgresQuery(
unsafeSQL: """
CREATE TABLE table1 (
\((0..<columnsCount).map({ #""int\#($0)" int NOT NULL"# }).joined(separator: ", "))
);
"""
)
try await connection.query(createQuery, logger: .psqlTest)
var binds = PostgresBindings(capacity: Int(UInt16.max))
for _ in (0..<rowsCount) {
for num in (0..<columnsCount) {
binds.append(num, context: .default)
}
}
XCTAssertEqual(binds.count, Int(UInt16.max))
let insertionValues = (0..<rowsCount).map { rowIndex in
let indices = (0..<columnsCount).map { columnIndex -> String in
"$\(rowIndex * columnsCount + columnIndex + 1)"
}
return "(\(indices.joined(separator: ", ")))"
}.joined(separator: ", ")
let insertionQuery = PostgresQuery(
unsafeSQL: "INSERT INTO table1 VALUES \(insertionValues)",
binds: binds
)
try await connection.query(insertionQuery, logger: .psqlTest)
let countQuery = PostgresQuery(unsafeSQL: "SELECT COUNT(*) FROM table1")
let countRows = try await connection.query(countQuery, logger: .psqlTest)
var countIterator = countRows.makeAsyncIterator()
let insertedRowsCount = try await countIterator.next()?.decode(Int.self, context: .default)
XCTAssertEqual(rowsCount, insertedRowsCount)
let dropQuery = PostgresQuery(unsafeSQL: "DROP TABLE table1")
try await connection.query(dropQuery, logger: .psqlTest)
}
}
func testListenAndNotify() async throws {
let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
defer { XCTAssertNoThrow(try eventLoopGroup.syncShutdownGracefully()) }
let eventLoop = eventLoopGroup.next()
try await self.withTestConnection(on: eventLoop) { connection in
let stream = try await connection.listen("foo")
var iterator = stream.makeAsyncIterator()
try await self.withTestConnection(on: eventLoop) { other in
try await other.query(#"NOTIFY foo, 'bar';"#, logger: .psqlTest)
try await other.query(#"NOTIFY foo, 'foo';"#, logger: .psqlTest)
}
let first = try await iterator.next()
XCTAssertEqual(first?.payload, "bar")
let second = try await iterator.next()
XCTAssertEqual(second?.payload, "foo")
}
}
#if canImport(Network)
func testSelect10kRowsNetworkFramework() async throws {
let eventLoopGroup = NIOTSEventLoopGroup()
defer { XCTAssertNoThrow(try eventLoopGroup.syncShutdownGracefully()) }
let eventLoop = eventLoopGroup.next()
let start = 1
let end = 10000
try await withTestConnection(on: eventLoop) { connection in
let rows = try await connection.query("SELECT generate_series(\(start), \(end));", logger: .psqlTest)
var counter = 1
for try await element in rows.decode(Int.self, context: .default) {
XCTAssertEqual(element, counter)
counter += 1
}
XCTAssertEqual(counter, end + 1)
}
}
#endif
func testCancelTaskThatIsVeryLongRunningWhichAlsoFailsWhileInStreamingMode() async throws {
let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
defer { XCTAssertNoThrow(try eventLoopGroup.syncShutdownGracefully()) }
let eventLoop = eventLoopGroup.next()
// we cancel the query after 400ms.
// the server times out the query after 1sec.
try await withTestConnection(on: eventLoop) { connection -> () in
try await connection.query("SET statement_timeout=1000;", logger: .psqlTest) // 1000 milliseconds
try await withThrowingTaskGroup(of: Void.self) { group in
group.addTask {
let start = 1
let end = 100_000_000
let rows = try await connection.query("SELECT generate_series(\(start), \(end));", logger: .psqlTest)
var counter = 0
do {
for try await element in rows.decode(Int.self, context: .default) {
XCTAssertEqual(element, counter + 1)
counter += 1
}
XCTFail("Expected to get cancelled while reading the query")
XCTAssertEqual(counter, end)
} catch let error as CancellationError {
XCTAssertGreaterThanOrEqual(counter, 1)
// Expected
print("\(error)")
} catch {
XCTFail("Unexpected error: \(error)")
}
XCTAssertTrue(Task.isCancelled)
XCTAssertFalse(connection.isClosed, "Connection should survive!")
}
let delay: UInt64 = 400_000_000 // 400 milliseconds
try await Task.sleep(nanoseconds: delay)
group.cancelAll()
}
try await connection.query("SELECT 1;", logger: .psqlTest)
}
}
}
extension XCTestCase {
func withTestConnection<Result>(
on eventLoop: EventLoop,
file: StaticString = #filePath,
line: UInt = #line,
_ closure: (PostgresConnection) async throws -> Result
) async throws -> Result {
let connection = try await PostgresConnection.test(on: eventLoop).get()
do {
let result = try await closure(connection)
try await connection.close()
return result
} catch {
XCTFail("Unexpected error: \(String(reflecting: error))", file: file, line: line)
try await connection.close()
throw error
}
}
}