Skip to content

Commit 11e469d

Browse files
Create a unique identifier for a test method execution
This is currently being passed to - uniqueChannelName(…) — to include the execution ID in the channel name - createUserQueue(…) — to include the execution ID in the queue label - commonAppSetup(…) — to include the execution ID in the channelNamePrefix and the intention is that it will be a unique identifier eventually used to link together everything to do with a given test method execution — e.g. for tagging log messages, labelling dispatch queues etc.
1 parent 21c3cdd commit 11e469d

20 files changed

+2125
-1350
lines changed

Ably.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,9 @@
268268
21881E7A283BD08300CFD9E2 /* GCDTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5A22171266F526600C87C42 /* GCDTests.swift */; };
269269
21881E7B283BD0DF00CFD9E2 /* StringifiableTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D520C4DD2680A1E3000012B2 /* StringifiableTests.swift */; };
270270
21881E7C283BD0E100CFD9E2 /* StringifiableTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D520C4DD2680A1E3000012B2 /* StringifiableTests.swift */; };
271+
21FD9F272A015BE400216482 /* Test.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21FD9F262A015BE400216482 /* Test.swift */; };
272+
21FD9F282A015BE400216482 /* Test.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21FD9F262A015BE400216482 /* Test.swift */; };
273+
21FD9F292A015BE400216482 /* Test.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21FD9F262A015BE400216482 /* Test.swift */; };
271274
560579D924AF1BA900A4D03D /* ARTDefaultTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 560579D824AF1BA900A4D03D /* ARTDefaultTests.swift */; };
272275
560579DA24AF1BA900A4D03D /* ARTDefaultTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 560579D824AF1BA900A4D03D /* ARTDefaultTests.swift */; };
273276
560579DB24AF1BA900A4D03D /* ARTDefaultTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 560579D824AF1BA900A4D03D /* ARTDefaultTests.swift */; };
@@ -1219,6 +1222,7 @@
12191222
21DCDA8229F818630073A211 /* Ably-iOS.xctestplan */ = {isa = PBXFileReference; lastKnownFileType = file; name = "Ably-iOS.xctestplan"; path = "Test/Ably-iOS.xctestplan"; sourceTree = SOURCE_ROOT; };
12201223
21DCDA8329F81B350073A211 /* Ably-macOS.xctestplan */ = {isa = PBXFileReference; lastKnownFileType = text; path = "Ably-macOS.xctestplan"; sourceTree = "<group>"; };
12211224
21DCDA8429F81B550073A211 /* Ably-tvOS.xctestplan */ = {isa = PBXFileReference; lastKnownFileType = text; path = "Ably-tvOS.xctestplan"; sourceTree = "<group>"; };
1225+
21FD9F262A015BE400216482 /* Test.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Test.swift; sourceTree = "<group>"; };
12221226
560579D824AF1BA900A4D03D /* ARTDefaultTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ARTDefaultTests.swift; sourceTree = "<group>"; };
12231227
56190953238C3D3200A862A6 /* CryptoTest.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = CryptoTest.m; sourceTree = "<group>"; };
12241228
841134772722205400CFA837 /* ARTArchiveTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ARTArchiveTests.m; sourceTree = "<group>"; };
@@ -1733,6 +1737,7 @@
17331737
2147F03429E5872C0071CB94 /* MockInternalLogCore.swift */,
17341738
D50D86E829E9444600EA72EA /* JSON.swift */,
17351739
210F67B029E9DB62007B9345 /* TestProxyTransportFactory.swift */,
1740+
21FD9F262A015BE400216482 /* Test.swift */,
17361741
);
17371742
path = "Test Utilities";
17381743
sourceTree = "<group>";
@@ -3040,6 +3045,7 @@
30403045
2132C21629D20F69000C4355 /* ResumeRequestResponseTests.swift in Sources */,
30413046
EB7913A81C6E54C3000ABF9B /* CryptoTests.swift in Sources */,
30423047
2132C22229D233EB000C4355 /* DefaultErrorCheckerTests.swift in Sources */,
3048+
21FD9F272A015BE400216482 /* Test.swift in Sources */,
30433049
D777EEE820650ADF002EBA03 /* PushChannelTests.swift in Sources */,
30443050
217FCF3F29D626E4006E5F2D /* StaticJitterCoefficients.swift in Sources */,
30453051
D746AE2D1BBB625E003ECEF8 /* RestClientChannelsTests.swift in Sources */,
@@ -3229,6 +3235,7 @@
32293235
56190955238C3D3200A862A6 /* CryptoTest.m in Sources */,
32303236
D7093C1D219E466600723F17 /* AuthTests.swift in Sources */,
32313237
21276CC329F00BAA00107B5F /* ContinuousClockTests.swift in Sources */,
3238+
21FD9F282A015BE400216482 /* Test.swift in Sources */,
32323239
D7093C25219E466E00723F17 /* RealtimeClientConnectionTests.swift in Sources */,
32333240
217FCF4029D626E4006E5F2D /* StaticJitterCoefficients.swift in Sources */,
32343241
);
@@ -3285,6 +3292,7 @@
32853292
56190956238C3D3200A862A6 /* CryptoTest.m in Sources */,
32863293
D7093C7A219EE26400723F17 /* RestPaginatedTests.swift in Sources */,
32873294
21276CC429F00BAA00107B5F /* ContinuousClockTests.swift in Sources */,
3295+
21FD9F292A015BE400216482 /* Test.swift in Sources */,
32883296
D7093C81219EE26400723F17 /* UtilitiesTests.swift in Sources */,
32893297
217FCF4129D626E4006E5F2D /* StaticJitterCoefficients.swift in Sources */,
32903298
);

Test/Test Utilities/Test.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
Represents an execution of a test case method.
3+
*/
4+
struct Test {
5+
var id = UUID()
6+
private var function: StaticString
7+
8+
init(function: StaticString = #function) {
9+
self.function = function
10+
NSLog("Created test \(id) for function \(function)")
11+
}
12+
}

Test/Test Utilities/TestUtilities.swift

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ let testTimeout = DispatchTimeInterval.seconds(20)
5050
let testResourcesPath = "ably-common/test-resources/"
5151
let echoServerAddress = "https://echo.ably.io/createJWT"
5252

53-
func uniqueChannelName(prefix: String = "",
54-
testIdentifier: String = #function,
53+
func uniqueChannelName(for test: Test,
54+
prefix: String = "",
5555
timestamp: TimeInterval = Date.timeIntervalSinceReferenceDate) -> String {
5656
let platform: String
5757
#if targetEnvironment(macCatalyst)
@@ -67,7 +67,7 @@ func uniqueChannelName(prefix: String = "",
6767
#else
6868
platform = "Unknown"
6969
#endif
70-
return "\(prefix)-\(platform)-\(testIdentifier.replacingOccurrences(of: "()", with: ""))-\(timestamp)-\(NSUUID().uuidString)"
70+
return "\(prefix)-\(platform)-\(test.id)-\(timestamp)-\(NSUUID().uuidString)"
7171
}
7272

7373
/// Common test utilities.
@@ -114,8 +114,8 @@ class AblyTests {
114114
return queue
115115
}()
116116

117-
static func createUserQueue() -> DispatchQueue {
118-
let queue = DispatchQueue(label: "io.ably.tests.callbacks.\(UUID().uuidString)", qos: .userInitiated)
117+
static func createUserQueue(for test: Test) -> DispatchQueue {
118+
let queue = DispatchQueue(label: "io.ably.tests.callbacks.\(test.id).\(UUID().uuidString)", qos: .userInitiated)
119119
queue.setSpecific(key: queueIdentityKey, value: QueueIdentity(label: queue.label))
120120
return queue
121121
}
@@ -124,9 +124,9 @@ class AblyTests {
124124
return DispatchQueue.getSpecific(key: queueIdentityKey)?.label
125125
}
126126

127-
class func commonAppSetup(debug: Bool = false, forceNewApp: Bool = false) throws -> ARTClientOptions {
128-
let options = try AblyTests.clientOptions(debug: debug)
129-
options.testOptions.channelNamePrefix = "test-\(UUID().uuidString)"
127+
class func commonAppSetup(for test: Test, debug: Bool = false, forceNewApp: Bool = false) throws -> ARTClientOptions {
128+
let options = try AblyTests.clientOptions(for: test, debug: debug)
129+
options.testOptions.channelNamePrefix = "test-\(test.id)-\(UUID().uuidString)"
130130

131131
if forceNewApp {
132132
testApplication = nil
@@ -161,7 +161,7 @@ class AblyTests {
161161
return options
162162
}
163163

164-
class func clientOptions(debug: Bool = false, key: String? = nil, requestToken: Bool = false) throws -> ARTClientOptions {
164+
class func clientOptions(for test: Test, debug: Bool = false, key: String? = nil, requestToken: Bool = false) throws -> ARTClientOptions {
165165
let options = ARTClientOptions()
166166
options.environment = getEnvironment()
167167
if debug {
@@ -171,7 +171,7 @@ class AblyTests {
171171
options.key = key
172172
}
173173
if requestToken {
174-
options.token = try getTestToken()
174+
options.token = try getTestToken(for: test)
175175
}
176176
options.dispatchQueue = DispatchQueue.main
177177
options.internalDispatchQueue = queue
@@ -518,22 +518,22 @@ class PublishTestMessage {
518518
}
519519

520520
/// Access Token
521-
func getTestToken(key: String? = nil, clientId: String? = nil, capability: String? = nil, ttl: TimeInterval? = nil, file: FileString = #file, line: UInt = #line) throws -> String {
522-
return try getTestTokenDetails(key: key, clientId: clientId, capability: capability, ttl: ttl, file: file, line: line).token
521+
func getTestToken(for test: Test, key: String? = nil, clientId: String? = nil, capability: String? = nil, ttl: TimeInterval? = nil, file: FileString = #file, line: UInt = #line) throws -> String {
522+
return try getTestTokenDetails(for: test, key: key, clientId: clientId, capability: capability, ttl: ttl, file: file, line: line).token
523523
}
524524

525-
func getTestToken(key: String? = nil, clientId: String? = nil, capability: String? = nil, ttl: TimeInterval? = nil, file: FileString = #file, line: UInt = #line, completion: @escaping (Swift.Result<String, Error>) -> Void) {
526-
getTestTokenDetails(key: key, clientId: clientId, capability: capability, ttl: ttl) { result in
525+
func getTestToken(for test: Test, key: String? = nil, clientId: String? = nil, capability: String? = nil, ttl: TimeInterval? = nil, file: FileString = #file, line: UInt = #line, completion: @escaping (Swift.Result<String, Error>) -> Void) {
526+
getTestTokenDetails(for: test, key: key, clientId: clientId, capability: capability, ttl: ttl) { result in
527527
completion(result.map(\.token))
528528
}
529529
}
530530

531531
/// Access TokenDetails
532-
func getTestTokenDetails(key: String? = nil, clientId: String? = nil, capability: String? = nil, ttl: TimeInterval? = nil, queryTime: Bool? = nil, completion: @escaping (Swift.Result<ARTTokenDetails, Error>) -> Void) {
532+
func getTestTokenDetails(for test: Test, key: String? = nil, clientId: String? = nil, capability: String? = nil, ttl: TimeInterval? = nil, queryTime: Bool? = nil, completion: @escaping (Swift.Result<ARTTokenDetails, Error>) -> Void) {
533533
let options: ARTClientOptions
534534
if let key = key {
535535
do {
536-
options = try AblyTests.clientOptions()
536+
options = try AblyTests.clientOptions(for: test)
537537
} catch {
538538
completion(.failure(error))
539539
return
@@ -542,7 +542,7 @@ func getTestTokenDetails(key: String? = nil, clientId: String? = nil, capability
542542
}
543543
else {
544544
do {
545-
options = try AblyTests.commonAppSetup()
545+
options = try AblyTests.commonAppSetup(for: test)
546546
} catch {
547547
completion(.failure(error))
548548
return
@@ -580,8 +580,8 @@ func getTestTokenDetails(key: String? = nil, clientId: String? = nil, capability
580580
}
581581
}
582582

583-
func getTestTokenDetails(key: String? = nil, clientId: String? = nil, capability: String? = nil, ttl: TimeInterval? = nil, queryTime: Bool? = nil, completion: @escaping (ARTTokenDetails?, Error?) -> Void) {
584-
getTestTokenDetails(key: key, clientId: clientId, capability: capability, ttl: ttl, queryTime: queryTime) { result in
583+
func getTestTokenDetails(for test: Test, key: String? = nil, clientId: String? = nil, capability: String? = nil, ttl: TimeInterval? = nil, queryTime: Bool? = nil, completion: @escaping (ARTTokenDetails?, Error?) -> Void) {
584+
getTestTokenDetails(for: test, key: key, clientId: clientId, capability: capability, ttl: ttl, queryTime: queryTime) { result in
585585
switch result {
586586
case .success(let tokenDetails):
587587
completion(tokenDetails, nil)
@@ -591,18 +591,18 @@ func getTestTokenDetails(key: String? = nil, clientId: String? = nil, capability
591591
}
592592
}
593593

594-
func getTestTokenDetails(key: String? = nil, clientId: String? = nil, capability: String? = nil, ttl: TimeInterval? = nil, queryTime: Bool? = nil, file: FileString = #file, line: UInt = #line) throws -> ARTTokenDetails {
594+
func getTestTokenDetails(for test: Test, key: String? = nil, clientId: String? = nil, capability: String? = nil, ttl: TimeInterval? = nil, queryTime: Bool? = nil, file: FileString = #file, line: UInt = #line) throws -> ARTTokenDetails {
595595
let result = try AblyTests.waitFor(timeout: testTimeout, file: file, line: line) { value in
596-
getTestTokenDetails(key: key, clientId: clientId, capability: capability, ttl: ttl, queryTime: queryTime) { result in
596+
getTestTokenDetails(for: test, key: key, clientId: clientId, capability: capability, ttl: ttl, queryTime: queryTime) { result in
597597
value(result)
598598
}
599599
}
600600

601601
return try result.get()
602602
}
603603

604-
func getJWTToken(invalid: Bool = false, expiresIn: Int = 3600, clientId: String = "testClientIDiOS", capability: String = "{\"*\":[\"*\"]}", jwtType: String = "", encrypted: Int = 0) throws -> String? {
605-
let options = try AblyTests.commonAppSetup()
604+
func getJWTToken(for test: Test, invalid: Bool = false, expiresIn: Int = 3600, clientId: String = "testClientIDiOS", capability: String = "{\"*\":[\"*\"]}", jwtType: String = "", encrypted: Int = 0) throws -> String? {
605+
let options = try AblyTests.commonAppSetup(for: test)
606606
guard let components = options.key?.components(separatedBy: ":"), let keyName = components.first, var keySecret = components.last else {
607607
fail("Invalid API key: \(options.key ?? "nil")")
608608
return nil
@@ -628,8 +628,8 @@ func getJWTToken(invalid: Bool = false, expiresIn: Int = 3600, clientId: String
628628
return String(data: responseData, encoding: String.Encoding.utf8)
629629
}
630630

631-
func getKeys() throws -> Dictionary<String, String> {
632-
let options = try AblyTests.commonAppSetup()
631+
func getKeys(for test: Test) throws -> Dictionary<String, String> {
632+
let options = try AblyTests.commonAppSetup(for: test)
633633
guard let components = options.key?.components(separatedBy: ":"), let keyName = components.first, let keySecret = components.last else {
634634
fatalError("Invalid API key)")
635635
}

0 commit comments

Comments
 (0)