Files
RediStack/Sources/RediStackTestUtils/RedisConnectionPoolIntegrationTestCase.swift
T
Nathan Harris 42e8d4b127 Allow repeated commands to same connection in pool
Motivation:

Some Redis commands are very connection specific that have impacts on future access that makes it difficult in the current
checkout-use-return cycle that `RedisConnectionPool` uses.

Developers need a way to borrow a specific connection, chain several commands together, and then return the connection to the pool.

Modifications:

- Add: `leaseConnection` method to `RedisConnectionPool` which provides a connection from the pool and returns it after a provided closure's ELF resolves
- Add: `allowSubscriptions` property to `RedisConnection` for controlling the ability to make PubSub subscriptions
- Add: `RedisClientError.pubsubNotAllowed` case for when `RedisConnection.allowSubscriptions` is set to `false` and a subscription was still attempted

Result:

Developers should now have an "escape hatch" with `RedisConnectionPool` to do limited exclusive chains of operations on a specific connection.
2020-10-15 13:39:58 -07:00

95 lines
3.4 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the RediStack open source project
//
// Copyright (c) 2020 RediStack project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of RediStack project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import NIO
import RediStack
import XCTest
/// A helper `XCTestCase` subclass that does the standard work of creating a connection pool to use in test cases.
///
/// This is essentially the pooled version of `RedisIntegrationTestCase`
open class RedisConnectionPoolIntegrationTestCase: XCTestCase {
/// An overridable value of the Redis instance's hostname to connect to for the test suite(s).
///
/// The default value is `RedisConnection.defaultHostname`
///
/// This is especially useful to override if you build on Linux & macOS where Redis might be installed locally vs. through Docker.
open var redisHostname: String { return RedisConnection.defaultHostname }
/// The port to connect over to Redis, defaulting to `RedisConnection.defaultPort`.
open var redisPort: Int { return RedisConnection.defaultPort }
/// The password to use to connect to Redis. Default is `nil` - no password authentication.
open var redisPassword: String? { return nil }
public var pool: RedisConnectionPool!
public let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 3)
deinit {
do {
try self.eventLoopGroup.syncShutdownGracefully()
} catch {
print("Failed to gracefully shutdown ELG: \(error)")
}
}
/// Creates a `RediStack.RedisConnectionPool` for the next test case, calling `fatalError` if it was not successful.
///
/// See `XCTest.XCTestCase.setUp()`
open override func setUp() {
do {
self.pool = try self.makeNewPool()
} catch {
fatalError("Failed to make a RedisConnectionPool: \(error)")
}
}
/// Sends a "FLUSHALL" command to Redis to clear it of any data from the previous test, then closes the connection.
///
/// If any steps fail, a `fatalError` is thrown.
///
/// See `XCTest.XCTestCase.tearDown()`
open override func tearDown() {
do {
_ = try self.pool.send(command: "FLUSHALL").wait()
} catch let err as RedisConnectionPoolError where err == .poolClosed {
// Ok, this is fine.
} catch {
fatalError("Failed to clean up the pool: \(error)")
}
self.pool.close()
self.pool = nil
}
public func makeNewPool(
connectionRetryTimeout: TimeAmount? = .seconds(5),
minimumConnectionCount: Int = 0
) throws -> RedisConnectionPool {
let address = try SocketAddress.makeAddressResolvingHost(self.redisHostname, port: self.redisPort)
let pool = RedisConnectionPool(
serverConnectionAddresses: [address],
loop: self.eventLoopGroup.next(),
maximumConnectionCount: .maximumActiveConnections(4),
minimumConnectionCount: minimumConnectionCount,
connectionPassword: self.redisPassword,
connectionRetryTimeout: connectionRetryTimeout
)
pool.activate()
return pool
}
}