Skip to content

Commit 154911e

Browse files
WIP log the active QueueIdentity objects at end of test suite
There are loads — why?
1 parent 2c1d12d commit 154911e

File tree

2 files changed

+72
-8
lines changed

2 files changed

+72
-8
lines changed

Test/Test Utilities/Test.swift

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
/**
22
Represents an execution of a test case method.
33
*/
4-
struct Test {
4+
struct Test: CustomStringConvertible {
55
var id = UUID()
6-
private var function: StaticString
6+
var fileID: String
7+
var function: String
78

8-
init(function: StaticString = #function) {
9+
init(fileID: String = #fileID, function: String = #function) {
10+
self.fileID = fileID
911
self.function = function
10-
NSLog("Created test \(id) for function \(function)")
12+
NSLog("Created test \(id) for function \(function) in file \(fileID)")
13+
}
14+
15+
var description: String {
16+
return "Test(id: \(id), fileID: \(fileID), function: \(function))"
1117
}
1218

1319
func uniqueChannelName(prefix: String = "",

Test/Test Utilities/TestUtilities.swift

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,34 @@ class AblyTestsConfiguration: NSObject, XCTestObservation {
2323
performedPreFirstTestCaseSetup = true
2424
}
2525
}
26+
27+
func testSuiteDidFinish(_ testSuite: XCTestSuite) {
28+
let activeQueueIdentities = AblyTests.QueueIdentity.active
29+
30+
if activeQueueIdentities.isEmpty {
31+
print("No active queue identities.")
32+
} else {
33+
print("\(activeQueueIdentities.count) active queue \(activeQueueIdentities.count == 1 ? "identity" : "identities"):")
34+
35+
let activeQueueIdentitiesGroupedByFileID = Dictionary(grouping: activeQueueIdentities, by: \.test.fileID)
36+
let sortedFileIDs = activeQueueIdentitiesGroupedByFileID.keys.sorted()
37+
for fileID in sortedFileIDs {
38+
print("\t\(fileID):")
39+
let activeQueueIdentitiesForFileID = activeQueueIdentitiesGroupedByFileID[fileID]!
40+
41+
let activeQueueIdentitiesForFileIDGroupedByFunction = Dictionary(grouping: activeQueueIdentitiesForFileID, by: \.test.function)
42+
let sortedFunctions = activeQueueIdentitiesForFileIDGroupedByFunction.keys.sorted()
43+
for function in sortedFunctions {
44+
print("\t\t\(function):")
45+
let activeQueueIdentitiesForFunction = activeQueueIdentitiesForFileIDGroupedByFunction[function]!
46+
47+
for queueIdentity in activeQueueIdentitiesForFunction {
48+
print("\t\t\t\(queueIdentity.label)")
49+
}
50+
}
51+
}
52+
}
53+
}
2654

2755
private func preFirstTestCaseSetup() {
2856
// This is code that, when we were using the Quick testing
@@ -82,30 +110,60 @@ class AblyTests {
82110

83111
static var testApplication: [String: Any]?
84112

85-
class QueueIdentity {
113+
class QueueIdentity: CustomStringConvertible, Hashable {
86114
let label: String
115+
let test: Test
116+
117+
private static let semaphore = DispatchSemaphore(value: 1)
118+
private static var _active: Set<QueueIdentity> = []
87119

88-
init(label: String) {
120+
static var active: Set<QueueIdentity> {
121+
semaphore.wait()
122+
let active = _active
123+
semaphore.signal()
124+
return active
125+
}
126+
127+
init(label: String, test: Test) {
89128
self.label = label
129+
self.test = test
130+
Self.semaphore.wait()
131+
Self._active.insert(self)
132+
Self.semaphore.signal()
90133
NSLog("Created QueueIdentity \(label)")
91134
}
92135

93136
deinit {
137+
Self.semaphore.wait()
138+
Self._active.remove(self)
139+
Self.semaphore.signal()
94140
NSLog("deinit QueueIdentity \(label)")
95141
}
142+
143+
var description: String {
144+
return "QueueIdentity(label: \(label), test: \(test))"
145+
}
146+
147+
static func == (lhs: AblyTests.QueueIdentity, rhs: AblyTests.QueueIdentity) -> Bool {
148+
return ObjectIdentifier(lhs) == ObjectIdentifier(rhs)
149+
}
150+
151+
func hash(into hasher: inout Hasher) {
152+
hasher.combine(ObjectIdentifier(self))
153+
}
96154
}
97155

98156
static let queueIdentityKey = DispatchSpecificKey<QueueIdentity>()
99157

100158
static func createInternalQueue(for test: Test) -> DispatchQueue {
101159
let queue = DispatchQueue(label: "io.ably.tests.\(test.id).\(UUID().uuidString)", qos: .userInitiated)
102-
queue.setSpecific(key: queueIdentityKey, value: QueueIdentity(label: queue.label))
160+
queue.setSpecific(key: queueIdentityKey, value: QueueIdentity(label: queue.label, test: test))
103161
return queue
104162
}
105163

106164
static func createUserQueue(for test: Test) -> DispatchQueue {
107165
let queue = DispatchQueue(label: "io.ably.tests.callbacks.\(test.id).\(UUID().uuidString)", qos: .userInitiated)
108-
queue.setSpecific(key: queueIdentityKey, value: QueueIdentity(label: queue.label))
166+
queue.setSpecific(key: queueIdentityKey, value: QueueIdentity(label: queue.label, test: test))
109167
return queue
110168
}
111169

0 commit comments

Comments
 (0)