Merge branch 'test-util-connections' into 'master'

Change test utils RedisConnection process to be less opinionated.

See merge request Mordil/swift-redi-stack!74
This commit is contained in:
Nathan Harris
2019-07-28 07:31:25 +00:00
10 changed files with 58 additions and 13 deletions
@@ -17,6 +17,9 @@ 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`.
@@ -28,11 +31,10 @@ extension RedisConnection {
/// - Returns: A `NIO.EventLoopFuture` that resolves with the new connection.
public static func connect(
on eventLoop: EventLoop,
port: Int = RedisConnection.defaultPort
host: String = RedisConnection.defaultHostname,
port: Int = RedisConnection.defaultPort,
password: String? = nil
) -> EventLoopFuture<RedisConnection> {
let env = ProcessInfo.processInfo.environment
let host = env["REDIS_URL"] ?? "127.0.0.1"
let address: SocketAddress
do {
address = try SocketAddress.makeAddressResolvingHost(host, port: port)
@@ -40,6 +42,6 @@ extension RedisConnection {
return eventLoop.makeFailedFuture(error)
}
return RedisConnection.connect(to: address, on: eventLoop, password: env["REDIS_PW"])
return RedisConnection.connect(to: address, on: eventLoop, password: password)
}
}
@@ -20,6 +20,19 @@ import XCTest
///
/// See `RedisConnection.connect(to:port:)` to understand how connections are made.
open class RedisIntegrationTestCase: 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 connection: RedisConnection!
private let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 2)
@@ -67,6 +80,11 @@ open class RedisIntegrationTestCase: XCTestCase {
/// See `RedisConnection.connect(to:port:)`
/// - Returns: The new `RediStack.RedisConnection`.
public func makeNewConnection() throws -> RedisConnection {
return try RedisConnection.connect(on: eventLoopGroup.next()).wait()
return try RedisConnection.connect(
on: eventLoopGroup.next(),
host: self.redisHostname,
port: self.redisPort,
password: self.redisPassword
).wait()
}
}
@@ -16,7 +16,7 @@
import RediStackTestUtils
import XCTest
final class BasicCommandsTests: RedisIntegrationTestCase {
final class BasicCommandsTests: RediStackIntegrationTestCase {
func test_select() {
XCTAssertNoThrow(try connection.select(database: 3).wait())
}
@@ -16,7 +16,7 @@
import RediStackTestUtils
import XCTest
final class HashCommandsTests: RedisIntegrationTestCase {
final class HashCommandsTests: RediStackIntegrationTestCase {
func test_hset() throws {
var result = try connection.hset("test", to: "\(#line)", in: #function).wait()
XCTAssertTrue(result)
@@ -16,7 +16,7 @@
import RediStackTestUtils
import XCTest
final class ListCommandsTests: RedisIntegrationTestCase {
final class ListCommandsTests: RediStackIntegrationTestCase {
func test_llen() throws {
var length = try connection.llen(of: #function).wait()
XCTAssertEqual(length, 0)
@@ -16,7 +16,7 @@
import RediStackTestUtils
import XCTest
final class SetCommandsTests: RedisIntegrationTestCase {
final class SetCommandsTests: RediStackIntegrationTestCase {
func test_sadd() throws {
var insertCount = try connection.sadd([1, 2, 3], to: #function).wait()
XCTAssertEqual(insertCount, 3)
@@ -17,7 +17,7 @@ import NIO
import RediStackTestUtils
import XCTest
final class SortedSetCommandsTests: RedisIntegrationTestCase {
final class SortedSetCommandsTests: RediStackIntegrationTestCase {
private static let testKey = "SortedSetCommandsTests"
private var key: String { return SortedSetCommandsTests.testKey }
@@ -16,7 +16,7 @@
import RediStackTestUtils
import XCTest
final class StringCommandsTests: RedisIntegrationTestCase {
final class StringCommandsTests: RediStackIntegrationTestCase {
private static let testKey = "SortedSetCommandsTests"
func test_get() throws {
@@ -0,0 +1,25 @@
//===----------------------------------------------------------------------===//
//
// 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 class Foundation.ProcessInfo
import RediStackTestUtils
class RediStackIntegrationTestCase: RedisIntegrationTestCase {
override var redisHostname: String {
return ProcessInfo.processInfo.environment["REDIS_URL"] ?? "localhost"
}
override var redisPassword: String? {
return ProcessInfo.processInfo.environment["REDIS_PW"]
}
}
@@ -16,7 +16,7 @@
import RediStackTestUtils
import XCTest
final class RedisConnectionTests: RedisIntegrationTestCase {
final class RedisConnectionTests: RediStackIntegrationTestCase {
static let expectedLogsMessage = "The following log(s) in this test are expected."
func test_unexpectedChannelClose() throws {