-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6695854
commit 826b1e2
Showing
1 changed file
with
58 additions
and
0 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
...ces/Unit/Sessions/WebAuthentication/ThrottledWebAuthenticationSessionDecoratorTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")!) | ||
} | ||
} |