|
| 1 | +import Atomics |
1 | 2 | import Logging |
2 | 3 | import XCTest |
3 | 4 | import PostgresNIO |
@@ -46,6 +47,58 @@ final class AsyncPostgresConnectionTests: XCTestCase { |
46 | 47 | } |
47 | 48 | } |
48 | 49 |
|
| 50 | + func testSelect10kRowsAndConsume() async throws { |
| 51 | + let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1) |
| 52 | + defer { XCTAssertNoThrow(try eventLoopGroup.syncShutdownGracefully()) } |
| 53 | + let eventLoop = eventLoopGroup.next() |
| 54 | + |
| 55 | + let start = 1 |
| 56 | + let end = 10000 |
| 57 | + |
| 58 | + try await withTestConnection(on: eventLoop) { connection in |
| 59 | + let rows = try await connection.query("SELECT generate_series(\(start), \(end));", logger: .psqlTest) |
| 60 | + |
| 61 | + let counter = ManagedAtomic(0) |
| 62 | + let metadata = try await rows.consume { row in |
| 63 | + let element = try row.decode(Int.self) |
| 64 | + let newCounter = counter.wrappingIncrementThenLoad(ordering: .relaxed) |
| 65 | + XCTAssertEqual(element, newCounter) |
| 66 | + } |
| 67 | + |
| 68 | + XCTAssertEqual(metadata.command, "SELECT") |
| 69 | + XCTAssertEqual(metadata.oid, nil) |
| 70 | + XCTAssertEqual(metadata.rows, 10000) |
| 71 | + |
| 72 | + XCTAssertEqual(counter.load(ordering: .relaxed), end) |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + func testSelect10kRowsAndCollect() async throws { |
| 77 | + let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1) |
| 78 | + defer { XCTAssertNoThrow(try eventLoopGroup.syncShutdownGracefully()) } |
| 79 | + let eventLoop = eventLoopGroup.next() |
| 80 | + |
| 81 | + let start = 1 |
| 82 | + let end = 10000 |
| 83 | + |
| 84 | + try await withTestConnection(on: eventLoop) { connection in |
| 85 | + let rows = try await connection.query("SELECT generate_series(\(start), \(end));", logger: .psqlTest) |
| 86 | + let (metadata, elements) = try await rows.collectWithMetadata() |
| 87 | + var counter = 0 |
| 88 | + for row in elements { |
| 89 | + let element = try row.decode(Int.self) |
| 90 | + XCTAssertEqual(element, counter + 1) |
| 91 | + counter += 1 |
| 92 | + } |
| 93 | + |
| 94 | + XCTAssertEqual(metadata.command, "SELECT") |
| 95 | + XCTAssertEqual(metadata.oid, nil) |
| 96 | + XCTAssertEqual(metadata.rows, 10000) |
| 97 | + |
| 98 | + XCTAssertEqual(counter, end) |
| 99 | + } |
| 100 | + } |
| 101 | + |
49 | 102 | func testSelectActiveConnection() async throws { |
50 | 103 | let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1) |
51 | 104 | defer { XCTAssertNoThrow(try eventLoopGroup.syncShutdownGracefully()) } |
@@ -207,7 +260,7 @@ final class AsyncPostgresConnectionTests: XCTestCase { |
207 | 260 |
|
208 | 261 | try await withTestConnection(on: eventLoop) { connection in |
209 | 262 | // Max binds limit is UInt16.max which is 65535 which is 3 * 5 * 17 * 257 |
210 | | - // Max columns limit is 1664, so we will only make 5 * 257 columns which is less |
| 263 | + // Max columns limit appears to be ~1600, so we will only make 5 * 257 columns which is less |
211 | 264 | // Then we will insert 3 * 17 rows |
212 | 265 | // In the insertion, there will be a total of 3 * 17 * 5 * 257 == UInt16.max bindings |
213 | 266 | // If the test is successful, it means Postgres supports UInt16.max bindings |
@@ -241,13 +294,9 @@ final class AsyncPostgresConnectionTests: XCTestCase { |
241 | 294 | unsafeSQL: "INSERT INTO table1 VALUES \(insertionValues)", |
242 | 295 | binds: binds |
243 | 296 | ) |
244 | | - try await connection.query(insertionQuery, logger: .psqlTest) |
245 | | - |
246 | | - let countQuery = PostgresQuery(unsafeSQL: "SELECT COUNT(*) FROM table1") |
247 | | - let countRows = try await connection.query(countQuery, logger: .psqlTest) |
248 | | - var countIterator = countRows.makeAsyncIterator() |
249 | | - let insertedRowsCount = try await countIterator.next()?.decode(Int.self, context: .default) |
250 | | - XCTAssertEqual(rowsCount, insertedRowsCount) |
| 297 | + let result = try await connection.query(insertionQuery, logger: .psqlTest) |
| 298 | + let metadata = try await result.collectWithMetadata().metadata |
| 299 | + XCTAssertEqual(metadata.rows, rowsCount) |
251 | 300 |
|
252 | 301 | let dropQuery = PostgresQuery(unsafeSQL: "DROP TABLE table1") |
253 | 302 | try await connection.query(dropQuery, logger: .psqlTest) |
|
0 commit comments