Skip to content

Commit

Permalink
SPT-1998 fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mrandrewsmith committed Mar 26, 2024
1 parent d9eea1d commit 6f954ed
Show file tree
Hide file tree
Showing 16 changed files with 723 additions and 231 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Foundation

/// Модель представления ответа сервера.
/// Используется для передачи информации внутри цепочки обработки ответа.
public struct UrlDataResponse {
public struct UrlDataResponse: Equatable {
/// Запрос, отправленный на сервер.
public let request: URLRequest
/// Ответ, полученный от сервера
Expand All @@ -33,5 +33,4 @@ public struct UrlDataResponse {
self.metrics = metrics
self.serializationDuration = serializationDuration
}

}
2 changes: 1 addition & 1 deletion NodeKit/Layers/Utils/AccessSafe/TokenRefresherNode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ open class TokenRefresherNode: AsyncNode {
/// - Parameter tokenRefresherActor: Актор для обновления токена.
public init(tokenRefreshChain: any AsyncNode<Void, Void>, tokenRefresherActor: TokenRefresherActorProtocol) {
self.tokenRefreshChain = tokenRefreshChain
self.tokenRefresherActor = TokenRefresherActor(tokenRefreshChain: tokenRefreshChain)
self.tokenRefresherActor = tokenRefresherActor
self.observers = []
}

Expand Down
8 changes: 7 additions & 1 deletion NodeKit/Layers/Utils/RequestAborterNode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,13 @@ open class AborterNode<Input, Output>: AsyncNode {
}
.asyncFlatMap {
return await withTaskCancellationHandler(
operation: { return await next.process(data, logContext: logContext) },
operation: {
return await .withMappedExceptions {
let result = await next.process(data, logContext: logContext)
try Task.checkCancellation()
return result
}
},
onCancel: { aborter.cancel(logContext: logContext) }
)
}
Expand Down
58 changes: 56 additions & 2 deletions NodeKitTests/UnitTests/AborterNode/AbortingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ final class AbortingTests: XCTestCase {

// MARK: - Dependencies

private var logContextMock: LoggingContextMock!
private var aborterMock: AborterMock!
private var nextNodeMock: AsyncNodeMock<Void, Void>!

Expand All @@ -29,13 +30,15 @@ final class AbortingTests: XCTestCase {
super.setUp()
aborterMock = AborterMock()
nextNodeMock = AsyncNodeMock()
logContextMock = LoggingContextMock()
sut = AborterNode(next: nextNodeMock, aborter: aborterMock)
}

override func tearDown() {
super.tearDown()
aborterMock = nil
nextNodeMock = nil
logContextMock = nil
sut = nil
}

Expand Down Expand Up @@ -90,9 +93,60 @@ final class AbortingTests: XCTestCase {
XCTAssertEqual(errorCalls, 0)
}

func testAsyncAbort_whenTaskCancelBeforeProcess_thenProcessNotCalled() {
func testAsyncAbort_whenTaskCancelBeforeProcess_thenProcessNotCalled() async {
// when

let task = Task {
try? await Task.sleep(nanoseconds: 10_000_000)
return await sut.process((), logContext: logContextMock)
}

task.cancel()
let result = await task.value

// then

XCTAssertFalse(nextNodeMock.invokedProcess)
switch result {
case .success:
XCTFail("Неожиданный результат")
case .failure(let error):
XCTAssertTrue(error is CancellationError)
}
}

func testAsyncAbort_whenTaskCancelAfterProcess_thenProcessCalled_andPassedSuccess() {
func testAsyncAbort_whenTaskCancelAfterProcess_thenProcessCalled_andPassedSuccess() async {
// given

nextNodeMock.stubbedAsyncProcessRunFunction = {
try? await Task.sleep(nanoseconds: 10_000_000)
}
nextNodeMock.stubbedAsyncProccessResult = .success(())

// when

let task = Task {
return await sut.process((), logContext: logContextMock)
}

let cancelTask = Task {
try? await Task.sleep(nanoseconds: 5_000_000)
task.cancel()
}

let result = await task.value

await cancelTask.value

// then

XCTAssertEqual(nextNodeMock.invokedAsyncProcessCount, 1)
XCTAssertEqual(aborterMock.invokedAsyncCancelCount, 1)
switch result {
case .success:
XCTFail("Неожиданный результат")
case .failure(let error):
XCTAssertTrue(error is CancellationError)
}
}
}
Loading

0 comments on commit 6f954ed

Please sign in to comment.