Skip to content

Commit

Permalink
fix logs and logs subscription
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrey Frolov committed Jan 30, 2025
1 parent 08ca5cd commit 92be7d6
Show file tree
Hide file tree
Showing 11 changed files with 33 additions and 109 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,20 @@ open class RequestCreatorNode<Output>: AsyncNode {
}

private func getLogMessage(_ data: TransportURLRequest) -> LogChain {
var message = "input: \(type(of: data))\n\t"
message += "method: \(data.method.rawValue)\n\t"
message += "url: \(data.url.absoluteString)\n\t"
message += "headers: \(data.headers)\n\t"
message += "raw: \(String(describing: data.raw))\n\t"
var message = "input: \(type(of: data))\n"
message += "method: \(data.method.rawValue)\n"
message += "url: \(data.url.absoluteString)\n"
message += "headers: \(data.headers)\n"
message += "raw: \(serilizeDataForLog(data: data.raw))\n"

return LogChain(message, id: self.objectName, logType: .info, order: LogOrder.requestCreatorNode)
}

private func serilizeDataForLog(data: Data?) -> String {
guard let data = data, let parsed = String(data: data, encoding: .utf8) else {
return String(describing: data)
}
return parsed
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ open class RequestSenderNode<Type>: AsyncNode, Aborter {

async let nodeResponse = nodeResponse(request, logContext: logContext)

log += "Request sended!" + .lineTabDeilimeter
log += "Request is sent!" + .lineTabDeilimeter

let response = await nodeResponse

Expand Down
2 changes: 1 addition & 1 deletion NodeKit/NodeKit/Utils/Logging/Log/LogSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ public protocol LogSession: Actor {
/// Request Route
var route: URLRouteProvider? { get }

func subscribe(_ subscription: @escaping ([Log]) -> Void)
func subscribe(_ subscription: @escaping ([Log]) async -> Void)
}
2 changes: 1 addition & 1 deletion NodeKit/NodeKit/Utils/Logging/Log/LoggingProxy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
//

public protocol LoggingProxy {
func handle(session: LogSession)
func handle(session: LogSession) async
}
2 changes: 1 addition & 1 deletion NodeKit/NodeKit/Utils/Logging/LoggerNode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ private extension LoggerNode {

func prepare(logContext: LoggingContextProtocol) async {
await logContext.set(method: method, route: route)
loggingProxy?.handle(session: logContext)
await loggingProxy?.handle(session: logContext)
}

func set(error: Error, in logContext: LoggingContextProtocol) async {
Expand Down
43 changes: 8 additions & 35 deletions NodeKit/NodeKit/Utils/Logging/LoggingContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public protocol LoggingContextProtocol: LogSession {
func set(method: Method?, route: URLRouteProvider?)

/// Notify subscriptions about completion.
func complete()
func complete() async
}

public actor LoggingContext: LoggingContextProtocol {
Expand All @@ -38,12 +38,7 @@ public actor LoggingContext: LoggingContextProtocol {
public private(set) var log: LogableChain?

/// Log subscriptions.
private var logSubscriptions: [([Log]) -> Void] = []
private var isCompleted = false

private var logs: [Log]? {
return log?.flatMap().sorted(by: { $0.order < $1.order })
}
private var logSubscriptions: [([Log]) async -> Void] = []

/// Adds a log message to the context.
/// If the context did not have a root log, the passed log will become the root.
Expand Down Expand Up @@ -78,40 +73,18 @@ public actor LoggingContext: LoggingContextProtocol {
}

/// Add subscriptions for logs.
public func subscribe(_ subscription: @escaping ([Log]) -> Void) {
guard isCompleted else {
logSubscriptions.append(subscription)
return
}
notify(subscription: subscription)
public func subscribe(_ subscription: @escaping ([Log]) async -> Void) {
logSubscriptions.append(subscription)
}

/// Notify subscriptions about completion.
public func complete() {
isCompleted = true
notifySubscriptions()
}

}

// MARK: - Private Methods

private extension LoggingContext {

func notifySubscriptions() {
guard let logs = logs, !logs.isEmpty else {
public func complete() async {
guard let logs = log?.flatMap().sorted(by: { $0.order < $1.order }), !logs.isEmpty else {
return
}
logSubscriptions.forEach { subscription in
subscription(logs)
}
}

func notify(subscription: @escaping ([Log]) -> Void) {
guard let logs = logs, !logs.isEmpty else {
return
for subscription in logSubscriptions {
await subscription(logs)
}
subscription(logs)
}

}
6 changes: 3 additions & 3 deletions NodeKit/NodeKitMock/LogSessionMock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ public actor LogSessionMock: LogSession {

public var invokedSubscribe = false
public var invokedSubscribeCount = 0
public var invokedSubscribeParameter: (([Log]) -> Void)?
public var invokedSubscribeParameterList: [([Log]) -> Void] = []
public var invokedSubscribeParameter: (([Log]) async -> Void)?
public var invokedSubscribeParameterList: [([Log]) async -> Void] = []

public func subscribe(_ subscription: @escaping ([Log]) -> Void) {
public func subscribe(_ subscription: @escaping ([Log]) async -> Void) {
invokedSubscribe = true
invokedSubscribeCount += 1
invokedSubscribeParameter = subscription
Expand Down
8 changes: 4 additions & 4 deletions NodeKit/NodeKitMock/LoggingContextMock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ public actor LoggingContextMock: LoggingContextProtocol {

public var invokedSubscribe = false
public var invokedSubscribeCount = 0
public var invokedSubscribeParameter: (([Log]) -> Void)?
public var invokedSubscribeParameterList: [([Log]) -> Void] = []
public var invokedSubscribeParameter: (([Log]) async -> Void)?
public var invokedSubscribeParameterList: [([Log]) async -> Void] = []

public func subscribe(_ subscription: @escaping ([Log]) -> Void) {
public func subscribe(_ subscription: @escaping ([Log]) async -> Void) {
invokedSubscribe = true
invokedSubscribeCount += 1
invokedSubscribeParameter = subscription
Expand All @@ -55,7 +55,7 @@ public actor LoggingContextMock: LoggingContextProtocol {
public var invokedComplete = false
public var invokedCompleteCount = 0

public func complete() {
public func complete() async {
invokedComplete = true
invokedCompleteCount += 1
}
Expand Down
2 changes: 1 addition & 1 deletion NodeKit/NodeKitMock/LoggingProxyMock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class LoggingProxyMock: LoggingProxy {
public var invokedHandleParameter: LogSession?
public var invokedHandleParameterList: [LogSession] = []

public func handle(session: LogSession) {
public func handle(session: LogSession) async {
invokedHandle = true
invokedHandleCount += 1
invokedHandleParameter = session
Expand Down
27 changes: 0 additions & 27 deletions NodeKit/NodeKitTests/UnitTests/Coding/EncodingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -176,33 +176,6 @@ final class EncodingTests: XCTestCase {
XCTAssertEqual(unwrappedResult, expectedResult)
}

func testAsyncProcess_whenEncodingError_thenErrorReceived() async throws {
// given

let wrongString = String(bytes: [0xD8, 0x00] as [UInt8], encoding: String.Encoding.utf16BigEndian)!
let url = "http://test.com/usr"
let dataRaw: Json = ["id": wrongString]
let urlParameters = TransportURLParameters(method: .head, url: URL(string: url)!)
let encodingModel = RequestEncodingModel(
urlParameters: urlParameters,
raw: dataRaw,
encoding: .json
)

nextNodeMock.stubbedAsyncProccessResult = .success([:])

// when

let result = await sut.process(encodingModel, logContext: logContextMock)

// then

let unwrappedResult = try XCTUnwrap(result.error as? RequestEncodingNodeError)

XCTAssertFalse(nextNodeMock.invokedAsyncProcess)
XCTAssertEqual(unwrappedResult, .unsupportedDataType)
}

func testAsyncProcess_whenEncodingParametersMissed_thenErrorReceived() async throws {
// given

Expand Down
30 changes: 0 additions & 30 deletions NodeKit/NodeKitTests/UnitTests/Logging/LoggingContextTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -184,36 +184,6 @@ final class LoggingContextTests: XCTestCase {
XCTAssertEqual(receivedLogs, [firstLog, fourthLog, thirdLog, secondLog])
}

func testLog_withLogSubscription_whenSubscibeAfterComplete() async throws {
// given

let firstLog = LogChain("Test first message", id: "Test first id", logType: .failure)
let secondLog = LogChain("Test second message", id: "Test second id", logType: .failure)
let thirdLog = LogChain("Test third message", id: "Test third id", logType: .info)
let fourthLog = LogChain("Test fourth message", id: "Test fourth id", logType: .info)

var logs: [[Log]] = []

// when

await sut.add(firstLog)
await sut.add(secondLog)
await sut.add(thirdLog)
await sut.add(fourthLog)
await sut.complete()

await sut.subscribe {
logs.append($0)
}

// then

let receivedLogs = try logs.flatMap {
try XCTUnwrap($0 as? [LogChain])
}
XCTAssertEqual(receivedLogs, [firstLog, fourthLog, thirdLog, secondLog])
}

func testLog_withLogSubscription_whenLogIsNil() async {
// given

Expand Down

0 comments on commit 92be7d6

Please sign in to comment.