feat: Fixes some tests and docker builds

This commit is contained in:
2024-11-14 14:58:09 -05:00
parent e7a849b003
commit 163f603b69
9 changed files with 176 additions and 282 deletions

View File

@@ -1,10 +1,11 @@
import Dependencies
import Logging
import Models
@_spi(Internal) import MQTTConnectionManager
import MQTTNIO
import NIO
import PsychrometricClientLive
@testable import SensorsService
@_spi(Internal) import SensorsService
import TopicDependencies
import XCTest
@@ -14,214 +15,74 @@ final class SensorsClientTests: XCTestCase {
static let logger: Logger = {
var logger = Logger(label: "SensorsClientTests")
logger.logLevel = .debug
logger.logLevel = .trace
return logger
}()
override func invokeTest() {
let client = createClient(identifier: "\(Self.self)")
withDependencies {
$0.mqttConnectionManager = .live(client: client, logger: Self.logger)
$0.psychrometricClient = PsychrometricClient.liveValue
$0.topicListener = .live(client: client)
$0.topicPublisher = .live(client: client)
} operation: {
super.invokeTest()
}
}
func testWhatHappensIfClientDisconnectsWhileListening() async throws {
let client = createClient(identifier: "testWhatHappensIfClientDisconnectsWhileListening")
let listener = TopicListener.live(client: client)
try await client.connect()
func testListeningResumesAfterDisconnectThenReconnect() async throws {
@Dependency(\.mqttConnectionManager) var manager
struct TimeoutError: Error {}
let stream = try await listener.listen("/some/topic")
let sensor = TemperatureAndHumiditySensor(location: .return)
var results = [TopicPublisher.PublishRequest]()
// try await Task.sleep(for: .seconds(1))
// try await client.disconnect()
//
// try await client.connect()
// try await Task.sleep(for: .seconds(1))
try await client.publish(
to: "/some/topic",
payload: ByteBufferAllocator().buffer(string: "Foo"),
qos: .atLeastOnce,
retain: true
)
try await Task.sleep(for: .seconds(1))
try await withDependencies {
$0.topicPublisher = .capturing { results.append($0) }
} operation: {
let sensorsService = SensorsService(sensors: [sensor], logger: Self.logger)
let task = Task { try await sensorsService.run() }
defer { task.cancel() }
listener.shutdown()
try await client.shutdown()
try await manager.connect()
defer { manager.shutdown() }
try await manager.withClient { client in
try await client.disconnect()
try await client.connect()
try await Task.sleep(for: .milliseconds(100))
try await client.publish(
to: sensor.topics.temperature,
payload: ByteBufferAllocator().buffer(string: "25"),
qos: .atLeastOnce,
retain: false
)
try await client.publish(
to: sensor.topics.humidity,
payload: ByteBufferAllocator().buffer(string: "50"),
qos: .atLeastOnce,
retain: false
)
}
var timeoutCount = 0
while !(results.count == 2) {
guard timeoutCount < 20 else {
throw TimeoutError()
}
try await Task.sleep(for: .milliseconds(100))
timeoutCount += 1
}
XCTAssertEqual(results.count, 2)
XCTAssert(results.contains(where: { $0.topicName == sensor.topics.dewPoint }))
XCTAssert(results.contains(where: { $0.topicName == sensor.topics.enthalpy }))
try await sensorsService.shutdown()
}
}
// func testConnectAndShutdown() async throws {
// let client = createClient(identifier: "testConnectAndShutdown")
// await client.connect()
// await client.shutdown()
// }
// func testSensorService() async throws {
// let mqtt = createClient(identifier: "testSensorService")
// // let mqtt = await client.client
// let sensor = TemperatureAndHumiditySensor(location: .mixedAir)
// let publishInfo = PublishInfoContainer(topicFilters: [
// sensor.topics.dewPoint,
// sensor.topics.enthalpy
// ])
// let service = SensorsService(client: mqtt, sensors: [sensor])
//
// // fix to connect the mqtt client.
// try await mqtt.connect()
// let task = Task { try await service.run() }
//
// _ = try await mqtt.subscribe(to: [
// MQTTSubscribeInfo(topicFilter: sensor.topics.dewPoint, qos: .exactlyOnce),
// MQTTSubscribeInfo(topicFilter: sensor.topics.enthalpy, qos: .exactlyOnce)
// ])
//
// let listener = mqtt.createPublishListener()
// Task {
// for await result in listener {
// switch result {
// case let .failure(error):
// XCTFail("\(error)")
// case let .success(value):
// await publishInfo.addPublishInfo(value)
// }
// }
// }
//
// try await mqtt.publish(
// to: sensor.topics.temperature,
// payload: ByteBufferAllocator().buffer(string: "75.123"),
// qos: MQTTQoS.exactlyOnce,
// retain: true
// )
//
// try await Task.sleep(for: .seconds(1))
//
// // XCTAssert(client.sensors.first!.needsProcessed)
// // let firstSensor = await client.sensors.first!
// // XCTAssertEqual(firstSensor.temperature, .init(75.123, units: .celsius))
//
// try await mqtt.publish(
// to: sensor.topics.humidity,
// payload: ByteBufferAllocator().buffer(string: "50"),
// qos: MQTTQoS.exactlyOnce,
// retain: true
// )
//
// try await Task.sleep(for: .seconds(1))
//
// // not working for some reason
// // XCTAssertEqual(publishInfo.info.count, 2)
//
// XCTAssert(publishInfo.info.count > 1)
//
// // fix to shutdown the mqtt client.
// task.cancel()
// try await mqtt.shutdown()
// }
// func testCapturingSensorClient() async throws {
// class CapturedValues {
// var values = [(value: Double, topic: String)]()
// var didShutdown = false
//
// init() {}
// }
//
// let capturedValues = CapturedValues()
//
// try await withDependencies {
// $0.sensorsClient = .testing(
// yielding: [
// (value: 76, to: "not-listening"),
// (value: 75, to: "test")
// ]
// ) { value, topic in
// capturedValues.values.append((value, topic))
// } captureShutdownEvent: {
// capturedValues.didShutdown = $0
// }
// } operation: {
// @Dependency(\.sensorsClient) var client
// let stream = try await client.listen(to: ["test"])
//
// for await result in stream {
// var buffer = result.buffer
// guard let double = Double(buffer: &buffer) else {
// XCTFail("Failed to decode double")
// return
// }
//
// XCTAssertEqual(double, 75)
// XCTAssertEqual(result.topic, "test")
// try await client.publish(26, to: "publish")
// try await Task.sleep(for: .milliseconds(100))
// client.shutdown()
// }
//
// XCTAssertEqual(capturedValues.values.count, 1)
// XCTAssertEqual(capturedValues.values.first?.value, 26)
// XCTAssertEqual(capturedValues.values.first?.topic, "publish")
// XCTAssertTrue(capturedValues.didShutdown)
// }
// }
//
// func testSensorCapturesPublishedState() async throws {
// let client = createClient(identifier: "testSensorCapturesPublishedState")
// let mqtt = client.client
// let sensor = TemperatureAndHumiditySensor(location: .mixedAir)
// let publishInfo = PublishInfoContainer(topicFilters: [
// sensor.topics.dewPoint,
// sensor.topics.enthalpy
// ])
//
// try await client.addSensor(sensor)
// await client.connect()
// try await client.start()
//
// _ = try await mqtt.subscribe(to: [
// MQTTSubscribeInfo(topicFilter: sensor.topics.dewPoint, qos: MQTTQoS.exactlyOnce),
// MQTTSubscribeInfo(topicFilter: sensor.topics.enthalpy, qos: MQTTQoS.exactlyOnce)
// ])
//
// let listener = mqtt.createPublishListener()
// Task {
// for await result in listener {
// switch result {
// case let .failure(error):
// XCTFail("\(error)")
// case let .success(value):
// await publishInfo.addPublishInfo(value)
// }
// }
// }
//
// try await mqtt.publish(
// to: sensor.topics.temperature,
// payload: ByteBufferAllocator().buffer(string: "75.123"),
// qos: MQTTQoS.exactlyOnce,
// retain: true
// )
//
// try await Task.sleep(for: .seconds(1))
//
// // XCTAssert(client.sensors.first!.needsProcessed)
// let firstSensor = client.sensors.first!
// XCTAssertEqual(firstSensor.temperature, DryBulb.celsius(75.123))
//
// try await mqtt.publish(
// to: sensor.topics.humidity,
// payload: ByteBufferAllocator().buffer(string: "50"),
// qos: MQTTQoS.exactlyOnce,
// retain: true
// )
//
// try await Task.sleep(for: .seconds(1))
//
// XCTAssertEqual(publishInfo.info.count, 2)
//
// await client.shutdown()
// }
func createClient(identifier: String) -> MQTTClient {
let envVars = EnvVars(
appEnv: .testing,
@@ -252,16 +113,6 @@ final class SensorsClientTests: XCTestCase {
// MARK: Helpers for tests.
extension AsyncStream {
func first() async -> Element {
var first: Element
for await value in self {
first = value
}
return first
}
}
class PublishInfoContainer {
private(set) var info: [MQTTPublishInfo]
private var topicFilters: [String]?
@@ -282,6 +133,14 @@ class PublishInfoContainer {
}
}
extension TopicPublisher {
static func capturing(
_ callback: @escaping (PublishRequest) -> Void
) -> Self {
.init { callback($0) }
}
}
// extension SensorsClient {
//
// static func testing(