mirror of
https://github.com/swift-server/RediStack.git
synced 2026-06-02 07:37:33 +00:00
The newly-released Service Discovery framework gives us the interesting opportunity to make RediStack aware of complex service discovery tools. This patch supplies a simple adaptor to integrat Service Discovery with RediStack's pooled client, allowing users to work with arbitrary service discovery systems.
126 lines
4.3 KiB
Swift
126 lines
4.3 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the RediStack open source project
|
|
//
|
|
// Copyright (c) 2020 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 Logging
|
|
import ServiceDiscovery
|
|
import NIO
|
|
import RediStack
|
|
import RediStackTestUtils
|
|
import XCTest
|
|
|
|
final class RedisLoggingTests: RediStackIntegrationTestCase {
|
|
func test_connectionUsesCustomLogger() throws {
|
|
let handler = TestLogHandler()
|
|
let logger = Logger(label: #function, factory: { _ in return handler })
|
|
_ = try self.connection
|
|
.logging(to: logger)
|
|
.ping()
|
|
.wait()
|
|
XCTAssertFalse(handler.messages.isEmpty)
|
|
}
|
|
|
|
func test_connectionLoggerMetadata() throws {
|
|
let handler = TestLogHandler()
|
|
let logger = Logger(label: #function, factory: { _ in return handler })
|
|
|
|
_ = try self.connection
|
|
.logging(to: logger)
|
|
.ping()
|
|
.wait()
|
|
XCTAssertEqual(
|
|
handler.metadata[RedisLogging.MetadataKeys.connectionID],
|
|
.string(self.connection.id.description)
|
|
)
|
|
}
|
|
|
|
func test_poolLoggerMetadata() throws {
|
|
let handler = TestLogHandler()
|
|
let logger = Logger(label: #function, factory: { _ in return handler })
|
|
|
|
let pool = RedisConnectionPool(
|
|
configuration: .init(
|
|
initialServerConnectionAddresses: [try .makeAddressResolvingHost(self.redisHostname, port: self.redisPort)],
|
|
maximumConnectionCount: .maximumActiveConnections(1),
|
|
connectionFactoryConfiguration: .init(connectionPassword: self.redisPassword)
|
|
),
|
|
boundEventLoop: self.connection.eventLoop
|
|
)
|
|
defer { pool.close() }
|
|
pool.activate()
|
|
|
|
_ = try pool
|
|
.logging(to: logger)
|
|
.ping()
|
|
.wait()
|
|
XCTAssertTrue(handler.metadata.keys.contains(RedisLogging.MetadataKeys.connectionID))
|
|
XCTAssertEqual(
|
|
handler.metadata[RedisLogging.MetadataKeys.connectionPoolID],
|
|
.string(pool.id.uuidString)
|
|
)
|
|
}
|
|
|
|
func test_serviceDiscoveryMetadata() throws {
|
|
let handler = TestLogHandler()
|
|
let logger = Logger(label: #function, factory: { _ in return handler })
|
|
let hosts = InMemoryServiceDiscovery<String, SocketAddress>(configuration: .init())
|
|
let config = RedisConnectionPool.Configuration(
|
|
initialServerConnectionAddresses: [],
|
|
maximumConnectionCount: .maximumActiveConnections(1),
|
|
connectionFactoryConfiguration: .init(connectionPassword: self.redisPassword)
|
|
)
|
|
let client = RedisConnectionPool.activatedServiceDiscoveryPool(
|
|
service: "default.local",
|
|
discovery: hosts,
|
|
configuration: config,
|
|
boundEventLoop: self.connection.eventLoop)
|
|
defer {
|
|
client.close()
|
|
}
|
|
|
|
let address = try SocketAddress.makeAddressResolvingHost(self.redisHostname, port: self.redisPort)
|
|
hosts.register("default.local", instances: [address])
|
|
|
|
_ = try client
|
|
.logging(to: logger)
|
|
.ping()
|
|
.wait()
|
|
XCTAssertTrue(handler.metadata.keys.contains(RedisLogging.MetadataKeys.connectionID))
|
|
XCTAssertEqual(
|
|
handler.metadata[RedisLogging.MetadataKeys.connectionPoolID],
|
|
.string(client.id.uuidString)
|
|
)
|
|
}
|
|
}
|
|
|
|
final class TestLogHandler: LogHandler {
|
|
var messages: [Logger.Message]
|
|
var metadata: Logger.Metadata
|
|
var logLevel: Logger.Level
|
|
|
|
init() {
|
|
self.messages = []
|
|
self.metadata = [:]
|
|
self.logLevel = .trace
|
|
}
|
|
|
|
func log(level: Logger.Level, message: Logger.Message, metadata: Logger.Metadata?, file: String, function: String, line: UInt) {
|
|
self.messages.append(message)
|
|
}
|
|
|
|
subscript(metadataKey key: String) -> Logger.Metadata.Value? {
|
|
get { self.metadata[key] }
|
|
set(newValue) { self.metadata[key] = newValue }
|
|
}
|
|
}
|