Skip to content

Commit

Permalink
Removed queue switching (#44)
Browse files Browse the repository at this point in the history
* Not switching queues inside BlackBox instance

* Actualized tests
  • Loading branch information
AllDmeat authored Dec 21, 2023
1 parent 69fafe2 commit a8bdd3f
Show file tree
Hide file tree
Showing 10 changed files with 236 additions and 307 deletions.
86 changes: 33 additions & 53 deletions Sources/BlackBox/BlackBox.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,12 @@ public class BlackBox {
public static var instance = BlackBox.default

private let loggers: [BBLoggerProtocol]
private let queue: DispatchQueue

/// Creates `BlackBox` instance
/// - Parameters:
/// - loggers: Instances to receive logs from `BlackBox`
/// - queue: Queue to receive logs on from `BlackBox`. Must be serial to guarantee correct logs order.
public init(loggers: [BBLoggerProtocol],
queue: DispatchQueue = .init(label: String(describing: BlackBox.self))) {
public init(loggers: [BBLoggerProtocol]) {
self.loggers = loggers
self.queue = queue
}
}

Expand Down Expand Up @@ -186,26 +182,22 @@ extension BlackBox {
function: StaticString,
line: UInt
) {
queue.async {
let source = GenericEvent.Source(
fileID: fileID,
function: function,
line: line
)
let event = BlackBox.GenericEvent(
message.description,
userInfo: userInfo,
serviceInfo: serviceInfo,
level: level,
category: category,
parentEvent: parentEvent,
source: source
)

self.loggers.forEach { logger in
logger.log(event)
}
}
let source = GenericEvent.Source(
fileID: fileID,
function: function,
line: line
)
let event = BlackBox.GenericEvent(
message.description,
userInfo: userInfo,
serviceInfo: serviceInfo,
level: level,
category: category,
parentEvent: parentEvent,
source: source
)

loggers.forEach { $0.log(event) }
}

func log(
Expand All @@ -217,24 +209,20 @@ extension BlackBox {
function: StaticString,
line: UInt
) {
queue.async {
let source = GenericEvent.Source(
fileID: fileID,
function: function,
line: line
)
let event = BlackBox.ErrorEvent(
error: error,
serviceInfo: serviceInfo,
category: category,
parentEvent: parentEvent,
source: source
)

self.loggers.forEach { logger in
logger.log(event)
}
}
let source = GenericEvent.Source(
fileID: fileID,
function: function,
line: line
)
let event = BlackBox.ErrorEvent(
error: error,
serviceInfo: serviceInfo,
category: category,
parentEvent: parentEvent,
source: source
)

loggers.forEach { $0.log(event) }
}

func logStart(
Expand Down Expand Up @@ -271,11 +259,7 @@ extension BlackBox {
func logStart(
_ event: BlackBox.StartEvent
) {
queue.async {
self.loggers.forEach { logger in
logger.logStart(event)
}
}
loggers.forEach { $0.logStart(event) }
}

func logEnd(
Expand Down Expand Up @@ -309,11 +293,7 @@ extension BlackBox {
func logEnd(
_ event: BlackBox.EndEvent
) {
queue.async {
self.loggers.forEach { logger in
logger.logEnd(event)
}
}
loggers.forEach { $0.logEnd(event) }
}
}

Expand Down
62 changes: 31 additions & 31 deletions Tests/BlackBoxTests/BlackBoxEndEventTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ class BlackBoxEndEventTests: BlackBoxTestCase {
}

func test_startEvent() {
waitForLog { BlackBox.logEnd(event) }
XCTAssertEqual(logger.endEvent?.startEvent, event)
BlackBox.logEnd(event)
XCTAssertEqual(testableLogger.endEvent?.startEvent, event)
}

func test_duration() {
Expand Down Expand Up @@ -63,77 +63,77 @@ class BlackBoxEndEventTests: BlackBoxTestCase {

func test_message() throws {
let event = BlackBox.StartEvent(timestamp: Date().addingTimeInterval(-1), "Test")
waitForLog { BlackBox.logEnd(event) }
let endEvent = try XCTUnwrap(logger.endEvent)
BlackBox.logEnd(event)
let endEvent = try XCTUnwrap(testableLogger.endEvent)
XCTAssertTrue(endEvent.durationFormatted.hasPrefix("1.00"))
}

func test_rawMessage() {
waitForLog { BlackBox.logEnd(event) }
XCTAssertEqual(logger.endEvent?.rawMessage.description, "Test".description)
BlackBox.logEnd(event)
XCTAssertEqual(testableLogger.endEvent?.rawMessage.description, "Test".description)
}

func test_customMessage() throws {
let event = BlackBox.StartEvent("Test")
waitForLog { BlackBox.logEnd(event, message: "Custom Message") }
let endEvent = try XCTUnwrap(logger.endEvent)
BlackBox.logEnd(event, message: "Custom Message")
let endEvent = try XCTUnwrap(testableLogger.endEvent)
XCTAssertTrue(endEvent.message.hasPrefix("End: Custom Message"))
}

func test_userInfo() {
waitForLog { BlackBox.logEnd(event, userInfo: ["name": "Kenobi"]) }
XCTAssertEqual(logger.endEvent?.userInfo as? [String: String], ["name": "Kenobi"])
BlackBox.logEnd(event, userInfo: ["name": "Kenobi"])
XCTAssertEqual(testableLogger.endEvent?.userInfo as? [String: String], ["name": "Kenobi"])
}

func test_serviceInfo() {
waitForLog { BlackBox.logEnd(event, serviceInfo: Lightsaber(color: "purple")) }
XCTAssertEqual(logger.endEvent?.serviceInfo as? Lightsaber, Lightsaber(color: "purple"))
BlackBox.logEnd(event, serviceInfo: Lightsaber(color: "purple"))
XCTAssertEqual(testableLogger.endEvent?.serviceInfo as? Lightsaber, Lightsaber(color: "purple"))
}

func test_levelComeFromStartEvent() {
event = BlackBox.StartEvent("Test", level: .warning)
waitForLog { BlackBox.logEnd(event) }
XCTAssertEqual(logger.endEvent?.level, .warning)
BlackBox.logEnd(event)
XCTAssertEqual(testableLogger.endEvent?.level, .warning)
}

func test_defaultLevel() {
waitForLog { BlackBox.logEnd(event) }
XCTAssertEqual(logger.endEvent?.level, .debug)
BlackBox.logEnd(event)
XCTAssertEqual(testableLogger.endEvent?.level, .debug)
}

func test_category() {
waitForLog { BlackBox.logEnd(event, category: "Analytics") }
XCTAssertEqual(logger.endEvent?.category, "Analytics")
BlackBox.logEnd(event, category: "Analytics")
XCTAssertEqual(testableLogger.endEvent?.category, "Analytics")
}

func test_parentEvent() {
waitForLog { BlackBox.logEnd(event) }
XCTAssertEqual(logger.endEvent?.parentEvent, event)
XCTAssertEqual(logger.endEvent?.startEvent, event)
BlackBox.logEnd(event)
XCTAssertEqual(testableLogger.endEvent?.parentEvent, event)
XCTAssertEqual(testableLogger.endEvent?.startEvent, event)
}

func test_fileID() {
waitForLog { BlackBox.logEnd(event) }
XCTAssertEqual(logger.endEvent?.source.fileID.description, "BlackBoxTests/BlackBoxEndEventTests.swift")
BlackBox.logEnd(event)
XCTAssertEqual(testableLogger.endEvent?.source.fileID.description, "BlackBoxTests/BlackBoxEndEventTests.swift")
}

func test_module() {
waitForLog { BlackBox.logEnd(event) }
XCTAssertEqual(logger.endEvent?.source.module, "BlackBoxTests")
BlackBox.logEnd(event)
XCTAssertEqual(testableLogger.endEvent?.source.module, "BlackBoxTests")
}

func test_filename() {
waitForLog { BlackBox.logEnd(event) }
XCTAssertEqual(logger.endEvent?.source.filename, "BlackBoxEndEventTests")
BlackBox.logEnd(event)
XCTAssertEqual(testableLogger.endEvent?.source.filename, "BlackBoxEndEventTests")
}

func test_function() {
waitForLog { BlackBox.logEnd(event) }
XCTAssertEqual(logger.endEvent?.source.function.description, "test_function()")
BlackBox.logEnd(event)
XCTAssertEqual(testableLogger.endEvent?.source.function.description, "test_function()")
}

func test_line() {
waitForLog { BlackBox.logEnd(event) }
XCTAssertEqual(logger.endEvent?.source.line, 136)
BlackBox.logEnd(event)
XCTAssertEqual(testableLogger.endEvent?.source.line, 136)
}
}
56 changes: 28 additions & 28 deletions Tests/BlackBoxTests/BlackBoxErrorEventTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,73 +11,73 @@ import XCTest

class BlackBoxErrorEventTests: BlackBoxTestCase {
func test_error() {
waitForLog { BlackBox.log(AnakinKills.maceWindu) }
XCTAssertEqual(logger.errorEvent?.error as? AnakinKills, AnakinKills.maceWindu)
BlackBox.log(AnakinKills.maceWindu)
XCTAssertEqual(testableLogger.errorEvent?.error as? AnakinKills, AnakinKills.maceWindu)
}

func test_message() {
waitForLog { BlackBox.log(AnakinKills.maceWindu) }
XCTAssertEqual(logger.errorEvent?.message, "AnakinKills.maceWindu")
BlackBox.log(AnakinKills.maceWindu)
XCTAssertEqual(testableLogger.errorEvent?.message, "AnakinKills.maceWindu")
}

func test_message_fromAnotherModule() {
waitForLog { ExampleService().logSomeError() }
XCTAssertEqual(logger.errorEvent?.message, "ExampleError.taskFailed")
ExampleService().logSomeError()
XCTAssertEqual(testableLogger.errorEvent?.message, "ExampleError.taskFailed")
}

func test_message_hasNoWithAssociatedValue() {
waitForLog { BlackBox.log(AnakinKills.younglings(count: 11)) }
XCTAssertEqual(logger.errorEvent?.message, "AnakinKills.younglings")
BlackBox.log(AnakinKills.younglings(count: 11))
XCTAssertEqual(testableLogger.errorEvent?.message, "AnakinKills.younglings")
}

func test_userInfo_hasAssociatedValue() {
waitForLog { BlackBox.log(AnakinKills.younglings(count: 11)) }
XCTAssertEqual(logger.errorEvent?.userInfo as? [String: Int], ["count": 11])
BlackBox.log(AnakinKills.younglings(count: 11))
XCTAssertEqual(testableLogger.errorEvent?.userInfo as? [String: Int], ["count": 11])
}

func test_serviceInfo() {
waitForLog { BlackBox.log(AnakinKills.maceWindu, serviceInfo: Lightsaber(color: "purple")) }
XCTAssertEqual(logger.errorEvent?.serviceInfo as? Lightsaber, Lightsaber(color: "purple"))
BlackBox.log(AnakinKills.maceWindu, serviceInfo: Lightsaber(color: "purple"))
XCTAssertEqual(testableLogger.errorEvent?.serviceInfo as? Lightsaber, Lightsaber(color: "purple"))
}

func test_defaultLevel() {
waitForLog { BlackBox.log(AnakinKills.maceWindu) }
XCTAssertEqual(logger.errorEvent?.level, .error)
BlackBox.log(AnakinKills.maceWindu)
XCTAssertEqual(testableLogger.errorEvent?.level, .error)
}

func test_category() {
waitForLog { BlackBox.log(AnakinKills.maceWindu, category: "Analytics") }
XCTAssertEqual(logger.errorEvent?.category, "Analytics")
BlackBox.log(AnakinKills.maceWindu, category: "Analytics")
XCTAssertEqual(testableLogger.errorEvent?.category, "Analytics")
}

func test_parentEvent() {
let parentEvent = BlackBox.GenericEvent("Test")
waitForLog { BlackBox.log(AnakinKills.maceWindu, parentEvent: parentEvent) }
XCTAssertEqual(logger.errorEvent?.parentEvent, parentEvent)
BlackBox.log(AnakinKills.maceWindu, parentEvent: parentEvent)
XCTAssertEqual(testableLogger.errorEvent?.parentEvent, parentEvent)
}

func test_fileID() {
waitForLog { BlackBox.log(AnakinKills.maceWindu) }
XCTAssertEqual(logger.errorEvent?.source.fileID.description, "BlackBoxTests/BlackBoxErrorEventTests.swift")
BlackBox.log(AnakinKills.maceWindu)
XCTAssertEqual(testableLogger.errorEvent?.source.fileID.description, "BlackBoxTests/BlackBoxErrorEventTests.swift")
}

func test_module() {
waitForLog { BlackBox.log(AnakinKills.maceWindu) }
XCTAssertEqual(logger.errorEvent?.source.module, "BlackBoxTests")
BlackBox.log(AnakinKills.maceWindu)
XCTAssertEqual(testableLogger.errorEvent?.source.module, "BlackBoxTests")
}

func test_filename() {
waitForLog { BlackBox.log(AnakinKills.maceWindu) }
XCTAssertEqual(logger.errorEvent?.source.filename, "BlackBoxErrorEventTests")
BlackBox.log(AnakinKills.maceWindu)
XCTAssertEqual(testableLogger.errorEvent?.source.filename, "BlackBoxErrorEventTests")
}

func test_function() {
waitForLog { BlackBox.log(AnakinKills.maceWindu) }
XCTAssertEqual(logger.errorEvent?.source.function.description, "test_function()")
BlackBox.log(AnakinKills.maceWindu)
XCTAssertEqual(testableLogger.errorEvent?.source.function.description, "test_function()")
}

func test_line() {
waitForLog { BlackBox.log(AnakinKills.maceWindu) }
XCTAssertEqual(logger.errorEvent?.source.line, 80)
BlackBox.log(AnakinKills.maceWindu)
XCTAssertEqual(testableLogger.errorEvent?.source.line, 80)
}
}
Loading

0 comments on commit a8bdd3f

Please sign in to comment.