Skip to content

Commit

Permalink
Move uniqueChannelName function to method on Test
Browse files Browse the repository at this point in the history
A bit more readable.
  • Loading branch information
lawrence-forooghian committed May 4, 2023
1 parent b426bc1 commit 4915315
Show file tree
Hide file tree
Showing 17 changed files with 529 additions and 530 deletions.
19 changes: 19 additions & 0 deletions Test/Test Utilities/Test.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,23 @@ struct Test {
self.function = function
NSLog("Created test \(id) for function \(function)")
}

func uniqueChannelName(prefix: String = "",
timestamp: TimeInterval = Date.timeIntervalSinceReferenceDate) -> String {
let platform: String
#if targetEnvironment(macCatalyst)
platform = "macCatalyst"
#elseif os(OSX)
platform = "OSX"
#elseif os(iOS)
platform = "iOS"
#elseif os(tvOS)
platform = "tvOS"
#elseif os(watchOS)
platform = "watchOS"
#else
platform = "Unknown"
#endif
return "\(prefix)-\(platform)-\(id)-\(timestamp)-\(NSUUID().uuidString)"
}
}
20 changes: 0 additions & 20 deletions Test/Test Utilities/TestUtilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,26 +50,6 @@ let testTimeout = DispatchTimeInterval.seconds(20)
let testResourcesPath = "ably-common/test-resources/"
let echoServerAddress = "https://echo.ably.io/createJWT"

func uniqueChannelName(for test: Test,
prefix: String = "",
timestamp: TimeInterval = Date.timeIntervalSinceReferenceDate) -> String {
let platform: String
#if targetEnvironment(macCatalyst)
platform = "macCatalyst"
#elseif os(OSX)
platform = "OSX"
#elseif os(iOS)
platform = "iOS"
#elseif os(tvOS)
platform = "tvOS"
#elseif os(watchOS)
platform = "watchOS"
#else
platform = "Unknown"
#endif
return "\(prefix)-\(platform)-\(test.id)-\(timestamp)-\(NSUUID().uuidString)"
}

/// Common test utilities.
class AblyTests {
enum Error: Swift.Error {
Expand Down
46 changes: 23 additions & 23 deletions Test/Tests/AuthTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class AuthTests: XCTestCase {
client.internal.httpExecutor = testHTTPExecutor

waitUntil(timeout: testTimeout) { done in
client.channels.get(uniqueChannelName(for: test)).publish(nil, data: "message") { _ in
client.channels.get(test.uniqueChannelName()).publish(nil, data: "message") { _ in
done()
}
}
Expand Down Expand Up @@ -127,7 +127,7 @@ class AuthTests: XCTestCase {
clientHTTP.internal.httpExecutor = testHTTPExecutor

waitUntil(timeout: testTimeout) { done in
clientHTTP.channels.get(uniqueChannelName(for: test)).publish(nil, data: "message") { _ in
clientHTTP.channels.get(test.uniqueChannelName()).publish(nil, data: "message") { _ in
done()
}
}
Expand All @@ -146,7 +146,7 @@ class AuthTests: XCTestCase {
clientHTTPS.internal.httpExecutor = testHTTPExecutor

waitUntil(timeout: testTimeout) { done in
clientHTTPS.channels.get(uniqueChannelName(for: test)).publish(nil, data: "message") { _ in
clientHTTPS.channels.get(test.uniqueChannelName()).publish(nil, data: "message") { _ in
done()
}
}
Expand All @@ -168,7 +168,7 @@ class AuthTests: XCTestCase {
client.internal.httpExecutor = testHTTPExecutor

waitUntil(timeout: testTimeout) { done in
client.channels.get(uniqueChannelName(for: test)).publish(nil, data: "message") { _ in
client.channels.get(test.uniqueChannelName()).publish(nil, data: "message") { _ in
done()
}
}
Expand Down Expand Up @@ -244,7 +244,7 @@ class AuthTests: XCTestCase {
let testHTTPExecutor = TestProxyHTTPExecutor(logger: .init(clientOptions: options))
rest.internal.httpExecutor = testHTTPExecutor

let channel = rest.channels.get(uniqueChannelName(for: test))
let channel = rest.channels.get(test.uniqueChannelName())

testHTTPExecutor.simulateIncomingServerErrorOnNextRequest(ARTErrorCode.tokenRevoked.intValue, description: "token revoked")
waitUntil(timeout: testTimeout) { done in
Expand Down Expand Up @@ -278,7 +278,7 @@ class AuthTests: XCTestCase {
XCTAssertNil(realtime.internal.options.authCallback)
XCTAssertNil(realtime.internal.options.authUrl)

let channel = realtime.channels.get(uniqueChannelName(for: test))
let channel = realtime.channels.get(test.uniqueChannelName())

waitUntil(timeout: testTimeout.multiplied(by: 2)) { done in
realtime.connect()
Expand Down Expand Up @@ -310,7 +310,7 @@ class AuthTests: XCTestCase {
let testHTTPExecutor = TestProxyHTTPExecutor(logger: .init(clientOptions: options))
rest.internal.httpExecutor = testHTTPExecutor

let channel = rest.channels.get(uniqueChannelName(for: test))
let channel = rest.channels.get(test.uniqueChannelName())

testHTTPExecutor.simulateIncomingServerErrorOnNextRequest(ARTErrorCode.tokenRevoked.intValue, description: "token revoked")

Expand Down Expand Up @@ -338,7 +338,7 @@ class AuthTests: XCTestCase {
let testHTTPExecutor = TestProxyHTTPExecutor(logger: .init(clientOptions: options))
rest.internal.httpExecutor = testHTTPExecutor

let channel = rest.channels.get(uniqueChannelName(for: test))
let channel = rest.channels.get(test.uniqueChannelName())

testHTTPExecutor.setListenerAfterRequest { _ in
testHTTPExecutor.simulateIncomingServerErrorOnNextRequest(ARTErrorCode.tokenRevoked.intValue, description: "token revoked")
Expand Down Expand Up @@ -459,7 +459,7 @@ class AuthTests: XCTestCase {

rest.internal.httpExecutor = proxyHTTPExecutor
waitUntil(timeout: testTimeout) { done in
rest.channels.get(uniqueChannelName(for: test)).history { _, error in
rest.channels.get(test.uniqueChannelName()).history { _, error in
guard let error = error else {
fail("Error is nil"); done(); return
}
Expand Down Expand Up @@ -500,7 +500,7 @@ class AuthTests: XCTestCase {
}

waitUntil(timeout: testTimeout) { done in
rest.channels.get(uniqueChannelName(for: test)).history { _, error in
rest.channels.get(test.uniqueChannelName()).history { _, error in
guard let error = error else {
fail("Error is nil"); done(); return
}
Expand Down Expand Up @@ -926,7 +926,7 @@ class AuthTests: XCTestCase {
let state = stateChange.current
let error = stateChange.reason
if state == .connected, error == nil {
let currentChannel = client.channels.get(uniqueChannelName(for: test))
let currentChannel = client.channels.get(test.uniqueChannelName())
currentChannel.subscribe { _ in
done()
}
Expand Down Expand Up @@ -1125,7 +1125,7 @@ class AuthTests: XCTestCase {
let rest = ARTRest(options: options)
let testHTTPExecutor = TestProxyHTTPExecutor(logger: .init(clientOptions: options))
rest.internal.httpExecutor = testHTTPExecutor
let channel = rest.channels.get(uniqueChannelName(for: test))
let channel = rest.channels.get(test.uniqueChannelName())
waitUntil(timeout: testTimeout) { done in
channel.publish("foo", data: nil) { error in
XCTAssertNil(error)
Expand Down Expand Up @@ -1153,7 +1153,7 @@ class AuthTests: XCTestCase {
client.internal.httpExecutor = testHTTPExecutor

waitUntil(timeout: testTimeout) { done in
client.channels.get(uniqueChannelName(for: test)).publish(nil, data: "message") { error in
client.channels.get(test.uniqueChannelName()).publish(nil, data: "message") { error in
if let e = error {
XCTFail(e.localizedDescription)
}
Expand Down Expand Up @@ -1193,7 +1193,7 @@ class AuthTests: XCTestCase {
}
options.defaultTokenParams = ARTTokenParams(clientId: "tester")
let client = ARTRest(options: options)
let channel = client.channels.get(uniqueChannelName(for: test))
let channel = client.channels.get(test.uniqueChannelName())

XCTAssertEqual(client.auth.clientId, "john")
waitUntil(timeout: testTimeout) { done in
Expand Down Expand Up @@ -1870,7 +1870,7 @@ class AuthTests: XCTestCase {
let rest = ARTRest(options: options)
let testHTTPExecutor = TestProxyHTTPExecutor(logger: .init(clientOptions: options))
rest.internal.httpExecutor = testHTTPExecutor
let channel = rest.channels.get(uniqueChannelName(for: test))
let channel = rest.channels.get(test.uniqueChannelName())

waitUntil(timeout: testTimeout) { done in
let message = ARTMessage(name: nil, data: "message without an explicit clientId")
Expand Down Expand Up @@ -1903,7 +1903,7 @@ class AuthTests: XCTestCase {
let options = try AblyTests.commonAppSetup(for: test)
options.token = try getTestToken(for: test, clientId: nil)
let rest = ARTRest(options: options)
let channel = rest.channels.get(uniqueChannelName(for: test))
let channel = rest.channels.get(test.uniqueChannelName())

waitUntil(timeout: testTimeout) { done in
let message = ARTMessage(name: nil, data: "message with an explicit clientId", clientId: "john")
Expand Down Expand Up @@ -1933,7 +1933,7 @@ class AuthTests: XCTestCase {

let testHTTPExecutor = TestProxyHTTPExecutor(logger: .init(clientOptions: options))
rest.internal.httpExecutor = testHTTPExecutor
let channel = rest.channels.get(uniqueChannelName(for: test))
let channel = rest.channels.get(test.uniqueChannelName())

waitUntil(timeout: testTimeout) { done in
let message = ARTMessage(name: nil, data: "no client")
Expand Down Expand Up @@ -1967,7 +1967,7 @@ class AuthTests: XCTestCase {
// Request a token with a wildcard '*' value clientId
options.token = try getTestToken(for: test, clientId: "*")
let rest = ARTRest(options: options)
let channel = rest.channels.get(uniqueChannelName(for: test))
let channel = rest.channels.get(test.uniqueChannelName())

waitUntil(timeout: testTimeout) { done in
let message = ARTMessage(name: nil, data: "message with an explicit clientId", clientId: "john")
Expand Down Expand Up @@ -2518,7 +2518,7 @@ class AuthTests: XCTestCase {
let options = try AblyTests.commonAppSetup(for: test)
options.useTokenAuth = true
let rest = ARTRest(options: options)
let channel = rest.channels.get(uniqueChannelName(for: test))
let channel = rest.channels.get(test.uniqueChannelName())

waitUntil(timeout: testTimeout) { done in
channel.publish(nil, data: "first check") { error in
Expand Down Expand Up @@ -2584,7 +2584,7 @@ class AuthTests: XCTestCase {
XCTAssertNotEqual(tokenDetails.token, testToken)
XCTAssertEqual(rest.auth.internal.method, ARTAuthMethod.token)

publishTestMessage(rest, channelName: uniqueChannelName(for: test), completion: { error in
publishTestMessage(rest, channelName: test.uniqueChannelName(), completion: { error in
XCTAssertNil(error)
XCTAssertEqual(rest.auth.internal.method, ARTAuthMethod.token)
XCTAssertEqual(rest.auth.tokenDetails?.token, tokenDetails.token)
Expand All @@ -2609,7 +2609,7 @@ class AuthTests: XCTestCase {
XCTAssertNotNil(tokenDetails.token)
XCTAssertEqual(rest.auth.internal.method, ARTAuthMethod.token)

publishTestMessage(rest, channelName: uniqueChannelName(for: test), completion: { error in
publishTestMessage(rest, channelName: test.uniqueChannelName(), completion: { error in
XCTAssertNil(error)
XCTAssertEqual(rest.auth.internal.method, ARTAuthMethod.token)
XCTAssertEqual(rest.auth.tokenDetails?.token, tokenDetails.token)
Expand Down Expand Up @@ -3837,7 +3837,7 @@ class AuthTests: XCTestCase {
options.token = initialToken
let realtime = ARTRealtime(options: options)
defer { realtime.dispose(); realtime.close() }
let channel = realtime.channels.get(uniqueChannelName(for: test))
let channel = realtime.channels.get(test.uniqueChannelName())

waitUntil(timeout: testTimeout) { done in
channel.attach { error in
Expand Down Expand Up @@ -4451,7 +4451,7 @@ class AuthTests: XCTestCase {
var originalTokenRequest: ARTTokenRequest!
let tmpRest = ARTRest(options: try AblyTests.commonAppSetup(for: test))

let channelName = uniqueChannelName(for: test)
let channelName = test.uniqueChannelName()
waitUntil(timeout: testTimeout) { done in
let tokenParams = ARTTokenParams()
tokenParams.clientId = "john"
Expand Down
6 changes: 3 additions & 3 deletions Test/Tests/DeltaCodecTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class DeltaCodecTests: XCTestCase {
"delta": "vcdiff",
]

let channel = client.channels.get(uniqueChannelName(for: test), options: channelOptions)
let channel = client.channels.get(test.uniqueChannelName(), options: channelOptions)

waitUntil(timeout: testTimeout) { done in
channel.attach { error in
Expand Down Expand Up @@ -81,7 +81,7 @@ class DeltaCodecTests: XCTestCase {
defer { client.dispose(); client.close() }
let channelOptions = ARTRealtimeChannelOptions()
channelOptions.params = ["delta": "vcdiff"]
let channel = client.channels.get(uniqueChannelName(for: test), options: channelOptions)
let channel = client.channels.get(test.uniqueChannelName(), options: channelOptions)

waitUntil(timeout: testTimeout) { done in
channel.attach { error in
Expand Down Expand Up @@ -141,7 +141,7 @@ class DeltaCodecTests: XCTestCase {
defer { client.dispose(); client.close() }
let channelOptions = ARTRealtimeChannelOptions()
channelOptions.params = ["delta": "vcdiff"]
let channel = client.channels.get(uniqueChannelName(for: test), options: channelOptions)
let channel = client.channels.get(test.uniqueChannelName(), options: channelOptions)

waitUntil(timeout: testTimeout) { done in
channel.attach { error in
Expand Down
4 changes: 2 additions & 2 deletions Test/Tests/ObjectLifetimesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class ObjectLifetimesTests: XCTestCase {
XCTAssertNotNil(weakClient)
defer { client?.close() }

let channelName = uniqueChannelName(for: test)
let channelName = test.uniqueChannelName()
waitUntil(timeout: testTimeout) { done in
client!.channels.get(channelName).subscribe(attachCallback: { _ in
client = nil
Expand All @@ -105,7 +105,7 @@ class ObjectLifetimesTests: XCTestCase {
var client: ARTRealtime? = ARTRealtime(options: options)
weak var weakClient = client!.internal

var channel: ARTRealtimeChannel? = client!.channels.get(uniqueChannelName(for: test))
var channel: ARTRealtimeChannel? = client!.channels.get(test.uniqueChannelName())
weak var weakChannel = channel!.internal

waitUntil(timeout: testTimeout) { done in
Expand Down
8 changes: 4 additions & 4 deletions Test/Tests/PushAdminTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ class PushAdminTests: XCTestCase {
let options = try AblyTests.commonAppSetup(for: test)
let realtime = ARTRealtime(options: options)
defer { realtime.dispose(); realtime.close() }
let channel = realtime.channels.get("pushenabled:\(uniqueChannelName(for: test))") // works with pure uniqueChannelName(for: test) as well
let channel = realtime.channels.get("pushenabled:\(test.uniqueChannelName())") // works with pure test.uniqueChannelName() as well
let publishObject = ["transportType": "ablyChannel",
"channel": channel.name,
"ablyKey": options.key!,
Expand Down Expand Up @@ -260,7 +260,7 @@ class PushAdminTests: XCTestCase {
let test = Test()
let realtime = ARTRealtime(options: try AblyTests.commonAppSetup(for: test))
defer { realtime.dispose(); realtime.close() }
let channel = realtime.channels.get("pushenabled:\(uniqueChannelName(for: test))") // works with pure uniqueChannelName(for: test) as well
let channel = realtime.channels.get("pushenabled:\(test.uniqueChannelName())") // works with pure test.uniqueChannelName() as well

waitUntil(timeout: testTimeout) { done in
channel.attach { error in
Expand Down Expand Up @@ -288,7 +288,7 @@ class PushAdminTests: XCTestCase {
let test = Test()
let realtime = ARTRealtime(options: try AblyTests.commonAppSetup(for: test))
defer { realtime.dispose(); realtime.close() }
let channel = realtime.channels.get("pushenabled:\(uniqueChannelName(for: test))") // works with pure uniqueChannelName(for: test) as well
let channel = realtime.channels.get("pushenabled:\(test.uniqueChannelName())") // works with pure test.uniqueChannelName() as well

waitUntil(timeout: testTimeout) { done in
channel.attach { error in
Expand All @@ -315,7 +315,7 @@ class PushAdminTests: XCTestCase {
let test = Test()
let realtime = ARTRealtime(options: try AblyTests.commonAppSetup(for: test))
defer { realtime.dispose(); realtime.close() }
let channel = realtime.channels.get("pushenabled:\(uniqueChannelName(for: test))") // works with pure uniqueChannelName(for: test) as well
let channel = realtime.channels.get("pushenabled:\(test.uniqueChannelName())") // works with pure test.uniqueChannelName() as well

waitUntil(timeout: testTimeout) { done in
channel.attach { error in
Expand Down
Loading

0 comments on commit 4915315

Please sign in to comment.