Update DiskLogger tests to use new API

This commit is contained in:
Wojciech Nagrodzki 2019-11-04 19:22:36 +01:00
parent 04ab08f294
commit 59e25369e6
Signed by: wnagrodzki
GPG key ID: E9D0EB0302264569
3 changed files with 92 additions and 86 deletions

View file

@ -18,7 +18,7 @@ protocol OSFileHandle {
extension FileHandle: OSFileHandle {
func osSeekToEndOfFile() throws -> UInt64 {
if #available(iOS 13.0, *) {
if #available(OSX 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) {
var offsetInFile: UInt64 = 0
try __seek(toEndReturningOffset:&offsetInFile)
return offsetInFile
@ -28,7 +28,7 @@ extension FileHandle: OSFileHandle {
}
func osWrite(_ data: Data) throws {
if #available(iOS 13.0, *) {
if #available(OSX 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) {
try __write(data, error: ())
} else {
fatalError()
@ -36,7 +36,7 @@ extension FileHandle: OSFileHandle {
}
func osSynchronizeFile() throws {
if #available(iOS 13.0, *) {
if #available(OSX 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) {
try synchronize()
} else {
fatalError()
@ -44,7 +44,7 @@ extension FileHandle: OSFileHandle {
}
func osCloseFile() throws {
if #available(iOS 13.0, *) {
if #available(OSX 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) {
try close()
} else {
fatalError()

View file

@ -30,89 +30,95 @@ class DiskLoggerTests: XCTestCase {
let logURL = URL(fileURLWithPath: "/var/log/application.log")
func testLoggingMessageToFile() {
let expectation = XCTestExpectation(description: "write(_:) was called on SizeLimitedFile")
expectation.expectedFulfillmentCount = 1
let fimeManager = FileManagerStub()
let sizeLimitedFileFactory = SizeLimitedFileMockFactory(writeCall: expectation)
let logrotateFactory = LogrotateMockFactory()
let logger = DiskLogger(fileURL: logURL,
fileSizeLimit: 1024,
rotations: 1,
fileManager: fimeManager,
sizeLimitedFileFactory: sizeLimitedFileFactory,
logrotateFactory: logrotateFactory)
logger.log("message", level: .critical)
let result = XCTWaiter().wait(for: [expectation], timeout: 1.0)
XCTAssertEqual(result, .completed)
XCTAssertEqual(sizeLimitedFileFactory.files.count, 1)
// "2018-08-30 17:23:11.514 <crit> DiskLoggerTests:46 testWritingMessageToFile() a message\n"
let loggedMessage = String(decoding: sizeLimitedFileFactory.files[0].data, as: UTF8.self)
let expectedSuffix = " <crit> DiskLoggerTests:46 testLoggingMessageToFile() message\n"
XCTAssertTrue(loggedMessage.hasSuffix(expectedSuffix))
if #available(OSX 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) {
let expectation = XCTestExpectation(description: "write(_:) was called on SizeLimitedFile")
expectation.expectedFulfillmentCount = 1
let fimeManager = FileManagerStub()
let sizeLimitedFileFactory = SizeLimitedFileMockFactory(writeCall: expectation)
let logrotateFactory = LogrotateMockFactory()
let logger = DiskLogger(fileURL: logURL,
fileSizeLimit: 1024,
rotations: 1,
fileManager: fimeManager,
sizeLimitedFileFactory: sizeLimitedFileFactory,
logrotateFactory: logrotateFactory)
logger.log("message", level: .critical)
let result = XCTWaiter().wait(for: [expectation], timeout: 1.0)
XCTAssertEqual(result, .completed)
XCTAssertEqual(sizeLimitedFileFactory.files.count, 1)
// "2018-08-30 17:23:11.514 <crit> DiskLoggerTests:46 testWritingMessageToFile() a message\n"
let loggedMessage = String(decoding: sizeLimitedFileFactory.files[0].data, as: UTF8.self)
let expectedSuffix = " <crit> DiskLoggerTests:47 testLoggingMessageToFile() message\n"
XCTAssertTrue(loggedMessage.hasSuffix(expectedSuffix))
}
}
func testExceedingFileSizeLimit() {
let expectation = XCTestExpectation(description: "write(_:) was called on SizeLimitedFile")
expectation.expectedFulfillmentCount = 2
let fimeManager = FileManagerStub()
let sizeLimitedFileFactory = SizeLimitedFileMockFactory(writeCall: expectation)
let logrotateFactory = LogrotateMockFactory()
let logger = DiskLogger(fileURL: logURL,
fileSizeLimit: 91,
rotations: 1,
fileManager: fimeManager,
sizeLimitedFileFactory: sizeLimitedFileFactory,
logrotateFactory: logrotateFactory)
logger.log("1st message", level: .critical)
logger.log("2st message", level: .critical)
let result = XCTWaiter().wait(for: [expectation], timeout: 1.0)
XCTAssertEqual(result, .completed)
XCTAssertEqual(sizeLimitedFileFactory.files.count, 2)
// "2018-08-31 18:29:34.748 <crit> DiskLoggerTests:75 testExceedingFileSizeLimit() 2st message\n"
let loggedMessage = String(decoding: sizeLimitedFileFactory.files[1].data, as: UTF8.self)
let expectedSuffix = " <crit> DiskLoggerTests:75 testExceedingFileSizeLimit() 2st message\n"
XCTAssertTrue(loggedMessage.hasSuffix(expectedSuffix))
if #available(OSX 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) {
let expectation = XCTestExpectation(description: "write(_:) was called on SizeLimitedFile")
expectation.expectedFulfillmentCount = 2
let fimeManager = FileManagerStub()
let sizeLimitedFileFactory = SizeLimitedFileMockFactory(writeCall: expectation)
let logrotateFactory = LogrotateMockFactory()
let logger = DiskLogger(fileURL: logURL,
fileSizeLimit: 91,
rotations: 1,
fileManager: fimeManager,
sizeLimitedFileFactory: sizeLimitedFileFactory,
logrotateFactory: logrotateFactory)
logger.log("1st message", level: .critical)
logger.log("2st message", level: .critical)
let result = XCTWaiter().wait(for: [expectation], timeout: 1.0)
XCTAssertEqual(result, .completed)
XCTAssertEqual(sizeLimitedFileFactory.files.count, 2)
// "2018-08-31 18:29:34.748 <crit> DiskLoggerTests:75 testExceedingFileSizeLimit() 2st message\n"
let loggedMessage = String(decoding: sizeLimitedFileFactory.files[1].data, as: UTF8.self)
let expectedSuffix = " <crit> DiskLoggerTests:78 testExceedingFileSizeLimit() 2st message\n"
XCTAssertTrue(loggedMessage.hasSuffix(expectedSuffix))
}
}
func testErrorDuringLogProcess() {
let expectation = XCTestExpectation(description: "write(_:) was called on SizeLimitedFile")
expectation.expectedFulfillmentCount = 2
let fimeManager = FileManagerStub()
let sizeLimitedFileFactory = UnwritableFileStubFactory(writeCall: expectation)
let logrotateFactory = LogrotateMockFactory()
let logger = DiskLogger(fileURL: logURL,
fileSizeLimit: 91,
rotations: 1,
fileManager: fimeManager,
sizeLimitedFileFactory: sizeLimitedFileFactory,
logrotateFactory: logrotateFactory)
logger.log("1st message", level: .critical)
logger.log("2st message", level: .critical)
let result = XCTWaiter().wait(for: [expectation], timeout: 1.0)
XCTAssertEqual(result, .completed)
XCTAssertEqual(sizeLimitedFileFactory.files.count, 1)
// "2018-08-31 18:29:34.748 <crit> DiskLoggerTests:75 testExceedingFileSizeLimit() 2st message\n"
let loggedMessage = String(decoding: sizeLimitedFileFactory.files[0].data, as: UTF8.self)
let expectedOccurence = " <warning> WriteFailed()"
XCTAssertTrue(loggedMessage.contains(expectedOccurence))
if #available(OSX 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) {
let expectation = XCTestExpectation(description: "write(_:) was called on SizeLimitedFile")
expectation.expectedFulfillmentCount = 2
let fimeManager = FileManagerStub()
let sizeLimitedFileFactory = UnwritableFileStubFactory(writeCall: expectation)
let logrotateFactory = LogrotateMockFactory()
let logger = DiskLogger(fileURL: logURL,
fileSizeLimit: 91,
rotations: 1,
fileManager: fimeManager,
sizeLimitedFileFactory: sizeLimitedFileFactory,
logrotateFactory: logrotateFactory)
logger.log("1st message", level: .critical)
logger.log("2st message", level: .critical)
let result = XCTWaiter().wait(for: [expectation], timeout: 1.0)
XCTAssertEqual(result, .completed)
XCTAssertEqual(sizeLimitedFileFactory.files.count, 1)
// "2018-08-31 18:29:34.748 <crit> DiskLoggerTests:75 testExceedingFileSizeLimit() 2st message\n"
let loggedMessage = String(decoding: sizeLimitedFileFactory.files[0].data, as: UTF8.self)
let expectedOccurence = " <warning> WriteFailed()"
XCTAssertTrue(loggedMessage.contains(expectedOccurence))
}
}
}

View file

@ -60,7 +60,7 @@ class SizeLimitedFileTests: XCTestCase {
func testSynchronizingAndClosingFile() throws {
let factory = FileMockFactory()
let writer = try SizeLimitedFileImpl(fileURL: logURL, fileSizeLimit: 1, fileFactory: factory)
writer.synchronizeAndCloseFile()
try writer.synchronizeAndCloseFile()
XCTAssertTrue(factory.mock.synchronizeFileCallCount == 1 && factory.mock.closeFileCallCount == 1)
}
}
@ -89,19 +89,19 @@ private class FileMock: OSFileHandle {
private(set) var synchronizeFileCallCount = 0
private(set) var closeFileCallCount = 0
func seekToEndOfFile() -> UInt64 {
func osSeekToEndOfFile() throws -> UInt64 {
return 0
}
func swift_write(_ data: Data) throws {
func osWrite(_ data: Data) throws {
self.writtenData.append(data)
}
func synchronizeFile() {
func osSynchronizeFile() throws {
synchronizeFileCallCount += 1
}
func closeFile() {
func osCloseFile() throws {
closeFileCallCount += 1
}
}