Skip to content

Support remote close #110

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,14 @@ jobs:
container:
image: vapor/swift:5.2
services:
psql:
postgres-a:
image: postgres
env:
POSTGRES_USER: vapor_username
POSTGRES_DB: vapor_database
POSTGRES_PASSWORD: vapor_password
postgres-b:
image: postgres
ports:
- 5432:5432
env:
POSTGRES_USER: vapor_username
POSTGRES_DB: vapor_database
Expand All @@ -61,4 +65,6 @@ jobs:
- run: swift test --enable-test-discovery --sanitize=thread
working-directory: ./fluent-postgres-driver
env:
POSTGRES_HOSTNAME: psql
POSTGRES_HOSTNAME_A: postgres-a
POSTGRES_HOSTNAME_B: postgres-b

2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
DerivedData
Package.resolved
.swiftpm

Tests/LinuxMain.swift
14 changes: 3 additions & 11 deletions Sources/PostgresNIO/Connection/PostgresConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,23 @@ public final class PostgresConnection {

public var logger: Logger

private var didClose: Bool

public var isClosed: Bool {
return !self.channel.isActive
}

init(channel: Channel, logger: Logger) {
self.channel = channel
self.logger = logger
self.didClose = false
}

public func close() -> EventLoopFuture<Void> {
guard !self.didClose else {
return self.eventLoop.makeSucceededFuture(())
}
self.didClose = true
if !self.isClosed {
return self.channel.close(mode: .all)
} else {
guard !self.isClosed else {
return self.eventLoop.makeSucceededFuture(())
}
return self.channel.close(mode: .all)
}

deinit {
assert(self.didClose, "PostgresConnection deinitialized before being closed.")
assert(self.isClosed, "PostgresConnection deinitialized before being closed.")
}
}
7 changes: 6 additions & 1 deletion Tests/PostgresNIOTests/PostgresNIOTests.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Logging
import PostgresNIO
@testable import PostgresNIO
import XCTest
import NIOTestUtils

Expand Down Expand Up @@ -914,6 +914,11 @@ final class PostgresNIOTests: XCTestCase {
XCTFail("Should have failed")
} catch PostgresError.connectionClosed { }
}

func testRemoteClose() throws {
let conn = try PostgresConnection.test(on: eventLoop).wait()
try conn.channel.close().wait()
}
}

func env(_ name: String) -> String? {
Expand Down
18 changes: 13 additions & 5 deletions Tests/PostgresNIOTests/Utilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@ import XCTest

extension PostgresConnection {
static func address() throws -> SocketAddress {
#if os(Linux)
return try .makeAddressResolvingHost("psql", port: 5432)
#else
return try .init(ipAddress: "127.0.0.1", port: 5432)
#endif
try .makeAddressResolvingHost(hostname, port: 5432)
}

static func testUnauthenticated(on eventLoop: EventLoop) -> EventLoopFuture<PostgresConnection> {
Expand Down Expand Up @@ -36,6 +32,18 @@ extension PostgresConnection {
}
}

var hostname: String {
if let hostname = env("POSTGRES_HOSTNAME") {
return hostname
} else {
#if os(Linux)
return "psql"
#else
return "localhost"
#endif
}
}

extension XCTestCase {

public static var shouldRunLongRunningTests: Bool {
Expand Down