Skip to content

Commit d299741

Browse files
committed
better docs + a test
1 parent a6dc894 commit d299741

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

Sources/PostgresNIO/New/PostgresRowSequence.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,14 @@ extension PostgresRowSequence {
8585
}
8686

8787
/// Consumes all rows and returns the query metadata.
88+
///
89+
/// If you don't need the query metadata, just use the for-try-await-loop syntax:
90+
/// ```swift
91+
/// for try await row in myPostgresSequence {
92+
/// /// Process each row
93+
/// }
94+
/// ```
95+
///
8896
/// - Parameter onRow: Processes each row.
8997
/// - Returns: The query metadata.
9098
public func consume(

Tests/IntegrationTests/AsyncTests.swift

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import Atomics
12
import Logging
23
import XCTest
34
import PostgresNIO
@@ -46,6 +47,32 @@ final class AsyncPostgresConnectionTests: XCTestCase {
4647
}
4748
}
4849

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+
4976
func testSelect10kRowsAndCollect() async throws {
5077
let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
5178
defer { XCTAssertNoThrow(try eventLoopGroup.syncShutdownGracefully()) }
@@ -63,6 +90,7 @@ final class AsyncPostgresConnectionTests: XCTestCase {
6390
XCTAssertEqual(element, counter + 1)
6491
counter += 1
6592
}
93+
6694
XCTAssertEqual(metadata.command, "SELECT")
6795
XCTAssertEqual(metadata.oid, nil)
6896
XCTAssertEqual(metadata.rows, 10000)

0 commit comments

Comments
 (0)