From 0d7667f12d6ede2185511bfc0f93b8f83dd81d5a Mon Sep 17 00:00:00 2001 From: Lawrence Forooghian Date: Wed, 3 May 2023 14:36:38 -0300 Subject: [PATCH] WIP log the active QueueIdentity objects at end of test suite MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit There are loads — why? --- Test/Test Utilities/Test.swift | 14 ++++-- Test/Test Utilities/TestUtilities.swift | 66 +++++++++++++++++++++++-- 2 files changed, 72 insertions(+), 8 deletions(-) diff --git a/Test/Test Utilities/Test.swift b/Test/Test Utilities/Test.swift index 42a3cbd7e..6266cb479 100644 --- a/Test/Test Utilities/Test.swift +++ b/Test/Test Utilities/Test.swift @@ -1,12 +1,18 @@ /** Represents an execution of a test case method. */ -struct Test { +struct Test: CustomStringConvertible { var id = UUID() - private var function: StaticString + var fileID: String + var function: String - init(function: StaticString = #function) { + init(fileID: String = #fileID, function: String = #function) { + self.fileID = fileID self.function = function - NSLog("Created test \(id) for function \(function)") + NSLog("Created test \(id) for function \(function) in file \(fileID)") + } + + var description: String { + return "Test(id: \(id), fileID: \(fileID), function: \(function))" } } diff --git a/Test/Test Utilities/TestUtilities.swift b/Test/Test Utilities/TestUtilities.swift index e0523441d..27cf14357 100644 --- a/Test/Test Utilities/TestUtilities.swift +++ b/Test/Test Utilities/TestUtilities.swift @@ -23,6 +23,34 @@ class AblyTestsConfiguration: NSObject, XCTestObservation { performedPreFirstTestCaseSetup = true } } + + func testSuiteDidFinish(_ testSuite: XCTestSuite) { + let activeQueueIdentities = AblyTests.QueueIdentity.active + + if activeQueueIdentities.isEmpty { + print("No active queue identities.") + } else { + print("\(activeQueueIdentities.count) active queue \(activeQueueIdentities.count == 1 ? "identity" : "identities"):") + + let activeQueueIdentitiesGroupedByFileID = Dictionary(grouping: activeQueueIdentities, by: \.test.fileID) + let sortedFileIDs = activeQueueIdentitiesGroupedByFileID.keys.sorted() + for fileID in sortedFileIDs { + print("\t\(fileID):") + let activeQueueIdentitiesForFileID = activeQueueIdentitiesGroupedByFileID[fileID]! + + let activeQueueIdentitiesForFileIDGroupedByFunction = Dictionary(grouping: activeQueueIdentitiesForFileID, by: \.test.function) + let sortedFunctions = activeQueueIdentitiesForFileIDGroupedByFunction.keys.sorted() + for function in sortedFunctions { + print("\t\t\(function):") + let activeQueueIdentitiesForFunction = activeQueueIdentitiesForFileIDGroupedByFunction[function]! + + for queueIdentity in activeQueueIdentitiesForFunction { + print("\t\t\t\(queueIdentity.label)") + } + } + } + } + } private func preFirstTestCaseSetup() { // This is code that, when we were using the Quick testing @@ -102,30 +130,60 @@ class AblyTests { static var testApplication: [String: Any]? - class QueueIdentity { + class QueueIdentity: CustomStringConvertible, Hashable { let label: String + let test: Test + + private static let semaphore = DispatchSemaphore(value: 1) + private static var _active: Set = [] - init(label: String) { + static var active: Set { + semaphore.wait() + let active = _active + semaphore.signal() + return active + } + + init(label: String, test: Test) { self.label = label + self.test = test + Self.semaphore.wait() + Self._active.insert(self) + Self.semaphore.signal() NSLog("Created QueueIdentity \(label)") } deinit { + Self.semaphore.wait() + Self._active.remove(self) + Self.semaphore.signal() NSLog("deinit QueueIdentity \(label)") } + + var description: String { + return "QueueIdentity(label: \(label), test: \(test))" + } + + static func == (lhs: AblyTests.QueueIdentity, rhs: AblyTests.QueueIdentity) -> Bool { + return ObjectIdentifier(lhs) == ObjectIdentifier(rhs) + } + + func hash(into hasher: inout Hasher) { + hasher.combine(ObjectIdentifier(self)) + } } static let queueIdentityKey = DispatchSpecificKey() static func createInternalQueue(for test: Test) -> DispatchQueue { let queue = DispatchQueue(label: "io.ably.tests.\(test.id).\(UUID().uuidString)", qos: .userInitiated) - queue.setSpecific(key: queueIdentityKey, value: QueueIdentity(label: queue.label)) + queue.setSpecific(key: queueIdentityKey, value: QueueIdentity(label: queue.label, test: test)) return queue } static func createUserQueue(for test: Test) -> DispatchQueue { let queue = DispatchQueue(label: "io.ably.tests.callbacks.\(test.id).\(UUID().uuidString)", qos: .userInitiated) - queue.setSpecific(key: queueIdentityKey, value: QueueIdentity(label: queue.label)) + queue.setSpecific(key: queueIdentityKey, value: QueueIdentity(label: queue.label, test: test)) return queue }