Skip to content

Commit c9a9e77

Browse files
committed
add checks for a pending auth token refresh
1 parent 5fed486 commit c9a9e77

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

swift-sdk/Internal/AuthManager.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,18 @@ class AuthManager: IterableInternalAuthManagerProtocol {
4343

4444
// @objc attribute only needed for the pre-iOS 10 Timer constructor in queueAuthTokenExpirationRefresh
4545
@objc func requestNewAuthToken(hasFailedPriorAuth: Bool = false, onSuccess: ((String?) -> Void)? = nil) {
46+
guard !pendingAuth else {
47+
return
48+
}
49+
4650
guard !self.hasFailedPriorAuth || !hasFailedPriorAuth else {
4751
return
4852
}
4953

5054
self.hasFailedPriorAuth = hasFailedPriorAuth
5155

56+
pendingAuth = true
57+
5258
delegate?.onAuthTokenRequested { [weak self] retrievedAuthToken in
5359
self?.onAuthTokenReceived(retrievedAuthToken: retrievedAuthToken, onSuccess: onSuccess)
5460
}
@@ -68,6 +74,7 @@ class AuthManager: IterableInternalAuthManagerProtocol {
6874

6975
private var authToken: String?
7076

77+
private var pendingAuth: Bool = false
7178
private var hasFailedPriorAuth: Bool = false
7279

7380
private weak var delegate: IterableAuthDelegate?
@@ -86,6 +93,8 @@ class AuthManager: IterableInternalAuthManagerProtocol {
8693
}
8794

8895
private func onAuthTokenReceived(retrievedAuthToken: String?, onSuccess: ((String?) -> Void)?) {
96+
pendingAuth = false
97+
8998
authToken = retrievedAuthToken
9099

91100
storeAuthToken()

tests/swift-sdk-swift-tests/AuthTests.swift

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,7 @@ class AuthTests: XCTestCase {
614614
onSuccess: { token in
615615
XCTAssertEqual(token, AuthTests.authToken)
616616
condition1.fulfill()
617-
})
617+
})
618618

619619
wait(for: [condition1], timeout: testExpectationTimeout)
620620
}
@@ -665,6 +665,44 @@ class AuthTests: XCTestCase {
665665
wait(for: [condition2], timeout: testExpectationTimeoutForInverted)
666666
}
667667

668+
func testRefreshTimerQueueRejection() {
669+
let condition1 = expectation(description: "\(#function) - first refresh timer never happened called")
670+
let condition2 = expectation(description: "\(#function) - second refresh timer should not have been called")
671+
condition2.isInverted = true
672+
673+
class AsyncAuthDelegate: IterableAuthDelegate {
674+
func onAuthTokenRequested(completion: @escaping AuthTokenRetrievalHandler) {
675+
DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
676+
completion(AuthTests.authToken)
677+
}
678+
}
679+
}
680+
681+
let authDelegate = AsyncAuthDelegate()
682+
683+
let config = IterableConfig()
684+
config.authDelegate = authDelegate
685+
686+
let authManager = AuthManager(delegate: config.authDelegate,
687+
expirationRefreshPeriod: config.expiringAuthTokenRefreshPeriod,
688+
localStorage: MockLocalStorage(),
689+
dateProvider: MockDateProvider())
690+
691+
authManager.requestNewAuthToken(hasFailedPriorAuth: false,
692+
onSuccess: { token in
693+
XCTAssertEqual(token, AuthTests.authToken)
694+
condition1.fulfill()
695+
})
696+
697+
authManager.requestNewAuthToken(hasFailedPriorAuth: false,
698+
onSuccess: { token in
699+
condition2.fulfill()
700+
})
701+
702+
wait(for: [condition1], timeout: testExpectationTimeout)
703+
wait(for: [condition2], timeout: 3)
704+
}
705+
668706
// MARK: - Private
669707

670708
class DefaultAuthDelegate: IterableAuthDelegate {

0 commit comments

Comments
 (0)