Fixed the logging event recording concurrency for the LogHandlerMock mock type in the test target.

This commit is contained in:
2025-09-27 02:21:33 +02:00
parent 3a9e3d176f
commit e68d4464ad
@@ -33,9 +33,7 @@ struct LogHandlerMock {
// MARK: Computed // MARK: Computed
/// A list of all the logged events that are being persisted in the recorder. /// A list of all the logged events that are being persisted in the recorder.
var entries: [LogEntry] { var entries: [LogEntry] { recorder.entries }
get async { await recorder.entries }
}
} }
@@ -63,13 +61,25 @@ struct LogEntry: Equatable {
// MARK: - LogRecorder // MARK: - LogRecorder
extension LogHandlerMock { extension LogHandlerMock {
/// An actor that persists all the events logged by the ``LogHandlerMock`` mock handler. /// A class that records all the events logged by the ``LogHandlerMock`` mock handler.
actor LogRecorder { ///
/// This class conforms to the `Sendable` protocol by using the `@unchecked` modifier because a `NSLock`type is used to handle the access to the logged events in a thread-safe way.
final class LogRecorder: @unchecked Sendable {
// MARK: Properties // MARK: Properties
/// A list of all the logged events persisted in a thread-safe way.
private(set) var _entries: [LogEntry] = []
/// A type that coordinates the access to the persisted logged events in a thread-safe way.
private let lock: NSLock = .init()
// MARK: Computed
/// A list of all the logged events. /// A list of all the logged events.
private(set) var entries: [LogEntry] = [] var entries: [LogEntry] {
lock.withLock { _entries }
}
// MARK: Functions // MARK: Functions
@@ -84,13 +94,15 @@ extension LogHandlerMock {
metadata: Logger.Metadata?, metadata: Logger.Metadata?,
message: Logger.Message, message: Logger.Message,
source: String source: String
) async { ) {
entries += [.init( lock.withLock {
level: level, _entries += [.init(
metadata: metadata, level: level,
message: message, metadata: metadata,
source: source message: message,
)] source: source
)]
}
} }
} }
@@ -130,12 +142,12 @@ extension LogHandlerMock: LogHandler {
function: String, function: String,
line: UInt line: UInt
) { ) {
Task { await recorder.record( recorder.record(
level: level, level: level,
metadata: metadata, metadata: metadata,
message: message, message: message,
source: source source: source
)} )
} }
} }