diff --git a/Sources/RediStackTestUtils/Extensions/RedisNIO.swift b/Sources/RediStackTestUtils/Extensions/RediStack.swift similarity index 83% rename from Sources/RediStackTestUtils/Extensions/RedisNIO.swift rename to Sources/RediStackTestUtils/Extensions/RediStack.swift index 5215d8b..8e48e30 100644 --- a/Sources/RediStackTestUtils/Extensions/RedisNIO.swift +++ b/Sources/RediStackTestUtils/Extensions/RediStack.swift @@ -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 { - 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) } } diff --git a/Sources/RediStackTestUtils/RedisIntegrationTestCase.swift b/Sources/RediStackTestUtils/RedisIntegrationTestCase.swift index f13e082..f41cbd6 100644 --- a/Sources/RediStackTestUtils/RedisIntegrationTestCase.swift +++ b/Sources/RediStackTestUtils/RedisIntegrationTestCase.swift @@ -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() } } diff --git a/Tests/RediStackIntegrationTests/Commands/BasicCommandsTests.swift b/Tests/RediStackIntegrationTests/Commands/BasicCommandsTests.swift index 972d39e..6da30f2 100644 --- a/Tests/RediStackIntegrationTests/Commands/BasicCommandsTests.swift +++ b/Tests/RediStackIntegrationTests/Commands/BasicCommandsTests.swift @@ -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()) } diff --git a/Tests/RediStackIntegrationTests/Commands/HashCommandsTests.swift b/Tests/RediStackIntegrationTests/Commands/HashCommandsTests.swift index 41ede08..e09fb90 100644 --- a/Tests/RediStackIntegrationTests/Commands/HashCommandsTests.swift +++ b/Tests/RediStackIntegrationTests/Commands/HashCommandsTests.swift @@ -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) diff --git a/Tests/RediStackIntegrationTests/Commands/ListCommandsTests.swift b/Tests/RediStackIntegrationTests/Commands/ListCommandsTests.swift index 8724726..8546eb6 100644 --- a/Tests/RediStackIntegrationTests/Commands/ListCommandsTests.swift +++ b/Tests/RediStackIntegrationTests/Commands/ListCommandsTests.swift @@ -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) diff --git a/Tests/RediStackIntegrationTests/Commands/SetCommandsTests.swift b/Tests/RediStackIntegrationTests/Commands/SetCommandsTests.swift index 5c00cd6..4b836bb 100644 --- a/Tests/RediStackIntegrationTests/Commands/SetCommandsTests.swift +++ b/Tests/RediStackIntegrationTests/Commands/SetCommandsTests.swift @@ -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) diff --git a/Tests/RediStackIntegrationTests/Commands/SortedSetCommandsTests.swift b/Tests/RediStackIntegrationTests/Commands/SortedSetCommandsTests.swift index 573d30c..7ecfb0e 100644 --- a/Tests/RediStackIntegrationTests/Commands/SortedSetCommandsTests.swift +++ b/Tests/RediStackIntegrationTests/Commands/SortedSetCommandsTests.swift @@ -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 } diff --git a/Tests/RediStackIntegrationTests/Commands/StringCommandsTests.swift b/Tests/RediStackIntegrationTests/Commands/StringCommandsTests.swift index 613aa5f..6a258ce 100644 --- a/Tests/RediStackIntegrationTests/Commands/StringCommandsTests.swift +++ b/Tests/RediStackIntegrationTests/Commands/StringCommandsTests.swift @@ -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 { diff --git a/Tests/RediStackIntegrationTests/RediStackIntegrationTestCase.swift b/Tests/RediStackIntegrationTests/RediStackIntegrationTestCase.swift new file mode 100644 index 0000000..eb35d4e --- /dev/null +++ b/Tests/RediStackIntegrationTests/RediStackIntegrationTestCase.swift @@ -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"] + } +} diff --git a/Tests/RediStackIntegrationTests/RedisConnectionTests.swift b/Tests/RediStackIntegrationTests/RedisConnectionTests.swift index 3041402..eccde8a 100644 --- a/Tests/RediStackIntegrationTests/RedisConnectionTests.swift +++ b/Tests/RediStackIntegrationTests/RedisConnectionTests.swift @@ -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 {