Skip to content

Commit

Permalink
Merge pull request #25 from Mordil/remove-driver
Browse files Browse the repository at this point in the history
Remove `RedisDriver`
  • Loading branch information
Mordil authored Mar 28, 2019
2 parents ba6bf6d + db4a156 commit 152eb30
Show file tree
Hide file tree
Showing 13 changed files with 72 additions and 175 deletions.
28 changes: 14 additions & 14 deletions Sources/NIORedis/Extensions/NIO/ClientBootstrap.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@ import Foundation
import NIO

extension ClientBootstrap {
/// Makes a new `ClientBootstrap` instance with a standard Redis `Channel` pipeline for sending and receiving
/// messages in Redis Serialization Protocol (RESP) format.
/// Makes a new `ClientBootstrap` instance with a default Redis `Channel` pipeline
/// for sending and receiving messages in Redis Serialization Protocol (RESP) format.
///
/// See `RESPEncoder`, `RESPDecoder`, and `RedisCommadHandler`.
/// See `RESPEncoder`, `RESPDecoder`, and `RedisCommadHandler`
/// - Parameter using: The `EventLoopGroup` to build the `ClientBootstrap` on.
/// - Returns: A `ClientBootstrap` with the standard configuration of a `Channel` pipeline for RESP messages.
public static func makeForRedis(using group: EventLoopGroup) -> ClientBootstrap {
/// - Returns: A `ClientBootstrap` with the default configuration of a `Channel` pipeline for RESP messages.
public static func makeRedisDefault(using group: EventLoopGroup) -> ClientBootstrap {
return ClientBootstrap(group: group)
.channelOption(ChannelOptions.socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1)
.channelInitializer { channel in
let handlers: [ChannelHandler] = [
MessageToByteHandler(RESPEncoder()),
ByteToMessageHandler(RESPDecoder()),
RedisCommandHandler()
]
return .andAllSucceed(handlers.map { channel.pipeline.addHandler($0) }, on: group.next())
}
.channelOption(
ChannelOptions.socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR),
value: 1
)
.channelInitializer { $0.pipeline.addHandlers([
MessageToByteHandler(RESPEncoder()),
ByteToMessageHandler(RESPDecoder()),
RedisCommandHandler()
])}
}
}
28 changes: 28 additions & 0 deletions Sources/NIORedis/RedisClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,31 @@ public final class RedisConnection: RedisClient {
return promise.futureResult
}
}

extension RedisConnection {
/// Makes a client connection to a Redis instance.
/// - Parameters:
/// - socket: The `SocketAddress` information of the Redis instance to connect to.
/// - password: The optional password to authorize the client with.
/// - eventLoopGroup: The `EventLoopGroup` to build the connection on.
/// - logger: The `Logger` instance to log with.
/// - Returns: A `RedisClient` instance representing this new connection.
public static func connect(
to socket: SocketAddress,
with password: String? = nil,
on eventLoopGroup: EventLoopGroup,
logger: Logger = Logger(label: "NIORedis.RedisClient")
) -> EventLoopFuture<RedisConnection> {
let bootstrap = ClientBootstrap.makeRedisDefault(using: eventLoopGroup)

return bootstrap.connect(to: socket)
.map { return RedisConnection(channel: $0, logger: logger) }
.flatMap { client in
guard let pw = password else {
return eventLoopGroup.next().makeSucceededFuture(client)
}
return client.authorize(with: pw)
.map { _ in return client }
}
}
}
73 changes: 0 additions & 73 deletions Sources/NIORedis/RedisDriver.swift

This file was deleted.

7 changes: 2 additions & 5 deletions Tests/NIORedisTests/Commands/BasicCommandsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,19 @@
import XCTest

final class BasicCommandsTests: XCTestCase {
private let redis = RedisDriver(ownershipModel: .internal(threadCount: 1))
deinit { try? redis.terminate() }

private var connection: RedisConnection!

override func setUp() {
do {
connection = try redis.makeConnection().wait()
connection = try RedisConnection.connect().wait()
} catch {
XCTFail("Failed to create RedisConnection!")
}
}

override func tearDown() {
_ = try? connection.send(command: "FLUSHALL").wait()
connection.close()
try? connection.close().wait()
connection = nil
}

Expand Down
7 changes: 2 additions & 5 deletions Tests/NIORedisTests/Commands/HashCommandsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,19 @@
import XCTest

final class HashCommandsTests: XCTestCase {
private let redis = RedisDriver(ownershipModel: .internal(threadCount: 1))
deinit { try? redis.terminate() }

private var connection: RedisConnection!

override func setUp() {
do {
connection = try redis.makeConnection().wait()
connection = try RedisConnection.connect().wait()
} catch {
XCTFail("Failed to create RedisConnection!")
}
}

override func tearDown() {
_ = try? connection.send(command: "FLUSHALL").wait()
connection.close()
try? connection.close().wait()
connection = nil
}

Expand Down
7 changes: 2 additions & 5 deletions Tests/NIORedisTests/Commands/ListCommandsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,19 @@
import XCTest

final class ListCommandsTests: XCTestCase {
private let redis = RedisDriver(ownershipModel: .internal(threadCount: 1))
deinit { try? redis.terminate() }

private var connection: RedisConnection!

override func setUp() {
do {
connection = try redis.makeConnection().wait()
connection = try RedisConnection.connect().wait()
} catch {
XCTFail("Failed to create RedisConnection!")
}
}

override func tearDown() {
_ = try? connection.send(command: "FLUSHALL").wait()
connection.close()
try? connection.close().wait()
connection = nil
}

Expand Down
7 changes: 2 additions & 5 deletions Tests/NIORedisTests/Commands/SetCommandsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,19 @@
import XCTest

final class SetCommandsTests: XCTestCase {
private let redis = RedisDriver(ownershipModel: .internal(threadCount: 1))
deinit { try? redis.terminate() }

private var connection: RedisConnection!

override func setUp() {
do {
connection = try redis.makeConnection().wait()
connection = try RedisConnection.connect().wait()
} catch {
XCTFail("Failed to create RedisConnection!")
}
}

override func tearDown() {
_ = try? connection.send(command: "FLUSHALL").wait()
connection.close()
try? connection.close().wait()
connection = nil
}

Expand Down
8 changes: 3 additions & 5 deletions Tests/NIORedisTests/Commands/SortedSetCommandsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@ import XCTest
final class SortedSetCommandsTests: XCTestCase {
private static let testKey = "SortedSetCommandsTests"

private let redis = RedisDriver(ownershipModel: .internal(threadCount: 1))
deinit { try? redis.terminate() }

private var connection: RedisConnection!

private var key: String { return SortedSetCommandsTests.testKey }

override func setUp() {
do {
connection = try redis.makeConnection().wait()
connection = try RedisConnection.connect().wait()

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

override func tearDown() {
_ = try? connection.send(command: "FLUSHALL").wait()
connection.close()
try? connection.close().wait()
connection = nil
}

Expand Down
7 changes: 2 additions & 5 deletions Tests/NIORedisTests/Commands/StringCommandsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,19 @@ import XCTest
final class StringCommandsTests: XCTestCase {
private static let testKey = "SortedSetCommandsTests"

private let redis = RedisDriver(ownershipModel: .internal(threadCount: 1))
deinit { try? redis.terminate() }

private var connection: RedisConnection!

override func setUp() {
do {
connection = try redis.makeConnection().wait()
connection = try RedisConnection.connect().wait()
} catch {
XCTFail("Failed to create RedisConnection!")
}
}

override func tearDown() {
_ = try? connection.send(command: "FLUSHALL").wait()
connection.close()
try? connection.close().wait()
connection = nil
}

Expand Down
47 changes: 0 additions & 47 deletions Tests/NIORedisTests/RedisDriverTests.swift

This file was deleted.

16 changes: 6 additions & 10 deletions Tests/NIORedisTests/RedisPipelineTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,20 @@
import XCTest

final class RedisPipelineTests: XCTestCase {
private var redis: RedisDriver!
private var connection: RedisConnection!

override func setUp() {
let redis = RedisDriver(ownershipModel: .internal(threadCount: 1))

guard let connection = try? redis.makeConnection().wait() else {
return XCTFail("Failed to create connection!")
do {
connection = try RedisConnection.connect().wait()
} catch {
XCTFail("Failed to create RedisConnection!")
}

self.redis = redis
self.connection = connection
}

override func tearDown() {
_ = try? connection.send(command: "FLUSHALL").wait()
connection.close()
try? redis.terminate()
try? connection.close().wait()
connection = nil
}

func test_enqueue() throws {
Expand Down
11 changes: 11 additions & 0 deletions Tests/NIORedisTests/Utilities/RedisConnection.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

@testable import NIORedis
import Foundation

extension RedisConnection {
static func connect(
on elg: EventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
) throws -> EventLoopFuture<RedisConnection> {
return RedisConnection.connect(to: try .init(ipAddress: "127.0.0.1", port: 6379), on: elg)
}
}
1 change: 0 additions & 1 deletion Tests/NIORedisTests/XCTestManifests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import XCTest
#if !os(macOS)
public func allTests() -> [XCTestCaseEntry] {
return [
testCase(RedisDriverTests.allTests),
testCase(RESPDecoderTests.allTests),
testCase(RESPDecoderParsingTests.allTests),
testCase(RESPDecoderByteToMessageDecoderTests.allTests),
Expand Down

0 comments on commit 152eb30

Please sign in to comment.