Skip to content

Commit 152eb30

Browse files
authored
Merge pull request #25 from Mordil/remove-driver
Remove `RedisDriver`
2 parents ba6bf6d + db4a156 commit 152eb30

13 files changed

+72
-175
lines changed

Sources/NIORedis/Extensions/NIO/ClientBootstrap.swift

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,22 @@ import Foundation
22
import NIO
33

44
extension ClientBootstrap {
5-
/// Makes a new `ClientBootstrap` instance with a standard Redis `Channel` pipeline for sending and receiving
6-
/// messages in Redis Serialization Protocol (RESP) format.
5+
/// Makes a new `ClientBootstrap` instance with a default Redis `Channel` pipeline
6+
/// for sending and receiving messages in Redis Serialization Protocol (RESP) format.
77
///
8-
/// See `RESPEncoder`, `RESPDecoder`, and `RedisCommadHandler`.
8+
/// See `RESPEncoder`, `RESPDecoder`, and `RedisCommadHandler`
99
/// - Parameter using: The `EventLoopGroup` to build the `ClientBootstrap` on.
10-
/// - Returns: A `ClientBootstrap` with the standard configuration of a `Channel` pipeline for RESP messages.
11-
public static func makeForRedis(using group: EventLoopGroup) -> ClientBootstrap {
10+
/// - Returns: A `ClientBootstrap` with the default configuration of a `Channel` pipeline for RESP messages.
11+
public static func makeRedisDefault(using group: EventLoopGroup) -> ClientBootstrap {
1212
return ClientBootstrap(group: group)
13-
.channelOption(ChannelOptions.socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1)
14-
.channelInitializer { channel in
15-
let handlers: [ChannelHandler] = [
16-
MessageToByteHandler(RESPEncoder()),
17-
ByteToMessageHandler(RESPDecoder()),
18-
RedisCommandHandler()
19-
]
20-
return .andAllSucceed(handlers.map { channel.pipeline.addHandler($0) }, on: group.next())
21-
}
13+
.channelOption(
14+
ChannelOptions.socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR),
15+
value: 1
16+
)
17+
.channelInitializer { $0.pipeline.addHandlers([
18+
MessageToByteHandler(RESPEncoder()),
19+
ByteToMessageHandler(RESPDecoder()),
20+
RedisCommandHandler()
21+
])}
2222
}
2323
}

Sources/NIORedis/RedisClient.swift

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,31 @@ public final class RedisConnection: RedisClient {
138138
return promise.futureResult
139139
}
140140
}
141+
142+
extension RedisConnection {
143+
/// Makes a client connection to a Redis instance.
144+
/// - Parameters:
145+
/// - socket: The `SocketAddress` information of the Redis instance to connect to.
146+
/// - password: The optional password to authorize the client with.
147+
/// - eventLoopGroup: The `EventLoopGroup` to build the connection on.
148+
/// - logger: The `Logger` instance to log with.
149+
/// - Returns: A `RedisClient` instance representing this new connection.
150+
public static func connect(
151+
to socket: SocketAddress,
152+
with password: String? = nil,
153+
on eventLoopGroup: EventLoopGroup,
154+
logger: Logger = Logger(label: "NIORedis.RedisClient")
155+
) -> EventLoopFuture<RedisConnection> {
156+
let bootstrap = ClientBootstrap.makeRedisDefault(using: eventLoopGroup)
157+
158+
return bootstrap.connect(to: socket)
159+
.map { return RedisConnection(channel: $0, logger: logger) }
160+
.flatMap { client in
161+
guard let pw = password else {
162+
return eventLoopGroup.next().makeSucceededFuture(client)
163+
}
164+
return client.authorize(with: pw)
165+
.map { _ in return client }
166+
}
167+
}
168+
}

Sources/NIORedis/RedisDriver.swift

Lines changed: 0 additions & 73 deletions
This file was deleted.

Tests/NIORedisTests/Commands/BasicCommandsTests.swift

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,19 @@
22
import XCTest
33

44
final class BasicCommandsTests: XCTestCase {
5-
private let redis = RedisDriver(ownershipModel: .internal(threadCount: 1))
6-
deinit { try? redis.terminate() }
7-
85
private var connection: RedisConnection!
96

107
override func setUp() {
118
do {
12-
connection = try redis.makeConnection().wait()
9+
connection = try RedisConnection.connect().wait()
1310
} catch {
1411
XCTFail("Failed to create RedisConnection!")
1512
}
1613
}
1714

1815
override func tearDown() {
1916
_ = try? connection.send(command: "FLUSHALL").wait()
20-
connection.close()
17+
try? connection.close().wait()
2118
connection = nil
2219
}
2320

Tests/NIORedisTests/Commands/HashCommandsTests.swift

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,19 @@
22
import XCTest
33

44
final class HashCommandsTests: XCTestCase {
5-
private let redis = RedisDriver(ownershipModel: .internal(threadCount: 1))
6-
deinit { try? redis.terminate() }
7-
85
private var connection: RedisConnection!
96

107
override func setUp() {
118
do {
12-
connection = try redis.makeConnection().wait()
9+
connection = try RedisConnection.connect().wait()
1310
} catch {
1411
XCTFail("Failed to create RedisConnection!")
1512
}
1613
}
1714

1815
override func tearDown() {
1916
_ = try? connection.send(command: "FLUSHALL").wait()
20-
connection.close()
17+
try? connection.close().wait()
2118
connection = nil
2219
}
2320

Tests/NIORedisTests/Commands/ListCommandsTests.swift

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,19 @@
22
import XCTest
33

44
final class ListCommandsTests: XCTestCase {
5-
private let redis = RedisDriver(ownershipModel: .internal(threadCount: 1))
6-
deinit { try? redis.terminate() }
7-
85
private var connection: RedisConnection!
96

107
override func setUp() {
118
do {
12-
connection = try redis.makeConnection().wait()
9+
connection = try RedisConnection.connect().wait()
1310
} catch {
1411
XCTFail("Failed to create RedisConnection!")
1512
}
1613
}
1714

1815
override func tearDown() {
1916
_ = try? connection.send(command: "FLUSHALL").wait()
20-
connection.close()
17+
try? connection.close().wait()
2118
connection = nil
2219
}
2320

Tests/NIORedisTests/Commands/SetCommandsTests.swift

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,19 @@
22
import XCTest
33

44
final class SetCommandsTests: XCTestCase {
5-
private let redis = RedisDriver(ownershipModel: .internal(threadCount: 1))
6-
deinit { try? redis.terminate() }
7-
85
private var connection: RedisConnection!
96

107
override func setUp() {
118
do {
12-
connection = try redis.makeConnection().wait()
9+
connection = try RedisConnection.connect().wait()
1310
} catch {
1411
XCTFail("Failed to create RedisConnection!")
1512
}
1613
}
1714

1815
override func tearDown() {
1916
_ = try? connection.send(command: "FLUSHALL").wait()
20-
connection.close()
17+
try? connection.close().wait()
2118
connection = nil
2219
}
2320

Tests/NIORedisTests/Commands/SortedSetCommandsTests.swift

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@ import XCTest
44
final class SortedSetCommandsTests: XCTestCase {
55
private static let testKey = "SortedSetCommandsTests"
66

7-
private let redis = RedisDriver(ownershipModel: .internal(threadCount: 1))
8-
deinit { try? redis.terminate() }
9-
107
private var connection: RedisConnection!
8+
119
private var key: String { return SortedSetCommandsTests.testKey }
1210

1311
override func setUp() {
1412
do {
15-
connection = try redis.makeConnection().wait()
13+
connection = try RedisConnection.connect().wait()
1614

1715
var dataset: [(RESPValueConvertible, Double)] = []
1816
for index in 1...10 {
@@ -27,7 +25,7 @@ final class SortedSetCommandsTests: XCTestCase {
2725

2826
override func tearDown() {
2927
_ = try? connection.send(command: "FLUSHALL").wait()
30-
connection.close()
28+
try? connection.close().wait()
3129
connection = nil
3230
}
3331

Tests/NIORedisTests/Commands/StringCommandsTests.swift

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,19 @@ import XCTest
44
final class StringCommandsTests: XCTestCase {
55
private static let testKey = "SortedSetCommandsTests"
66

7-
private let redis = RedisDriver(ownershipModel: .internal(threadCount: 1))
8-
deinit { try? redis.terminate() }
9-
107
private var connection: RedisConnection!
118

129
override func setUp() {
1310
do {
14-
connection = try redis.makeConnection().wait()
11+
connection = try RedisConnection.connect().wait()
1512
} catch {
1613
XCTFail("Failed to create RedisConnection!")
1714
}
1815
}
1916

2017
override func tearDown() {
2118
_ = try? connection.send(command: "FLUSHALL").wait()
22-
connection.close()
19+
try? connection.close().wait()
2320
connection = nil
2421
}
2522

Tests/NIORedisTests/RedisDriverTests.swift

Lines changed: 0 additions & 47 deletions
This file was deleted.

Tests/NIORedisTests/RedisPipelineTests.swift

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,20 @@
22
import XCTest
33

44
final class RedisPipelineTests: XCTestCase {
5-
private var redis: RedisDriver!
65
private var connection: RedisConnection!
76

87
override func setUp() {
9-
let redis = RedisDriver(ownershipModel: .internal(threadCount: 1))
10-
11-
guard let connection = try? redis.makeConnection().wait() else {
12-
return XCTFail("Failed to create connection!")
8+
do {
9+
connection = try RedisConnection.connect().wait()
10+
} catch {
11+
XCTFail("Failed to create RedisConnection!")
1312
}
14-
15-
self.redis = redis
16-
self.connection = connection
1713
}
1814

1915
override func tearDown() {
2016
_ = try? connection.send(command: "FLUSHALL").wait()
21-
connection.close()
22-
try? redis.terminate()
17+
try? connection.close().wait()
18+
connection = nil
2319
}
2420

2521
func test_enqueue() throws {
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
@testable import NIORedis
3+
import Foundation
4+
5+
extension RedisConnection {
6+
static func connect(
7+
on elg: EventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
8+
) throws -> EventLoopFuture<RedisConnection> {
9+
return RedisConnection.connect(to: try .init(ipAddress: "127.0.0.1", port: 6379), on: elg)
10+
}
11+
}

Tests/NIORedisTests/XCTestManifests.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import XCTest
33
#if !os(macOS)
44
public func allTests() -> [XCTestCaseEntry] {
55
return [
6-
testCase(RedisDriverTests.allTests),
76
testCase(RESPDecoderTests.allTests),
87
testCase(RESPDecoderParsingTests.allTests),
98
testCase(RESPDecoderByteToMessageDecoderTests.allTests),

0 commit comments

Comments
 (0)