feat: Adds MQTTConnectionServiceTests
This commit is contained in:
@@ -9,7 +9,7 @@ public actor MQTTConnectionService: Service {
|
|||||||
private let cleanSession: Bool
|
private let cleanSession: Bool
|
||||||
public let client: MQTTClient
|
public let client: MQTTClient
|
||||||
private var shuttingDown = false
|
private var shuttingDown = false
|
||||||
var logger: Logger { client.logger }
|
nonisolated var logger: Logger { client.logger }
|
||||||
|
|
||||||
public init(
|
public init(
|
||||||
cleanSession: Bool = true,
|
cleanSession: Bool = true,
|
||||||
@@ -25,22 +25,28 @@ public actor MQTTConnectionService: Service {
|
|||||||
/// It will attempt to gracefully shutdown the connection upon receiving
|
/// It will attempt to gracefully shutdown the connection upon receiving
|
||||||
/// `sigterm` signals.
|
/// `sigterm` signals.
|
||||||
public func run() async throws {
|
public func run() async throws {
|
||||||
|
await withDiscardingTaskGroup { group in
|
||||||
await withGracefulShutdownHandler {
|
await withGracefulShutdownHandler {
|
||||||
await self.connect()
|
group.addTask { await self.connect() }
|
||||||
} onGracefulShutdown: {
|
} onGracefulShutdown: {
|
||||||
|
// try? self.client.syncShutdownGracefully()
|
||||||
Task { await self.shutdown() }
|
Task { await self.shutdown() }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private func shutdown() async {
|
func shutdown() async {
|
||||||
shuttingDown = true
|
shuttingDown = true
|
||||||
try? await client.disconnect()
|
try? await client.disconnect()
|
||||||
try? await client.shutdown()
|
try? await client.shutdown()
|
||||||
}
|
}
|
||||||
|
|
||||||
private func connect() async {
|
func connect() async {
|
||||||
do {
|
do {
|
||||||
try await client.connect(cleanSession: cleanSession)
|
try await withThrowingDiscardingTaskGroup { group in
|
||||||
|
group.addTask {
|
||||||
|
try await self.client.connect(cleanSession: self.cleanSession)
|
||||||
|
}
|
||||||
client.addCloseListener(named: "SensorsClient") { [self] _ in
|
client.addCloseListener(named: "SensorsClient") { [self] _ in
|
||||||
Task {
|
Task {
|
||||||
self.logger.debug("Connection closed.")
|
self.logger.debug("Connection closed.")
|
||||||
@@ -48,9 +54,23 @@ public actor MQTTConnectionService: Service {
|
|||||||
await self.connect()
|
await self.connect()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
logger.debug("Connection successful.")
|
self.logger.debug("Connection successful.")
|
||||||
} catch {
|
|
||||||
logger.trace("Connection Failed.\n\(error)")
|
|
||||||
}
|
}
|
||||||
|
} catch {
|
||||||
|
logger.trace("Failed to connect.")
|
||||||
|
}
|
||||||
|
// do {
|
||||||
|
// try await client.connect(cleanSession: cleanSession)
|
||||||
|
// client.addCloseListener(named: "SensorsClient") { [self] _ in
|
||||||
|
// Task {
|
||||||
|
// self.logger.debug("Connection closed.")
|
||||||
|
// self.logger.debug("Reconnecting...")
|
||||||
|
// await self.connect()
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// logger.debug("Connection successful.")
|
||||||
|
// } catch {
|
||||||
|
// logger.trace("Connection Failed.\(error)")
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,6 +19,11 @@ public actor SensorsService: Service {
|
|||||||
self.sensors = sensors
|
self.sensors = sensors
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The entry-point of the service.
|
||||||
|
///
|
||||||
|
/// This method is called to start the service and begin
|
||||||
|
/// listening for sensor value changes then publishing the dew-point
|
||||||
|
/// and enthalpy values of the sensors.
|
||||||
public func run() async throws {
|
public func run() async throws {
|
||||||
guard client.isActive() else {
|
guard client.isActive() else {
|
||||||
throw MQTTClientNotConnected()
|
throw MQTTClientNotConnected()
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
import EnvVars
|
import EnvVars
|
||||||
import Logging
|
import Logging
|
||||||
import MQTTConnectionService
|
@testable import MQTTConnectionService
|
||||||
import MQTTNIO
|
import MQTTNIO
|
||||||
import NIO
|
import NIO
|
||||||
|
import ServiceLifecycle
|
||||||
import ServiceLifecycleTestKit
|
import ServiceLifecycleTestKit
|
||||||
import XCTest
|
import XCTest
|
||||||
|
|
||||||
@@ -12,22 +13,19 @@ final class MQTTConnectionServiceTests: XCTestCase {
|
|||||||
|
|
||||||
static let logger: Logger = {
|
static let logger: Logger = {
|
||||||
var logger = Logger(label: "AsyncClientTests")
|
var logger = Logger(label: "AsyncClientTests")
|
||||||
logger.logLevel = .debug
|
logger.logLevel = .trace
|
||||||
return logger
|
return logger
|
||||||
}()
|
}()
|
||||||
|
|
||||||
func testGracefulShutdownWorks() async throws {
|
func testGracefulShutdownWorks() async throws {
|
||||||
let client = createClient(identifier: "testGracefulShutdown")
|
|
||||||
|
|
||||||
try await testGracefulShutdown { trigger in
|
try await testGracefulShutdown { trigger in
|
||||||
|
let client = createClient(identifier: "testGracefulShutdown")
|
||||||
let service = MQTTConnectionService(client: client)
|
let service = MQTTConnectionService(client: client)
|
||||||
try await service.run()
|
try await service.run()
|
||||||
|
try await Task.sleep(for: .seconds(1))
|
||||||
|
XCTAssert(client.isActive())
|
||||||
trigger.triggerGracefulShutdown()
|
trigger.triggerGracefulShutdown()
|
||||||
}
|
}
|
||||||
|
|
||||||
try await Task.sleep(for: .seconds(1))
|
|
||||||
|
|
||||||
XCTAssertFalse(client.isActive())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func createClient(identifier: String) -> MQTTClient {
|
func createClient(identifier: String) -> MQTTClient {
|
||||||
|
|||||||
Reference in New Issue
Block a user