-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
259 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
Tests/PostgresConnectionPoolTests/ConnectionTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
// | ||
// Created by Thomas Rasch on 11.05.23. | ||
// | ||
|
||
@testable import PostgresConnectionPool | ||
import PostgresNIO | ||
import XCTest | ||
|
||
final class ConnectionTests: XCTestCase { | ||
|
||
private var logger: Logger = { | ||
var logger = Logger(label: "ConnectionTests") | ||
logger.logLevel = .info | ||
return logger | ||
}() | ||
|
||
// MARK: - | ||
|
||
// Test that the pool can actually connect to the server. | ||
func testCanConnect() async throws { | ||
let pool = PostgresConnectionPool(configuration: PostgresHelpers.poolConfiguration(), logger: logger) | ||
|
||
do { | ||
try await pool.connection { connection in | ||
try await connection.query("SELECT 1", logger: logger) | ||
} | ||
await pool.shutdown() | ||
} | ||
catch { | ||
XCTFail("Is the cocker container running? (\(String(describing: (error as? PoolError)?.debugDescription))") | ||
} | ||
|
||
let didShutdown = await pool.isShutdown | ||
XCTAssertTrue(didShutdown) | ||
} | ||
|
||
func testPoolInfo() async throws { | ||
let initialUsageCounter = PoolConnection.globalUsageCounter | ||
let pool = PostgresConnectionPool(configuration: PostgresHelpers.poolConfiguration(), logger: logger) | ||
|
||
let poolInfoBefore = await pool.poolInfo() | ||
print(poolInfoBefore) | ||
XCTAssertEqual(poolInfoBefore.activeConnections, 0) | ||
XCTAssertEqual(poolInfoBefore.availableConnections, 0) | ||
XCTAssertEqual(poolInfoBefore.openConnections, 0) | ||
XCTAssertEqual(poolInfoBefore.usageCounter, initialUsageCounter) | ||
XCTAssertEqual(poolInfoBefore.connections.count, poolInfoBefore.openConnections) | ||
XCTAssertFalse(poolInfoBefore.isShutdown) | ||
XCTAssertNil(poolInfoBefore.shutdownError) | ||
|
||
let start = 1 | ||
let end = 1000 | ||
|
||
await withThrowingTaskGroup(of: Void.self) { taskGroup in | ||
for _ in 1 ... 1000 { | ||
taskGroup.addTask { | ||
try await pool.connection { connection in | ||
_ = try await connection.query("SELECT generate_series(\(start), \(end));", logger: self.logger) | ||
} | ||
} | ||
} | ||
} | ||
|
||
let poolInfo = await pool.poolInfo() | ||
print(poolInfo) | ||
XCTAssertEqual(poolInfo.activeConnections, 0) | ||
XCTAssertGreaterThan(poolInfo.availableConnections, 0) | ||
XCTAssertGreaterThan(poolInfo.openConnections, 0) | ||
XCTAssertEqual(poolInfo.usageCounter, 1000 + initialUsageCounter) | ||
XCTAssertEqual(poolInfo.connections.count, poolInfo.openConnections) | ||
XCTAssertFalse(poolInfo.isShutdown) | ||
XCTAssertNil(poolInfo.shutdownError) | ||
|
||
await pool.shutdown() | ||
|
||
let poolInfoAfterShutdown = await pool.poolInfo() | ||
print(poolInfoAfterShutdown) | ||
XCTAssertEqual(poolInfoAfterShutdown.activeConnections, 0) | ||
XCTAssertEqual(poolInfoAfterShutdown.availableConnections, 0) | ||
XCTAssertEqual(poolInfoAfterShutdown.openConnections, 0) | ||
XCTAssertGreaterThan(poolInfoAfterShutdown.usageCounter, 0) | ||
XCTAssertEqual(poolInfoAfterShutdown.connections.count, 0) | ||
XCTAssertTrue(poolInfoAfterShutdown.isShutdown) | ||
XCTAssertNil(poolInfoAfterShutdown.shutdownError) | ||
} | ||
|
||
func testPoolSize100() async throws { | ||
let initialUsageCounter = PoolConnection.globalUsageCounter | ||
let pool = PostgresConnectionPool(configuration: PostgresHelpers.poolConfiguration(poolSize: 100), logger: logger) | ||
|
||
let start = 1 | ||
let end = 100 | ||
|
||
await withThrowingTaskGroup(of: Void.self) { taskGroup in | ||
for _ in 1 ... 10000 { | ||
taskGroup.addTask { | ||
try await pool.connection { connection in | ||
_ = try await connection.query("SELECT generate_series(\(start), \(end));", logger: self.logger) | ||
} | ||
} | ||
} | ||
} | ||
|
||
let poolInfo = await pool.poolInfo() | ||
XCTAssertEqual(poolInfo.activeConnections, 0) | ||
XCTAssertGreaterThan(poolInfo.availableConnections, 0) | ||
XCTAssertGreaterThan(poolInfo.openConnections, 0) | ||
XCTAssertEqual(poolInfo.usageCounter, 10000 + initialUsageCounter) | ||
XCTAssertEqual(poolInfo.connections.count, poolInfo.openConnections) | ||
XCTAssertFalse(poolInfo.isShutdown) | ||
XCTAssertNil(poolInfo.shutdownError) | ||
|
||
await pool.closeIdleConnections() | ||
|
||
let poolInfoIdleClosed = await pool.poolInfo() | ||
XCTAssertEqual(poolInfoIdleClosed.activeConnections, 0) | ||
XCTAssertEqual(poolInfoIdleClosed.availableConnections, 0) | ||
XCTAssertEqual(poolInfoIdleClosed.openConnections, 0) | ||
XCTAssertEqual(poolInfoIdleClosed.usageCounter, 10000 + initialUsageCounter) | ||
XCTAssertEqual(poolInfoIdleClosed.connections.count, poolInfoIdleClosed.openConnections) | ||
XCTAssertFalse(poolInfoIdleClosed.isShutdown) | ||
XCTAssertNil(poolInfoIdleClosed.shutdownError) | ||
|
||
await pool.shutdown() | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// | ||
// Created by Thomas Rasch on 11.05.23. | ||
// | ||
|
||
import Foundation | ||
import PostgresConnectionPool | ||
import PostgresNIO | ||
|
||
enum PostgresHelpers { | ||
|
||
static func poolConfiguration( | ||
host: String? = nil, | ||
port: Int? = nil, | ||
username: String? = nil, | ||
password: String? = nil, | ||
database: String? = nil, | ||
tls: PostgresConnection.Configuration.TLS = .disable, | ||
poolSize: Int = 5) | ||
-> PoolConfiguration | ||
{ | ||
let postgresConfiguration = PostgresConnection.Configuration( | ||
host: host ?? env("POSTGRES_HOSTNAME") ?? "localhost", | ||
port: port ?? env("POSTGRES_PORT").flatMap(Int.init(_:)) ?? 5432, | ||
username: username ?? env("POSTGRES_USER") ?? "test_username", | ||
password: password ?? env("POSTGRES_PASSWORD") ?? "test_password", | ||
database: database ?? env("POSTGRES_DB") ?? "test_database", | ||
tls: tls) | ||
return PoolConfiguration( | ||
applicationName: "PoolTests", | ||
postgresConfiguration: postgresConfiguration, | ||
connectTimeout: 10.0, | ||
queryTimeout: 10.0, | ||
poolSize: poolSize, | ||
maxIdleConnections: 1) | ||
} | ||
|
||
private static func env(_ name: String) -> String? { | ||
getenv(name).flatMap { String(cString: $0) } | ||
} | ||
|
||
} |