Skip to content

Commit cd40320

Browse files
committed
Merge branch 'test-util-connections' into 'master'
Change test utils RedisConnection process to be less opinionated. See merge request Mordil/swift-redi-stack!74
2 parents 30a0774 + a09c434 commit cd40320

File tree

10 files changed

+58
-13
lines changed

10 files changed

+58
-13
lines changed

Sources/RediStackTestUtils/Extensions/RedisNIO.swift renamed to Sources/RediStackTestUtils/Extensions/RediStack.swift

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ import NIO
1717
import RediStack
1818

1919
extension RedisConnection {
20+
/// A default hostname of `localhost` to try and connect to Redis at.
21+
public static let defaultHostname = "localhost"
22+
2023
/// Creates a connection intended for tests using `REDIS_URL` and `REDIS_PW` environment variables if available.
2124
///
2225
/// The default URL is `127.0.0.1` while the default port is `RedisConnection.defaultPort`.
@@ -28,18 +31,17 @@ extension RedisConnection {
2831
/// - Returns: A `NIO.EventLoopFuture` that resolves with the new connection.
2932
public static func connect(
3033
on eventLoop: EventLoop,
31-
port: Int = RedisConnection.defaultPort
34+
host: String = RedisConnection.defaultHostname,
35+
port: Int = RedisConnection.defaultPort,
36+
password: String? = nil
3237
) -> EventLoopFuture<RedisConnection> {
33-
let env = ProcessInfo.processInfo.environment
34-
let host = env["REDIS_URL"] ?? "127.0.0.1"
35-
3638
let address: SocketAddress
3739
do {
3840
address = try SocketAddress.makeAddressResolvingHost(host, port: port)
3941
} catch {
4042
return eventLoop.makeFailedFuture(error)
4143
}
4244

43-
return RedisConnection.connect(to: address, on: eventLoop, password: env["REDIS_PW"])
45+
return RedisConnection.connect(to: address, on: eventLoop, password: password)
4446
}
4547
}

Sources/RediStackTestUtils/RedisIntegrationTestCase.swift

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,19 @@ import XCTest
2020
///
2121
/// See `RedisConnection.connect(to:port:)` to understand how connections are made.
2222
open class RedisIntegrationTestCase: XCTestCase {
23+
/// An overridable value of the Redis instance's hostname to connect to for the test suite(s).
24+
///
25+
/// The default value is `RedisConnection.defaultHostname`
26+
///
27+
/// This is especially useful to override if you build on Linux & macOS where Redis might be installed locally vs. through Docker.
28+
open var redisHostname: String { return RedisConnection.defaultHostname }
29+
30+
/// The port to connect over to Redis, defaulting to `RedisConnection.defaultPort`.
31+
open var redisPort: Int { return RedisConnection.defaultPort }
32+
33+
/// The password to use to connect to Redis. Default is `nil` - no password authentication.
34+
open var redisPassword: String? { return nil }
35+
2336
public var connection: RedisConnection!
2437

2538
private let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 2)
@@ -67,6 +80,11 @@ open class RedisIntegrationTestCase: XCTestCase {
6780
/// See `RedisConnection.connect(to:port:)`
6881
/// - Returns: The new `RediStack.RedisConnection`.
6982
public func makeNewConnection() throws -> RedisConnection {
70-
return try RedisConnection.connect(on: eventLoopGroup.next()).wait()
83+
return try RedisConnection.connect(
84+
on: eventLoopGroup.next(),
85+
host: self.redisHostname,
86+
port: self.redisPort,
87+
password: self.redisPassword
88+
).wait()
7189
}
7290
}

Tests/RediStackIntegrationTests/Commands/BasicCommandsTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import RediStackTestUtils
1717
import XCTest
1818

19-
final class BasicCommandsTests: RedisIntegrationTestCase {
19+
final class BasicCommandsTests: RediStackIntegrationTestCase {
2020
func test_select() {
2121
XCTAssertNoThrow(try connection.select(database: 3).wait())
2222
}

Tests/RediStackIntegrationTests/Commands/HashCommandsTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import RediStackTestUtils
1717
import XCTest
1818

19-
final class HashCommandsTests: RedisIntegrationTestCase {
19+
final class HashCommandsTests: RediStackIntegrationTestCase {
2020
func test_hset() throws {
2121
var result = try connection.hset("test", to: "\(#line)", in: #function).wait()
2222
XCTAssertTrue(result)

Tests/RediStackIntegrationTests/Commands/ListCommandsTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import RediStackTestUtils
1717
import XCTest
1818

19-
final class ListCommandsTests: RedisIntegrationTestCase {
19+
final class ListCommandsTests: RediStackIntegrationTestCase {
2020
func test_llen() throws {
2121
var length = try connection.llen(of: #function).wait()
2222
XCTAssertEqual(length, 0)

Tests/RediStackIntegrationTests/Commands/SetCommandsTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import RediStackTestUtils
1717
import XCTest
1818

19-
final class SetCommandsTests: RedisIntegrationTestCase {
19+
final class SetCommandsTests: RediStackIntegrationTestCase {
2020
func test_sadd() throws {
2121
var insertCount = try connection.sadd([1, 2, 3], to: #function).wait()
2222
XCTAssertEqual(insertCount, 3)

Tests/RediStackIntegrationTests/Commands/SortedSetCommandsTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import NIO
1717
import RediStackTestUtils
1818
import XCTest
1919

20-
final class SortedSetCommandsTests: RedisIntegrationTestCase {
20+
final class SortedSetCommandsTests: RediStackIntegrationTestCase {
2121
private static let testKey = "SortedSetCommandsTests"
2222

2323
private var key: String { return SortedSetCommandsTests.testKey }

Tests/RediStackIntegrationTests/Commands/StringCommandsTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import RediStackTestUtils
1717
import XCTest
1818

19-
final class StringCommandsTests: RedisIntegrationTestCase {
19+
final class StringCommandsTests: RediStackIntegrationTestCase {
2020
private static let testKey = "SortedSetCommandsTests"
2121

2222
func test_get() throws {
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the RediStack open source project
4+
//
5+
// Copyright (c) 2019 RediStack project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of RediStack project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
import class Foundation.ProcessInfo
16+
import RediStackTestUtils
17+
18+
class RediStackIntegrationTestCase: RedisIntegrationTestCase {
19+
override var redisHostname: String {
20+
return ProcessInfo.processInfo.environment["REDIS_URL"] ?? "localhost"
21+
}
22+
override var redisPassword: String? {
23+
return ProcessInfo.processInfo.environment["REDIS_PW"]
24+
}
25+
}

Tests/RediStackIntegrationTests/RedisConnectionTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import RediStackTestUtils
1717
import XCTest
1818

19-
final class RedisConnectionTests: RedisIntegrationTestCase {
19+
final class RedisConnectionTests: RediStackIntegrationTestCase {
2020
static let expectedLogsMessage = "The following log(s) in this test are expected."
2121

2222
func test_unexpectedChannelClose() throws {

0 commit comments

Comments
 (0)