Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix failing log rotation test #6936

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions ios/MullvadLogging/LogFileOutputStream.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down
2 changes: 1 addition & 1 deletion ios/MullvadVPN.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -2498,8 +2498,8 @@
440E9EFA2BDA976800B1FD11 /* MullvadLogging */ = {
isa = PBXGroup;
children = (
7AA513852BC91C6B00D081A4 /* LogRotationTests.swift */,
44B02E3A2BC5732D008EDF34 /* LoggingTests.swift */,
7AA513852BC91C6B00D081A4 /* LogRotationTests.swift */,
);
path = MullvadLogging;
sourceTree = "<group>";
Expand Down
4 changes: 1 addition & 3 deletions ios/MullvadVPNTests/MullvadLogging/LogRotationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions ios/MullvadVPNTests/MullvadLogging/LoggingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Foundation
@testable import MullvadLogging
import XCTest

class MullvadLoggingTests: XCTestCase {
class LoggingTests: XCTestCase {
let fileManager = FileManager.default
var directoryPath: URL!

Expand All @@ -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)")
Expand Down Expand Up @@ -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)
Expand Down
Loading