From 8e7c8d3b02786ca80f6b2dcf825d4b3f2b64328c Mon Sep 17 00:00:00 2001 From: Mike Nachbaur Date: Fri, 16 Feb 2024 12:38:58 -0800 Subject: [PATCH] Write a test that proves issue #170 --- .../CredentialRefreshTests.swift | 39 ++++++++++++++++--- .../TestCommon/TimeInterval+Extensions.swift | 27 +++++++++++++ 2 files changed, 61 insertions(+), 5 deletions(-) create mode 100644 Tests/TestCommon/TimeInterval+Extensions.swift diff --git a/Tests/AuthFoundationTests/CredentialRefreshTests.swift b/Tests/AuthFoundationTests/CredentialRefreshTests.swift index 2df6f56c0..91d7af9f4 100644 --- a/Tests/AuthFoundationTests/CredentialRefreshTests.swift +++ b/Tests/AuthFoundationTests/CredentialRefreshTests.swift @@ -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..