// The MIT License // // Original work sponsored and donated by The Danish Health Data Authority (http://www.sundhedsdatastyrelsen.dk) // // Copyright (C) 2020 The Danish Health Data Authority (http://www.sundhedsdatastyrelsen.dk) // // Permission is hereby granted, free of charge, to any person obtaining a copy of // this software and associated documentation files (the "Software"), to deal in // the Software without restriction, including without limitation the rights to // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies // of the Software, and to permit persons to whom the Software is furnished to do // so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in all // copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. import XCTest import os.log @testable import Logger final class MultiLoggerTests: XCTestCase { func testMultiLogger() { let expect1 = XCTestExpectation() let expect2 = XCTestExpectation() let singleLogger1 = TestAsyncLogger(expectation: expect2) let singleLogger2 = TestAsyncLogger(expectation: expect1) let singleLogger3 = TestLogger() let singleLogger4 = TestLogger() let multiLogger = MultiLogger(loggers: [singleLogger1, singleLogger2, singleLogger3, singleLogger4]) multiLogger.default("Invoke all the loggers!") multiLogger.debug("Invoke all the loggers!") multiLogger.info("Invoke all the loggers!") multiLogger.error("Invoke all the loggers!") multiLogger.fault("Invoke all the loggers!") // 1 and 2 should be false here, because default log function is sleeping XCTAssertFalse(singleLogger1.isFulfilled()) XCTAssertFalse(singleLogger2.isFulfilled()) // 3 and 4 should be true, because 1 and 2 are running as async XCTAssertTrue(singleLogger3.isFulfilled()) XCTAssertTrue(singleLogger4.isFulfilled()) wait(for: [expect1, expect2], timeout: 5.0) XCTAssertTrue(singleLogger1.isFulfilled()) XCTAssertTrue(singleLogger2.isFulfilled()) XCTAssertTrue(singleLogger3.isFulfilled()) XCTAssertTrue(singleLogger4.isFulfilled()) } static var allTests = [ ("testMultiLogger", testMultiLogger) ] } class TestLogger : LoggerProtocol { var invoked: [String: Bool] = [ OSLogType.default.title: false, OSLogType.debug.title: false, OSLogType.info.title: false, OSLogType.error.title: false, OSLogType.fault.title: false ] // MARK: - Logger func log(_ message: String, at level: OSLogType, file: String, function: String, line: UInt, category: String?) { invoked[level.title] = true } func isFulfilled() -> Bool { invoked.allSatisfy({ $0.value }) } } class TestAsyncLogger : TestLogger, AsyncLoggerProtocol { let expectation: XCTestExpectation init(expectation: XCTestExpectation) { self.expectation = expectation } override func log(_ message: String, at level: OSLogType, file: String, function: String, line: UInt, category: String?) { if level == .debug { sleep(1) //Sleep to prove that it is started as async } super.log(message, at: level, file: file, function: function, line: line, category: category) fulfillExpectationIfDone() } private func fulfillExpectationIfDone() { if isFulfilled() { expectation.fulfill() } } }