Skip to content

Commit

Permalink
Test throttled session
Browse files Browse the repository at this point in the history
  • Loading branch information
andrii-vysotskyi-cko committed Oct 10, 2024
1 parent 6695854 commit 826b1e2
Showing 1 changed file with 58 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//
// ThrottledWebAuthenticationSessionDecoratorTests.swift
// ProcessOut
//
// Created by Andrii Vysotskyi on 10.10.2024.
//

@testable import ProcessOut
import XCTest

final class ThrottledWebAuthenticationSessionDecoratorTests: XCTestCase {

// MARK: - Wait

func test_authenticate_allowsOneAuthenticationAtTime() async throws {
// Given
let mockSession = MockWebAuthenticationSession()
mockSession.authenticateFromClosure = { _, _, _ in
try await Task.sleep(for: .seconds(5))
return URL(string: "response.com")!
}
let sut = ThrottledWebAuthenticationSessionDecorator(session: mockSession)

// When
Task {
_ = try await sut.authenticate(using: URL(string: "request1.com")!)
}
Task {
_ = try await sut.authenticate(using: URL(string: "request2.com")!)
}
try await Task.sleep(for: .seconds(1))

// Then
XCTAssertEqual(mockSession.authenticateCallsCount, 1)
}

func test_authenticate_throttlesAuthentications() async throws {
var lastAuthenticationStartTime: DispatchTime?

// Given
let mockSession = MockWebAuthenticationSession()
mockSession.authenticateFromClosure = { _, _, _ in
// Then
if let lastAuthenticationStartTime {
let delay = Int(DispatchTime.now().uptimeNanoseconds - lastAuthenticationStartTime.uptimeNanoseconds)
let expectedDelay = 1_000_000_000, tolerance = 500_000_000 // 1 and 0.5 seconds respectfully
XCTAssertTrue(abs(delay - expectedDelay) <= tolerance)
}
lastAuthenticationStartTime = DispatchTime.now()
return URL(string: "response.com")!
}
let sut = ThrottledWebAuthenticationSessionDecorator(session: mockSession)

// When
_ = try await sut.authenticate(using: URL(string: "request1.com")!)
_ = try await sut.authenticate(using: URL(string: "request2.com")!)
}
}

0 comments on commit 826b1e2

Please sign in to comment.