Files
Nathan Harris a09c434612 Change test utils RedisConnection process to be less opinionated.
Motivation:

After working with RedisKit with RediStackTestUtils as a dependency, it was realized how opinionated the module is in how RedisConnections can be created in test environments.

Modifications:

Require more information, with reasonable defaults for `RedisConnection.init()`. Provide subclass hooks for `RedisIntegrationTestCase` for implementors to make decisions for themselves at how to connect to Redis.

Result:

Users should have more freedom in how they connect to Redis in their units tests.
2019-07-28 00:21:07 -07:00

48 lines
1.7 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the RediStack open source project
//
// Copyright (c) 2019 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 Foundation
import NIO
import RediStack
extension RedisConnection {
/// A default hostname of `localhost` to try and connect to Redis at.
public static let defaultHostname = "localhost"
/// Creates a connection intended for tests using `REDIS_URL` and `REDIS_PW` environment variables if available.
///
/// The default URL is `127.0.0.1` while the default port is `RedisConnection.defaultPort`.
///
/// If `REDIS_PW` is not defined, no authentication will happen on the connection.
/// - Parameters:
/// - eventLoop: The event loop that the connection should execute on.
/// - port: The port to connect on.
/// - Returns: A `NIO.EventLoopFuture` that resolves with the new connection.
public static func connect(
on eventLoop: EventLoop,
host: String = RedisConnection.defaultHostname,
port: Int = RedisConnection.defaultPort,
password: String? = nil
) -> EventLoopFuture<RedisConnection> {
let address: SocketAddress
do {
address = try SocketAddress.makeAddressResolvingHost(host, port: port)
} catch {
return eventLoop.makeFailedFuture(error)
}
return RedisConnection.connect(to: address, on: eventLoop, password: password)
}
}