From 9f866a0f4a4ac3892b2bcc3c42d5b31133d1f92a Mon Sep 17 00:00:00 2001 From: Jon Petersson Date: Mon, 7 Oct 2024 12:50:15 +0200 Subject: [PATCH] Fix failing log rotation test --- ios/MullvadLogging/LogFileOutputStream.swift | 7 +++++++ ios/MullvadVPN.xcodeproj/project.pbxproj | 2 +- ios/MullvadVPNTests/MullvadLogging/LogRotationTests.swift | 4 +--- ios/MullvadVPNTests/MullvadLogging/LoggingTests.swift | 6 +++--- 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/ios/MullvadLogging/LogFileOutputStream.swift b/ios/MullvadLogging/LogFileOutputStream.swift index 17d335849e56..7f4eec3a0bb5 100644 --- a/ios/MullvadLogging/LogFileOutputStream.swift +++ b/ios/MullvadLogging/LogFileOutputStream.swift @@ -75,6 +75,13 @@ class LogFileOutputStream: TextOutputStream { } } + /// Waits for write operations to finish by issuing a synchronous closure. + /// - Note: This function is mainly used in unit tests to facilitate acting + /// on disk writes. It should typically not be used in production code. + func synchronize() { + queue.sync {} + } + private func writeOnQueue(_ string: String) { guard let data = string.data(using: encoding) else { return } diff --git a/ios/MullvadVPN.xcodeproj/project.pbxproj b/ios/MullvadVPN.xcodeproj/project.pbxproj index 8c5a8f19acfd..0cb01cf7d13c 100644 --- a/ios/MullvadVPN.xcodeproj/project.pbxproj +++ b/ios/MullvadVPN.xcodeproj/project.pbxproj @@ -2498,8 +2498,8 @@ 440E9EFA2BDA976800B1FD11 /* MullvadLogging */ = { isa = PBXGroup; children = ( - 7AA513852BC91C6B00D081A4 /* LogRotationTests.swift */, 44B02E3A2BC5732D008EDF34 /* LoggingTests.swift */, + 7AA513852BC91C6B00D081A4 /* LogRotationTests.swift */, ); path = MullvadLogging; sourceTree = ""; diff --git a/ios/MullvadVPNTests/MullvadLogging/LogRotationTests.swift b/ios/MullvadVPNTests/MullvadLogging/LogRotationTests.swift index 9ea45fe8e94b..a6194bfc9899 100644 --- a/ios/MullvadVPNTests/MullvadLogging/LogRotationTests.swift +++ b/ios/MullvadVPNTests/MullvadLogging/LogRotationTests.swift @@ -41,10 +41,8 @@ final class LogRotationTests: XCTestCase { let stream = LogFileOutputStream(fileURL: logPath, header: "", fileSizeLimit: UInt64(totalLogSizeLimit)) for _ in 0 ..< writeOperationCount { stream.write(stringOfSize(logChunkSize)) - - // Without sync between every write the test fails on Github. - sync() } + stream.synchronize() let actualLogCount = try fileManager.contentsOfDirectory(atPath: directoryPath.relativePath).count XCTAssertEqual(expectedLogCount, actualLogCount) diff --git a/ios/MullvadVPNTests/MullvadLogging/LoggingTests.swift b/ios/MullvadVPNTests/MullvadLogging/LoggingTests.swift index d22222ea3b97..e4fc94a91c2b 100644 --- a/ios/MullvadVPNTests/MullvadLogging/LoggingTests.swift +++ b/ios/MullvadVPNTests/MullvadLogging/LoggingTests.swift @@ -10,7 +10,7 @@ import Foundation @testable import MullvadLogging import XCTest -class MullvadLoggingTests: XCTestCase { +class LoggingTests: XCTestCase { let fileManager = FileManager.default var directoryPath: URL! @@ -33,7 +33,7 @@ class MullvadLoggingTests: XCTestCase { let fileURL = directoryPath.appendingPathComponent(UUID().uuidString) let stream = LogFileOutputStream(fileURL: fileURL, header: headerText) stream.write(logMessage) - sync() + stream.synchronize() let contents = try XCTUnwrap(String(contentsOf: fileURL)) XCTAssertEqual(contents, "\(headerText)\n\(logMessage)") @@ -71,7 +71,7 @@ class MullvadLoggingTests: XCTestCase { logPaths.forEach { url in let stream = LogFileOutputStream(fileURL: url, header: "") stream.write("test") - sync() + stream.synchronize() } var urls = ApplicationConfiguration.logFileURLs(for: .mainApp, in: directoryPath)