Skip to content

Commit

Permalink
Streamline some tests for reliability
Browse files Browse the repository at this point in the history
  • Loading branch information
mikenachbaur-okta committed Oct 30, 2024
1 parent d40817e commit 9f07a6a
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,13 @@ final class CredentialCoordinatorImpl: Sendable, CredentialCoordinator {
DefaultCredentialDataSource()
}

let notificationCenter: NotificationCenter

init(tokenStorage: any TokenStorage = defaultTokenStorage(),
credentialDataSource: any CredentialDataSource = defaultCredentialDataSource())
credentialDataSource: any CredentialDataSource = defaultCredentialDataSource(),
notificationCenter: NotificationCenter = .default)
{
self.notificationCenter = notificationCenter
_credentialDataSource = credentialDataSource
_tokenStorage = tokenStorage

Expand Down Expand Up @@ -182,8 +186,8 @@ final class CredentialCoordinatorImpl: Sendable, CredentialCoordinator {
_default = credential

DispatchQueue.main.async {
NotificationCenter.default.post(name: .defaultCredentialChanged,
object: credential)
self.notificationCenter.post(name: .defaultCredentialChanged,
object: credential)
}
} catch {
// Placeholder for when logging is added in a future release
Expand Down Expand Up @@ -243,13 +247,13 @@ extension CredentialCoordinatorImpl: CredentialDataSourceDelegate {
func credential(dataSource: any CredentialDataSource, created credential: Credential) {
credential.coordinator = self

NotificationCenter.default.post(name: .credentialCreated, object: credential)
self.notificationCenter.post(name: .credentialCreated, object: credential)
}

func credential(dataSource: any CredentialDataSource, removed credential: Credential) {
credential.coordinator = nil

NotificationCenter.default.post(name: .credentialRemoved, object: credential)
self.notificationCenter.post(name: .credentialRemoved, object: credential)
}

func credential(dataSource: any CredentialDataSource, updated credential: Credential) {
Expand Down
7 changes: 5 additions & 2 deletions Tests/AuthFoundationTests/CredentialNotificationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ final class CredentialNotificationTests: XCTestCase {
var userDefaults: UserDefaults!
var storage: UserDefaultsTokenStorage!
var coordinator: CredentialCoordinatorImpl!
var notificationCenter: NotificationCenter!

let token = try! Token(id: "TokenId",
issuedAt: Date(),
Expand All @@ -42,8 +43,9 @@ final class CredentialNotificationTests: XCTestCase {
userDefaults = UserDefaults(suiteName: name)
userDefaults.removePersistentDomain(forName: name)

notificationCenter = NotificationCenter()
storage = UserDefaultsTokenStorage(userDefaults: userDefaults)
coordinator = CredentialCoordinatorImpl(tokenStorage: storage)
coordinator = CredentialCoordinatorImpl(tokenStorage: storage, notificationCenter: notificationCenter)

XCTAssertEqual(storage.allIDs.count, 0)
}
Expand All @@ -59,7 +61,8 @@ final class CredentialNotificationTests: XCTestCase {
func testNotifications() throws {
let oldCredential = coordinator.default

let recorder = NotificationRecorder(observing: [.defaultCredentialChanged])
let recorder = NotificationRecorder(notificationCenter: notificationCenter,
observing: [.defaultCredentialChanged])

let credential = try coordinator.store(token: token, security: [])
sleep(for: .short)
Expand Down
6 changes: 3 additions & 3 deletions Tests/AuthFoundationTests/OAuth2ClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,8 @@ final class OAuth2ClientTests: XCTestCase {
let queue = DispatchQueue(label: "results")
nonisolated(unsafe) var jwksResults = [JWKS]()

for _ in 1...4 {
let expect = expectation(description: "network request")
for index in 1...4 {
let expect = expectation(description: "network request \(index)")
client.jwks { result in
switch result {
case .success(let jwks):
Expand All @@ -191,7 +191,7 @@ final class OAuth2ClientTests: XCTestCase {
}
}

waitForExpectations(timeout: 1.0) { error in
waitForExpectations(timeout: .long) { error in
XCTAssertNil(error)
}

Expand Down
12 changes: 7 additions & 5 deletions Tests/TestCommon/NotificationRecorder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,25 @@
public final class NotificationRecorder: Sendable {
nonisolated(unsafe) private(set) public var notifications: [Notification] = []
nonisolated(unsafe) private var observers = [any NSObjectProtocol]()
let notificationCenter: NotificationCenter

public init(observing: [Notification.Name]? = nil) {
public init(notificationCenter: NotificationCenter = .default,
observing: [Notification.Name]? = nil)
{
self.notificationCenter = notificationCenter
observing?.forEach {
observe($0)
}
}

deinit {
let center = NotificationCenter.default
observers.forEach { observer in
center.removeObserver(observer)
notificationCenter.removeObserver(observer)
}
}

public func observe(_ name: Notification.Name, object: AnyObject? = nil) {
let center = NotificationCenter.default
observers.append(center.addObserver(forName: name, object: object, queue: nil, using: { [weak self] notification in
observers.append(notificationCenter.addObserver(forName: name, object: object, queue: nil, using: { [weak self] notification in
self?.received(notification)
}))
}
Expand Down

0 comments on commit 9f07a6a

Please sign in to comment.