Skip to content

Commit

Permalink
Merge pull request #1282 from ably/fix/1280-unique-test-channel-names
Browse files Browse the repository at this point in the history
Fix/1280 unique test channel names
  • Loading branch information
lawrence-forooghian authored Mar 12, 2022
2 parents 6fc6909 + 6bfb4ba commit 3c3763c
Show file tree
Hide file tree
Showing 16 changed files with 779 additions and 613 deletions.
44 changes: 32 additions & 12 deletions Spec/Test Utilities/TestUtilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,26 @@ let testTimeout = DispatchTimeInterval.seconds(20)
let testResourcesPath = "ably-common/test-resources/"
let echoServerAddress = "https://echo.ably.io/createJWT"

func uniqueChannelName(prefix: String = "",
testIdentifier: String = #function,
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)-\(testIdentifier.replacingOccurrences(of: "()", with: ""))-\(timestamp)-\(NSUUID().uuidString)"
}

/// Common test utilities.
class AblyTests {

Expand Down Expand Up @@ -421,8 +441,8 @@ class PublishTestMessage {
var completion: ((ARTErrorInfo?) -> Void)? = nil
var error: ARTErrorInfo? = nil

init(client: ARTRest, failOnError: Bool = true, completion: ((ARTErrorInfo?) -> Void)? = nil) {
client.channels.get("test").publish(nil, data: "message") { error in
init(client: ARTRest, channelName: String, failOnError: Bool = true, completion: ((ARTErrorInfo?) -> Void)? = nil) {
client.channels.get(channelName).publish(nil, data: "message") { error in
self.error = error
if let callback = completion {
callback(error)
Expand All @@ -433,7 +453,7 @@ class PublishTestMessage {
}
}

init(client: ARTRealtime, failOnError: Bool = true, completion: ((ARTErrorInfo?) -> Void)? = nil) {
init(client: ARTRealtime, channelName: String, failOnError: Bool = true, completion: ((ARTErrorInfo?) -> Void)? = nil) {
let complete: (ARTErrorInfo?) -> Void = { errorInfo in
// ARTErrorInfo to NSError
self.error = errorInfo
Expand All @@ -449,7 +469,7 @@ class PublishTestMessage {
client.connection.on { stateChange in
let state = stateChange.current
if state == .connected {
let channel = client.channels.get("test")
let channel = client.channels.get(channelName)
channel.on { stateChange in
switch stateChange.current {
case .attached:
Expand All @@ -470,24 +490,24 @@ class PublishTestMessage {
}

/// Rest - Publish message
@discardableResult func publishTestMessage(_ rest: ARTRest, completion: Optional<(ARTErrorInfo?)->()>) -> PublishTestMessage {
return PublishTestMessage(client: rest, failOnError: false, completion: completion)
@discardableResult func publishTestMessage(_ rest: ARTRest, channelName: String, completion: Optional<(ARTErrorInfo?)->()>) -> PublishTestMessage {
return PublishTestMessage(client: rest, channelName: channelName, failOnError: false, completion: completion)
}

@discardableResult func publishTestMessage(_ rest: ARTRest, failOnError: Bool = true) -> PublishTestMessage {
return PublishTestMessage(client: rest, failOnError: failOnError)
@discardableResult func publishTestMessage(_ rest: ARTRest, channelName: String, failOnError: Bool = true) -> PublishTestMessage {
return PublishTestMessage(client: rest, channelName: channelName, failOnError: failOnError)
}

/// Realtime - Publish message with callback
/// (publishes if connection state changes to CONNECTED and channel state changes to ATTACHED)
@discardableResult func publishFirstTestMessage(_ realtime: ARTRealtime, completion: Optional<(ARTErrorInfo?)->()>) -> PublishTestMessage {
return PublishTestMessage(client: realtime, failOnError: false, completion: completion)
@discardableResult func publishFirstTestMessage(_ realtime: ARTRealtime, channelName: String, completion: Optional<(ARTErrorInfo?)->()>) -> PublishTestMessage {
return PublishTestMessage(client: realtime, channelName: channelName, failOnError: false, completion: completion)
}

/// Realtime - Publish message
/// (publishes if connection state changes to CONNECTED and channel state changes to ATTACHED)
@discardableResult func publishFirstTestMessage(_ realtime: ARTRealtime, failOnError: Bool = true) -> PublishTestMessage {
return PublishTestMessage(client: realtime, failOnError: failOnError)
@discardableResult func publishFirstTestMessage(_ realtime: ARTRealtime, channelName: String, failOnError: Bool = true) -> PublishTestMessage {
return PublishTestMessage(client: realtime, channelName: channelName, failOnError: failOnError)
}

/// Access Token
Expand Down
50 changes: 26 additions & 24 deletions Spec/Tests/AuthTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ class AuthTests: XCTestCase {
client.internal.httpExecutor = testHTTPExecutor

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

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

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

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

let channel = rest.channels.get("test")
let channel = rest.channels.get(uniqueChannelName())

testHTTPExecutor.simulateIncomingServerErrorOnNextRequest(ARTErrorCode.tokenRevoked.intValue, description: "token revoked")
waitUntil(timeout: testTimeout) { done in
Expand Down Expand Up @@ -316,7 +316,7 @@ class AuthTests: XCTestCase {
expect(realtime.internal.options.authUrl).to(beNil())
realtime.internal.setTransport(TestProxyTransport.self)

let channel = realtime.channels.get("test")
let channel = realtime.channels.get(uniqueChannelName())

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

let channel = rest.channels.get("test")
let channel = rest.channels.get(uniqueChannelName())

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

Expand All @@ -374,7 +374,7 @@ class AuthTests: XCTestCase {
testHTTPExecutor = TestProxyHTTPExecutor(options.logHandler)
rest.internal.httpExecutor = testHTTPExecutor

let channel = rest.channels.get("test")
let channel = rest.channels.get(uniqueChannelName())

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

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

waitUntil(timeout: testTimeout) { done in
rest.channels.get("foo").history { _, error in
rest.channels.get(uniqueChannelName()).history { _, error in
guard let error = error else {
fail("Error is nil"); done(); return
}
Expand Down Expand Up @@ -947,7 +947,7 @@ class AuthTests: XCTestCase {
let state = stateChange.current
let error = stateChange.reason
if state == .connected, error == nil {
let currentChannel = client.channels.get("test")
let currentChannel = client.channels.get(uniqueChannelName())
currentChannel.subscribe { _ in
done()
}
Expand Down Expand Up @@ -1139,7 +1139,7 @@ class AuthTests: XCTestCase {
let rest = ARTRest(options: options)
testHTTPExecutor = TestProxyHTTPExecutor(options.logHandler)
rest.internal.httpExecutor = testHTTPExecutor
let channel = rest.channels.get("RSA7a1")
let channel = rest.channels.get(uniqueChannelName())
waitUntil(timeout: testTimeout) { done in
channel.publish("foo", data: nil) { error in
expect(error).to(beNil())
Expand All @@ -1166,7 +1166,7 @@ class AuthTests: XCTestCase {
client.internal.httpExecutor = testHTTPExecutor

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

expect(client.auth.clientId).to(equal("john"))
waitUntil(timeout: testTimeout) { done in
Expand Down Expand Up @@ -1853,7 +1853,7 @@ class AuthTests: XCTestCase {
let rest = ARTRest(options: options)
testHTTPExecutor = TestProxyHTTPExecutor(options.logHandler)
rest.internal.httpExecutor = testHTTPExecutor
let channel = rest.channels.get("test")
let channel = rest.channels.get(uniqueChannelName())

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

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

testHTTPExecutor = TestProxyHTTPExecutor(options.logHandler)
rest.internal.httpExecutor = testHTTPExecutor
let channel = rest.channels.get("test")
let channel = rest.channels.get(uniqueChannelName())

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

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

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

publishTestMessage(rest, completion: { error in
publishTestMessage(rest, channelName: uniqueChannelName(), completion: { error in
expect(error).to(beNil())
expect(rest.auth.internal.method).to(equal(ARTAuthMethod.token))
expect(rest.auth.tokenDetails?.token).to(equal(tokenDetails.token))
Expand All @@ -2570,7 +2570,7 @@ class AuthTests: XCTestCase {
expect(tokenDetails.token).toNot(beNil())
expect(rest.auth.internal.method).to(equal(ARTAuthMethod.token))

publishTestMessage(rest, completion: { error in
publishTestMessage(rest, channelName: uniqueChannelName(), completion: { error in
expect(error).to(beNil())
expect(rest.auth.internal.method).to(equal(ARTAuthMethod.token))
expect(rest.auth.tokenDetails?.token).to(equal(tokenDetails.token))
Expand Down Expand Up @@ -3769,7 +3769,7 @@ class AuthTests: XCTestCase {
options.token = initialToken
let realtime = ARTRealtime(options: options)
defer { realtime.dispose(); realtime.close() }
let channel = realtime.channels.get("foo")
let channel = realtime.channels.get(uniqueChannelName())

waitUntil(timeout: testTimeout) { done in
channel.attach { error in
Expand Down Expand Up @@ -4320,11 +4320,13 @@ class AuthTests: XCTestCase {
func test__002__should_accept_authURL_response_with_timestamp_argument_as_string() throws {
var originalTokenRequest: ARTTokenRequest!
let tmpRest = ARTRest(options: AblyTests.commonAppSetup())

let channelName = uniqueChannelName()
waitUntil(timeout: testTimeout) { done in
let tokenParams = ARTTokenParams()
tokenParams.clientId = "john"
tokenParams.capability = """
{"chat:*":["publish","subscribe","presence","history"]}
{"\(channelName)":["publish","subscribe","presence","history"]}
"""
tokenParams.ttl = 43200
tmpRest.auth.createTokenRequest(tokenParams, options: nil) { tokenRequest, error in
Expand All @@ -4348,7 +4350,7 @@ class AuthTests: XCTestCase {
#endif
let testHttpExecutor = TestProxyHTTPExecutor(options.logHandler)
rest.internal.httpExecutor = testHttpExecutor
let channel = rest.channels.get("chat:one")
let channel = rest.channels.get(channelName)

testHttpExecutor.simulateIncomingPayloadOnNextRequest(tokenRequestJsonString.data(using: .utf8)!)

Expand Down
6 changes: 3 additions & 3 deletions Spec/Tests/DeltaCodecTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class DeltaCodecTests: XCTestCase {
"delta": "vcdiff",
]

let channel = client.channels.get("foo", options: channelOptions)
let channel = client.channels.get(uniqueChannelName(), options: channelOptions)

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

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

waitUntil(timeout: testTimeout) { done in
channel.attach { error in
Expand Down
7 changes: 4 additions & 3 deletions Spec/Tests/ObjectLifetimesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,11 @@ class ObjectLifetimesTests: XCTestCase {
expect(weakClient).toNot(beNil())
defer { client?.close() }

let channelName = uniqueChannelName()
waitUntil(timeout: testTimeout) { done in
client!.channels.get("foo").subscribe(attachCallback: { _ in
client!.channels.get(channelName).subscribe(attachCallback: { _ in
client = nil
ARTRest(options: options).channels.get("foo").publish(nil, data: "bar")
ARTRest(options: options).channels.get(channelName).publish(nil, data: "bar")
}, callback: { msg in
expect(msg.data as? String).to(equal("bar"))
done()
Expand All @@ -102,7 +103,7 @@ class ObjectLifetimesTests: XCTestCase {
var client: ARTRealtime? = ARTRealtime(options: options)
weak var weakClient = client!.internal

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

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

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

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

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

0 comments on commit 3c3763c

Please sign in to comment.