Skip to content

Commit 3b3416a

Browse files
authored
Ensure no interference with other services in tests (#142)
1 parent fa93d76 commit 3b3416a

File tree

2 files changed

+30
-23
lines changed

2 files changed

+30
-23
lines changed

Tests/PostgresNIOTests/New/IntegrationTests.swift

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,6 @@ final class IntegrationTests: XCTestCase {
1515
XCTAssertNoThrow(try conn?.close().wait())
1616
}
1717

18-
func testConnectionFailure() {
19-
// TODO: Create server with port 0 first
20-
21-
let config = PSQLConnection.Configuration(
22-
host: env("POSTGRES_HOSTNAME") ?? "localhost",
23-
port: 1234, // wrong port number!
24-
username: env("POSTGRES_USER") ?? "postgres",
25-
database: env("POSTGRES_DB"),
26-
password: env("POSTGRES_PASSWORD"),
27-
tlsConfiguration: nil)
28-
29-
let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
30-
defer { XCTAssertNoThrow(try eventLoopGroup.syncShutdownGracefully()) }
31-
32-
var logger = Logger.psqlTest
33-
logger.logLevel = .trace
34-
35-
XCTAssertThrowsError(try PSQLConnection.connect(configuration: config, logger: logger, on: eventLoopGroup.next()).wait()) {
36-
XCTAssertTrue($0 is PSQLError)
37-
}
38-
}
39-
4018
func testAuthenticationFailure() throws {
4119
// If the postgres server trusts every connection, it is really hard to create an
4220
// authentication failure.

Tests/PostgresNIOTests/New/PSQLConnectionTests.swift

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,34 @@ import Logging
55

66
class PSQLConnectionTests: XCTestCase {
77

8-
8+
func testConnectionFailure() {
9+
// We start a local server and close it immediately to ensure that the port
10+
// number we try to connect to is not used by any other process.
11+
let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
12+
defer { XCTAssertNoThrow(try eventLoopGroup.syncShutdownGracefully()) }
13+
14+
var tempChannel: Channel?
15+
XCTAssertNoThrow(tempChannel = try ServerBootstrap(group: eventLoopGroup)
16+
.bind(to: .init(ipAddress: "127.0.0.1", port: 0)).wait())
17+
let maybePort = tempChannel?.localAddress?.port
18+
XCTAssertNoThrow(try tempChannel?.close().wait())
19+
guard let port = maybePort else {
20+
return XCTFail("Could not get port number from temp started server")
21+
}
22+
23+
let config = PSQLConnection.Configuration(
24+
host: "127.0.0.1",
25+
port: port,
26+
username: "postgres",
27+
database: "postgres",
28+
password: "abc123",
29+
tlsConfiguration: nil)
30+
31+
var logger = Logger.psqlTest
32+
logger.logLevel = .trace
33+
34+
XCTAssertThrowsError(try PSQLConnection.connect(configuration: config, logger: logger, on: eventLoopGroup.next()).wait()) {
35+
XCTAssertTrue($0 is PSQLError)
36+
}
37+
}
938
}

0 commit comments

Comments
 (0)