Skip to content

Commit 944706b

Browse files
authored
Support remote close (vapor#110)
* support remote close * fix fluent tests * bump * fix fluent test
1 parent d21f869 commit 944706b

File tree

5 files changed

+33
-22
lines changed

5 files changed

+33
-22
lines changed

.github/workflows/test.yml

+10-4
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,14 @@ jobs:
4444
container:
4545
image: vapor/swift:5.2
4646
services:
47-
psql:
47+
postgres-a:
48+
image: postgres
49+
env:
50+
POSTGRES_USER: vapor_username
51+
POSTGRES_DB: vapor_database
52+
POSTGRES_PASSWORD: vapor_password
53+
postgres-b:
4854
image: postgres
49-
ports:
50-
- 5432:5432
5155
env:
5256
POSTGRES_USER: vapor_username
5357
POSTGRES_DB: vapor_database
@@ -61,4 +65,6 @@ jobs:
6165
- run: swift test --enable-test-discovery --sanitize=thread
6266
working-directory: ./fluent-postgres-driver
6367
env:
64-
POSTGRES_HOSTNAME: psql
68+
POSTGRES_HOSTNAME_A: postgres-a
69+
POSTGRES_HOSTNAME_B: postgres-b
70+

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
DerivedData
66
Package.resolved
77
.swiftpm
8-
8+
Tests/LinuxMain.swift

Sources/PostgresNIO/Connection/PostgresConnection.swift

+3-11
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,23 @@ public final class PostgresConnection {
1414

1515
public var logger: Logger
1616

17-
private var didClose: Bool
18-
1917
public var isClosed: Bool {
2018
return !self.channel.isActive
2119
}
2220

2321
init(channel: Channel, logger: Logger) {
2422
self.channel = channel
2523
self.logger = logger
26-
self.didClose = false
2724
}
2825

2926
public func close() -> EventLoopFuture<Void> {
30-
guard !self.didClose else {
31-
return self.eventLoop.makeSucceededFuture(())
32-
}
33-
self.didClose = true
34-
if !self.isClosed {
35-
return self.channel.close(mode: .all)
36-
} else {
27+
guard !self.isClosed else {
3728
return self.eventLoop.makeSucceededFuture(())
3829
}
30+
return self.channel.close(mode: .all)
3931
}
4032

4133
deinit {
42-
assert(self.didClose, "PostgresConnection deinitialized before being closed.")
34+
assert(self.isClosed, "PostgresConnection deinitialized before being closed.")
4335
}
4436
}

Tests/PostgresNIOTests/PostgresNIOTests.swift

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Logging
2-
import PostgresNIO
2+
@testable import PostgresNIO
33
import XCTest
44
import NIOTestUtils
55

@@ -914,6 +914,11 @@ final class PostgresNIOTests: XCTestCase {
914914
XCTFail("Should have failed")
915915
} catch PostgresError.connectionClosed { }
916916
}
917+
918+
func testRemoteClose() throws {
919+
let conn = try PostgresConnection.test(on: eventLoop).wait()
920+
try conn.channel.close().wait()
921+
}
917922
}
918923

919924
func env(_ name: String) -> String? {

Tests/PostgresNIOTests/Utilities.swift

+13-5
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,7 @@ import XCTest
44

55
extension PostgresConnection {
66
static func address() throws -> SocketAddress {
7-
#if os(Linux)
8-
return try .makeAddressResolvingHost("psql", port: 5432)
9-
#else
10-
return try .init(ipAddress: "127.0.0.1", port: 5432)
11-
#endif
7+
try .makeAddressResolvingHost(hostname, port: 5432)
128
}
139

1410
static func testUnauthenticated(on eventLoop: EventLoop) -> EventLoopFuture<PostgresConnection> {
@@ -36,6 +32,18 @@ extension PostgresConnection {
3632
}
3733
}
3834

35+
var hostname: String {
36+
if let hostname = env("POSTGRES_HOSTNAME") {
37+
return hostname
38+
} else {
39+
#if os(Linux)
40+
return "psql"
41+
#else
42+
return "localhost"
43+
#endif
44+
}
45+
}
46+
3947
extension XCTestCase {
4048

4149
public static var shouldRunLongRunningTests: Bool {

0 commit comments

Comments
 (0)