@@ -36,13 +36,45 @@ final class AsyncPostgresConnectionTests: XCTestCase {
3636
3737 try await withTestConnection ( on: eventLoop) { connection in
3838 let rows = try await connection. query ( " SELECT generate_series( \( start) , \( end) ); " , logger: . psqlTest)
39- var counter = 1
39+ var counter = 0
4040 for try await element in rows. decode ( Int . self, context: . default) {
41- XCTAssertEqual ( element, counter)
41+ XCTAssertEqual ( element, counter + 1 )
4242 counter += 1
4343 }
4444
45- XCTAssertEqual ( counter, end + 1 )
45+ XCTAssertEqual ( counter, end)
46+ }
47+ }
48+
49+ func testSelectTimeoutWhileLongRunningQuery( ) async throws {
50+ let eventLoopGroup = MultiThreadedEventLoopGroup ( numberOfThreads: 1 )
51+ defer { XCTAssertNoThrow ( try eventLoopGroup. syncShutdownGracefully ( ) ) }
52+ let eventLoop = eventLoopGroup. next ( )
53+
54+ let start = 1
55+ let end = 10000000
56+
57+ try await withTestConnection ( on: eventLoop) { connection -> ( ) in
58+ try await connection. query ( " SET statement_timeout=1000; " , logger: . psqlTest)
59+
60+ let rows = try await connection. query ( " SELECT generate_series( \( start) , \( end) ); " , logger: . psqlTest)
61+ var counter = 0
62+ do {
63+ for try await element in rows. decode ( Int . self, context: . default) {
64+ XCTAssertEqual ( element, counter + 1 )
65+ counter += 1
66+ }
67+ XCTFail ( " Expected to get cancelled while reading the query " )
68+ } catch {
69+ guard let error = error as? PSQLError else { return XCTFail ( " Unexpected error type " ) }
70+
71+ print ( error)
72+
73+ XCTAssertEqual ( error. code, . server)
74+ XCTAssertEqual ( error. serverInfo ? [ . severity] , " ERROR " )
75+ }
76+
77+ XCTAssertFalse ( connection. isClosed, " Connection should survive! " )
4678 }
4779 }
4880
0 commit comments