Skip to content

Commit

Permalink
Write a test that proves issue #170
Browse files Browse the repository at this point in the history
  • Loading branch information
mikenachbaur-okta committed Feb 20, 2024
1 parent d5b48c3 commit 8e7c8d3
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 5 deletions.
39 changes: 34 additions & 5 deletions Tests/AuthFoundationTests/CredentialRefreshTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -315,26 +315,55 @@ final class CredentialRefreshTests: XCTestCase, OAuth2ClientDelegate {
@available(iOS 13.0, tvOS 13.0, macOS 10.15, watchOS 6, *)
func testRefreshAsync() async throws {
let credential = try credential(for: Token.simpleMockToken)
try await credential.refresh()
try perform {
try await credential.refresh()
}
}

@available(iOS 13.0, tvOS 13.0, macOS 10.15, watchOS 6, *)
func testRefreshIfNeededExpiredAsync() async throws {
let credential = try credential(for: Token.mockToken(issuedOffset: 6000))
try await credential.refreshIfNeeded(graceInterval: 300)
try perform {
try await credential.refreshIfNeeded(graceInterval: 300)
}
}

@available(iOS 13.0, tvOS 13.0, macOS 10.15, watchOS 6, *)
func testRefreshIfNeededWithinGraceIntervalAsync() async throws {
let credential = try credential(for: Token.mockToken(issuedOffset: 0),
expectAPICalls: .none)
try await credential.refreshIfNeeded(graceInterval: 300)
try perform {
try await credential.refreshIfNeeded(graceInterval: 300)
}
}

@available(iOS 13.0, tvOS 13.0, macOS 10.15, watchOS 6, *)
func testRefreshIfNeededOutsideGraceIntervalAsync() async throws {
let credential = try credential(for: Token.mockToken(issuedOffset: 3500))
try await credential.refreshIfNeeded(graceInterval: 300)
let credential = try credential(for: Token.mockToken(issuedOffset: 3500))
try perform {
try await credential.refreshIfNeeded(graceInterval: 300)
}
}

func perform(queueCount: Int = 5, iterationCount: Int = 10, _ block: @escaping () async throws -> Void) rethrows {
let queues: [DispatchQueue] = (0..<queueCount).map { queueNumber in
DispatchQueue(label: "Async queue \(queueNumber)")
}

let group = DispatchGroup()
for queue in queues {
for _ in 0..<iterationCount {
queue.async {
group.enter()
Task {
try await block()
group.leave()
}
}
}
}

_ = group.wait(timeout: .short)
}
#endif
}
27 changes: 27 additions & 0 deletions Tests/TestCommon/TimeInterval+Extensions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// Copyright (c) 2023-Present, Okta, Inc. and/or its affiliates. All rights reserved.
// The Okta software accompanied by this notice is provided pursuant to the Apache License, Version 2.0 (the "License.")
//
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//
// See the License for the specific language governing permissions and limitations under the License.
//

import Foundation

public extension TimeInterval {
static let standard: Self = 3
static let short: Self = 1
static let long: Self = 5
static let veryLong: Self = 10
}

public extension DispatchTime {
static var standard: Self { .now() + .standard }
static var short: Self { .now() + .short }
static var long: Self { .now() + .long }
static var veryLong: Self { .now() + .veryLong }
}

0 comments on commit 8e7c8d3

Please sign in to comment.