Skip to content

Commit

Permalink
WIP log the active QueueIdentity objects at end of test suite
Browse files Browse the repository at this point in the history
There are loads — why?
  • Loading branch information
lawrence-forooghian committed May 3, 2023
1 parent eddfbc6 commit 0d7667f
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 8 deletions.
14 changes: 10 additions & 4 deletions Test/Test Utilities/Test.swift
Original file line number Diff line number Diff line change
@@ -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))"
}
}
66 changes: 62 additions & 4 deletions Test/Test Utilities/TestUtilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<QueueIdentity> = []

init(label: String) {
static var active: Set<QueueIdentity> {
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<QueueIdentity>()

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
}

Expand Down

0 comments on commit 0d7667f

Please sign in to comment.