feat: Adds MQTTConnectionServiceTests
This commit is contained in:
@@ -19,6 +19,7 @@ let package = Package(
|
|||||||
.library(name: "Models", targets: ["Models"]),
|
.library(name: "Models", targets: ["Models"]),
|
||||||
.library(name: "Client", targets: ["Client"]),
|
.library(name: "Client", targets: ["Client"]),
|
||||||
.library(name: "ClientLive", targets: ["ClientLive"]),
|
.library(name: "ClientLive", targets: ["ClientLive"]),
|
||||||
|
.library(name: "MQTTConnectionService", targets: ["MQTTConnectionService"]),
|
||||||
.library(name: "SensorsService", targets: ["SensorsService"])
|
.library(name: "SensorsService", targets: ["SensorsService"])
|
||||||
],
|
],
|
||||||
dependencies: [
|
dependencies: [
|
||||||
@@ -112,6 +113,13 @@ let package = Package(
|
|||||||
],
|
],
|
||||||
swiftSettings: swiftSettings
|
swiftSettings: swiftSettings
|
||||||
),
|
),
|
||||||
|
.testTarget(
|
||||||
|
name: "MQTTConnectionServiceTests",
|
||||||
|
dependencies: [
|
||||||
|
"MQTTConnectionService",
|
||||||
|
.product(name: "ServiceLifecycleTestKit", package: "swift-service-lifecycle")
|
||||||
|
]
|
||||||
|
),
|
||||||
.target(
|
.target(
|
||||||
name: "SensorsService",
|
name: "SensorsService",
|
||||||
dependencies: [
|
dependencies: [
|
||||||
|
|||||||
@@ -0,0 +1,60 @@
|
|||||||
|
import EnvVars
|
||||||
|
import Logging
|
||||||
|
import MQTTConnectionService
|
||||||
|
import MQTTNIO
|
||||||
|
import NIO
|
||||||
|
import ServiceLifecycleTestKit
|
||||||
|
import XCTest
|
||||||
|
|
||||||
|
final class MQTTConnectionServiceTests: XCTestCase {
|
||||||
|
|
||||||
|
static let hostname = ProcessInfo.processInfo.environment["MOSQUITTO_SERVER"] ?? "localhost"
|
||||||
|
|
||||||
|
static let logger: Logger = {
|
||||||
|
var logger = Logger(label: "AsyncClientTests")
|
||||||
|
logger.logLevel = .debug
|
||||||
|
return logger
|
||||||
|
}()
|
||||||
|
|
||||||
|
func testGracefulShutdownWorks() async throws {
|
||||||
|
let client = createClient(identifier: "testGracefulShutdown")
|
||||||
|
|
||||||
|
try await testGracefulShutdown { trigger in
|
||||||
|
let service = MQTTConnectionService(client: client)
|
||||||
|
try await service.run()
|
||||||
|
trigger.triggerGracefulShutdown()
|
||||||
|
}
|
||||||
|
|
||||||
|
try await Task.sleep(for: .seconds(1))
|
||||||
|
|
||||||
|
XCTAssertFalse(client.isActive())
|
||||||
|
}
|
||||||
|
|
||||||
|
func createClient(identifier: String) -> MQTTClient {
|
||||||
|
let envVars = EnvVars(
|
||||||
|
appEnv: .testing,
|
||||||
|
host: Self.hostname,
|
||||||
|
port: "1883",
|
||||||
|
identifier: identifier,
|
||||||
|
userName: nil,
|
||||||
|
password: nil
|
||||||
|
)
|
||||||
|
let config = MQTTClient.Configuration(
|
||||||
|
version: .v3_1_1,
|
||||||
|
userName: envVars.userName,
|
||||||
|
password: envVars.password,
|
||||||
|
useSSL: false,
|
||||||
|
useWebSockets: false,
|
||||||
|
tlsConfiguration: nil,
|
||||||
|
webSocketURLPath: nil
|
||||||
|
)
|
||||||
|
return .init(
|
||||||
|
host: Self.hostname,
|
||||||
|
identifier: identifier,
|
||||||
|
eventLoopGroupProvider: .shared(MultiThreadedEventLoopGroup(numberOfThreads: 1)),
|
||||||
|
logger: Self.logger,
|
||||||
|
configuration: config
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -39,7 +39,7 @@ final class SensorsClientTests: XCTestCase {
|
|||||||
func testSensorService() async throws {
|
func testSensorService() async throws {
|
||||||
let client = createClient(identifier: "testSensorService")
|
let client = createClient(identifier: "testSensorService")
|
||||||
let mqtt = await client.client
|
let mqtt = await client.client
|
||||||
let sensor = TemperatureAndHumiditySensor(location: .mixedAir, units: .metric)
|
let sensor = TemperatureAndHumiditySensor(location: .mixedAir)
|
||||||
let publishInfo = PublishInfoContainer(topicFilters: [
|
let publishInfo = PublishInfoContainer(topicFilters: [
|
||||||
sensor.topics.dewPoint,
|
sensor.topics.dewPoint,
|
||||||
sensor.topics.enthalpy
|
sensor.topics.enthalpy
|
||||||
@@ -99,7 +99,7 @@ final class SensorsClientTests: XCTestCase {
|
|||||||
func testSensorCapturesPublishedState() async throws {
|
func testSensorCapturesPublishedState() async throws {
|
||||||
let client = createClient(identifier: "testSensorCapturesPublishedState")
|
let client = createClient(identifier: "testSensorCapturesPublishedState")
|
||||||
let mqtt = await client.client
|
let mqtt = await client.client
|
||||||
let sensor = TemperatureAndHumiditySensor(location: .mixedAir, units: .metric)
|
let sensor = TemperatureAndHumiditySensor(location: .mixedAir)
|
||||||
let publishInfo = PublishInfoContainer(topicFilters: [
|
let publishInfo = PublishInfoContainer(topicFilters: [
|
||||||
sensor.topics.dewPoint,
|
sensor.topics.dewPoint,
|
||||||
sensor.topics.enthalpy
|
sensor.topics.enthalpy
|
||||||
|
|||||||
Reference in New Issue
Block a user