Skip to content

Commit cd694bb

Browse files
authored
Nio 2 final (#19)
* nio 2 final * add close methods
1 parent f6b8ca6 commit cd694bb

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-8
lines changed

Sources/NIOPostgres/Connection/PostgresConnection.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import Foundation
12
public final class PostgresConnection {
23
// #warning("publicize these values?")
34
// public var status: [String: String]
@@ -22,4 +23,9 @@ public final class PostgresConnection {
2223
}
2324

2425
#warning("TODO: add error handler that closes connection")
26+
deinit {
27+
if self.channel.isActive {
28+
assertionFailure("PostgresConnection deinitialized before being closed.")
29+
}
30+
}
2531
}

Sources/NIOPostgres/Data/PostgresData+String.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,9 @@ extension PostgresData {
111111
}
112112
}
113113
}
114+
115+
extension PostgresData: ExpressibleByStringLiteral {
116+
public init(stringLiteral value: String) {
117+
self.init(string: value)
118+
}
119+
}

Tests/NIOPostgresTests/NIOPostgresTests.swift

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ final class NIOPostgresTests: XCTestCase {
2525

2626
func testSimpleQueryVersion() throws {
2727
let conn = try PostgresConnection.test(on: eventLoop).wait()
28-
defer { try? conn.close().wait() }
28+
defer { try! conn.close().wait() }
2929
let rows = try conn.simpleQuery("SELECT version()").wait()
3030
XCTAssertEqual(rows.count, 1)
3131
let version = rows[0].column("version")?.string
@@ -34,7 +34,7 @@ final class NIOPostgresTests: XCTestCase {
3434

3535
func testQueryVersion() throws {
3636
let conn = try PostgresConnection.test(on: eventLoop).wait()
37-
defer { try? conn.close().wait() }
37+
defer { try! conn.close().wait() }
3838
let rows = try conn.query("SELECT version()", .init()).wait()
3939
XCTAssertEqual(rows.count, 1)
4040
let version = rows[0].column("version")?.string
@@ -44,16 +44,16 @@ final class NIOPostgresTests: XCTestCase {
4444

4545
func testQuerySelectParameter() throws {
4646
let conn = try PostgresConnection.test(on: eventLoop).wait()
47-
defer { try? conn.close().wait() }
48-
let rows = try conn.query("SELECT $1::TEXT as foo", [.init(string: "hello")]).wait()
47+
defer { try! conn.close().wait() }
48+
let rows = try conn.query("SELECT $1::TEXT as foo", ["hello"]).wait()
4949
XCTAssertEqual(rows.count, 1)
5050
let version = rows[0].column("foo")?.string
5151
XCTAssertEqual(version, "hello")
5252
}
5353

5454
func testSQLError() throws {
5555
let conn = try PostgresConnection.test(on: eventLoop).wait()
56-
defer { try? conn.close().wait() }
56+
defer { try! conn.close().wait() }
5757
do {
5858
_ = try conn.simpleQuery("SELECT &").wait()
5959
XCTFail("An error should have been thrown")
@@ -64,12 +64,14 @@ final class NIOPostgresTests: XCTestCase {
6464

6565
func testSelectTypes() throws {
6666
let conn = try PostgresConnection.test(on: eventLoop).wait()
67+
defer { try! conn.close().wait() }
6768
let results = try conn.simpleQuery("SELECT * FROM pg_type").wait()
6869
XCTAssert(results.count >= 350, "Results count not large enough: \(results.count)")
6970
}
7071

7172
func testSelectType() throws {
7273
let conn = try PostgresConnection.test(on: eventLoop).wait()
74+
defer { try! conn.close().wait() }
7375
let results = try conn.simpleQuery("SELECT * FROM pg_type WHERE typname = 'float8'").wait()
7476
// [
7577
// "typreceive": "float8recv",
@@ -116,6 +118,7 @@ final class NIOPostgresTests: XCTestCase {
116118

117119
func testIntegers() throws {
118120
let conn = try PostgresConnection.test(on: eventLoop).wait()
121+
defer { try! conn.close().wait() }
119122
struct Integers: Decodable {
120123
let smallint: Int16
121124
let smallint_min: Int16
@@ -156,6 +159,7 @@ final class NIOPostgresTests: XCTestCase {
156159

157160
func testPi() throws {
158161
let conn = try PostgresConnection.test(on: eventLoop).wait()
162+
defer { try! conn.close().wait() }
159163
struct Pi: Decodable {
160164
let text: String
161165
let numeric_string: String
@@ -186,6 +190,7 @@ final class NIOPostgresTests: XCTestCase {
186190

187191
func testUUID() throws {
188192
let conn = try PostgresConnection.test(on: eventLoop).wait()
193+
defer { try! conn.close().wait() }
189194
struct Model: Decodable {
190195
let id: UUID
191196
let string: String
@@ -206,6 +211,7 @@ final class NIOPostgresTests: XCTestCase {
206211

207212
func testDates() throws {
208213
let conn = try PostgresConnection.test(on: eventLoop).wait()
214+
defer { try! conn.close().wait() }
209215
struct Dates: Decodable {
210216
var date: Date
211217
var timestamp: Date
@@ -250,9 +256,9 @@ final class NIOPostgresTests: XCTestCase {
250256
}
251257

252258
func testInvalidPassword() throws {
253-
let auth = PostgresConnection.testUnauthenticated(on: eventLoop).flatMap({ (connection) in
254-
connection.authenticate(username: "invalid", database: "invalid", password: "bad").map { connection }
255-
})
259+
let conn = try PostgresConnection.testUnauthenticated(on: eventLoop).wait()
260+
defer { try! conn.close().wait() }
261+
let auth = conn.authenticate(username: "invalid", database: "invalid", password: "bad")
256262
do {
257263
let _ = try auth.wait()
258264
XCTFail("The authentication should fail")
@@ -264,6 +270,7 @@ final class NIOPostgresTests: XCTestCase {
264270
func testSelectPerformance() throws {
265271
// std deviation too high
266272
let conn = try PostgresConnection.test(on: eventLoop).wait()
273+
defer { try! conn.close().wait() }
267274
measure {
268275
do {
269276
_ = try conn.query("SELECT * FROM pg_type").wait()
@@ -276,6 +283,7 @@ final class NIOPostgresTests: XCTestCase {
276283
func testRangeSelectPerformance() throws {
277284
// std deviation too high
278285
let conn = try PostgresConnection.test(on: eventLoop).wait()
286+
defer { try! conn.close().wait() }
279287
measure {
280288
do {
281289
_ = try conn.simpleQuery("SELECT * FROM generate_series(1, 10000) num").wait()
@@ -292,6 +300,7 @@ final class NIOPostgresTests: XCTestCase {
292300
}
293301

294302
let conn = try PostgresConnection.test(on: eventLoop).wait()
303+
defer { try! conn.close().wait() }
295304
measure {
296305
do {
297306
try conn.query("SELECT * FROM generate_series(1, 10000) num") { row in

0 commit comments

Comments
 (0)