Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ECO-4838] Enforce RTP6c attach rule for presence. #1942

Merged
merged 3 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Source/ARTRealtimePresence.m
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,9 @@ - (ARTEventListener *)_subscribe:(ARTPresenceAction)action onAttach:(nullable AR
ARTLogWarn(self.logger, @"R:%p C:%p (%@) presence subscribe to '%@' action(s) has been ignored (attempted to subscribe while channel is in FAILED state)", self->_realtime, self->_channel, self->_channel.name, ARTPresenceActionToStr(action));
return;
}
[self->_channel _attach:onAttach];
if (self->_channel.shouldAttach) { // RTP6c
[self->_channel _attach:onAttach];
}
listener = action == ARTPresenceActionAll ? [_eventEmitter on:cb] : [_eventEmitter on:[ARTEvent newWithPresenceAction:action] callback:cb];
ARTLogVerbose(self.logger, @"R:%p C:%p (%@) presence subscribe to '%@' action(s)", self->_realtime, self->_channel, self->_channel.name, ARTPresenceActionToStr(action));
});
Expand Down
1 change: 1 addition & 0 deletions Source/PrivateHeaders/Ably/ARTRealtimeChannel+Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ NS_ASSUME_NONNULL_BEGIN
- (ARTErrorInfo *)errorReason_nosync;
- (NSString * _Nullable)clientId_nosync;
- (BOOL)canBeReattached;
- (BOOL)shouldAttach;

@property (readonly, weak, nonatomic) ARTRealtimeInternal *realtime; // weak because realtime owns self
@property (readonly, nonatomic) ARTRestChannelInternal *restChannel;
Expand Down
17 changes: 15 additions & 2 deletions Test/Tests/RealtimeClientChannelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3318,11 +3318,24 @@ class RealtimeClientChannelTests: XCTestCase {
let test = Test()
let client = ARTRealtime(options: try AblyTests.commonAppSetup(for: test))
defer { client.dispose(); client.close() }

let channel = client.channels.get(test.uniqueChannelName())

// Initialized
XCTAssertEqual(channel.state, ARTRealtimeChannelState.initialized)
channel.subscribe { _ in }
XCTAssertEqual(channel.state, ARTRealtimeChannelState.attaching)
expect(channel.state).toEventually(equal(ARTRealtimeChannelState.attached), timeout: testTimeout)

// Detaching
channel.detach()
channel.subscribe { _ in }
XCTAssertEqual(channel.state, ARTRealtimeChannelState.detaching)
expect(channel.state).toEventually(equal(ARTRealtimeChannelState.attached), timeout: testTimeout)

// Detached
channel.detach()
expect(channel.state).toEventually(equal(ARTRealtimeChannelState.detached), timeout: testTimeout)
channel.subscribe { _ in }

expect(channel.state).toEventually(equal(ARTRealtimeChannelState.attached), timeout: testTimeout)
}

Expand Down
106 changes: 21 additions & 85 deletions Test/Tests/RealtimeClientPresenceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -893,16 +893,22 @@ class RealtimeClientPresenceTests: XCTestCase {
defer { client.dispose(); client.close() }
let channel = client.channels.get(test.uniqueChannelName())

// Initialized
XCTAssertEqual(channel.state, ARTRealtimeChannelState.initialized)
channel.presence.subscribe { _ in }
XCTAssertEqual(channel.state, ARTRealtimeChannelState.attaching)
expect(channel.state).toEventually(equal(ARTRealtimeChannelState.attached), timeout: testTimeout)


// Detaching
channel.detach()
channel.presence.subscribe { _ in }
XCTAssertEqual(channel.state, ARTRealtimeChannelState.detaching)
expect(channel.state).toEventually(equal(ARTRealtimeChannelState.attached), timeout: testTimeout)

// Detached
channel.detach()
expect(channel.state).toEventually(equal(ARTRealtimeChannelState.detached), timeout: testTimeout)

channel.presence.subscribe(.present) { _ in }
XCTAssertEqual(channel.state, ARTRealtimeChannelState.attaching)
channel.presence.subscribe { _ in }
expect(channel.state).toEventually(equal(ARTRealtimeChannelState.attached), timeout: testTimeout)
}

Expand All @@ -919,12 +925,10 @@ class RealtimeClientPresenceTests: XCTestCase {
waitUntil(timeout: testTimeout) { done in
channel.presence.subscribe(attachCallback: { errorInfo in
XCTAssertNotNil(errorInfo)

channel.presence.subscribe(.enter, onAttach: { errorInfo in
XCTAssertNotNil(errorInfo)
done()
}) { _ in }
}) { _ in }
done()
}, callback: { _ in
fail("Should not be called")
})
}
}

Expand All @@ -937,14 +941,13 @@ class RealtimeClientPresenceTests: XCTestCase {

waitUntil(timeout: testTimeout) { done in
let error = AblyTests.newErrorProtocolMessage()
channel.presence.subscribe(attachCallback: { errorInfo in
XCTAssertNotNil(errorInfo)

channel.presence.subscribe(.enter, onAttach: { errorInfo in
XCTAssertNotNil(errorInfo)
done()
}) { _ in }
}) { _ in }
channel.presence.subscribe(attachCallback: { error in
XCTAssertEqual(channel.state, ARTRealtimeChannelState.failed)
XCTAssertNotNil(error)
done()
}, callback: { _ in
fail("Should not be called")
})
AblyTests.queue.async {
channel.internal.onError(error)
}
Expand Down Expand Up @@ -2332,73 +2335,6 @@ class RealtimeClientPresenceTests: XCTestCase {
}
}

// RTP6

// RTP6c
func test__075__Presence__subscribe__should_implicitly_attach_the_channel() throws {
let test = Test()
let client = ARTRealtime(options: try AblyTests.commonAppSetup(for: test))
defer { client.dispose(); client.close() }
let channel = client.channels.get(test.uniqueChannelName())

XCTAssertEqual(channel.state, ARTRealtimeChannelState.initialized)
channel.presence.subscribe { _ in }
XCTAssertEqual(channel.state, ARTRealtimeChannelState.attaching)
expect(channel.state).toEventually(equal(ARTRealtimeChannelState.attached), timeout: testTimeout)

channel.detach()
expect(channel.state).toEventually(equal(ARTRealtimeChannelState.detached), timeout: testTimeout)

channel.presence.subscribe(.present) { _ in }
XCTAssertEqual(channel.state, ARTRealtimeChannelState.attaching)
expect(channel.state).toEventually(equal(ARTRealtimeChannelState.attached), timeout: testTimeout)
}

// RTP6c
func test__076__Presence__subscribe__should_result_in_an_error_if_the_channel_is_in_the_FAILED_state() throws {
let test = Test()
let client = ARTRealtime(options: try AblyTests.commonAppSetup(for: test))
defer { client.dispose(); client.close() }
let channel = client.channels.get(test.uniqueChannelName())

let protocolError = AblyTests.newErrorProtocolMessage()
AblyTests.queue.async {
channel.internal.onError(protocolError)
}

waitUntil(timeout: testTimeout) { done in
channel.presence.subscribe(attachCallback: { error in
XCTAssertEqual(channel.state, ARTRealtimeChannelState.failed)
XCTAssertNotNil(error)
done()
}, callback: { _ in
fail("Should not be called")
})
}
}

// RTP6c
func test__077__Presence__subscribe__should_result_in_an_error_if_the_channel_moves_to_the_FAILED_state() throws {
let test = Test()
let client = ARTRealtime(options: try AblyTests.commonAppSetup(for: test))
defer { client.dispose(); client.close() }
let channel = client.channels.get(test.uniqueChannelName())

waitUntil(timeout: testTimeout) { done in
let error = AblyTests.newErrorProtocolMessage()
channel.presence.subscribe(attachCallback: { error in
XCTAssertEqual(channel.state, ARTRealtimeChannelState.failed)
XCTAssertNotNil(error)
done()
}, callback: { _ in
fail("Should not be called")
})
AblyTests.queue.async {
channel.internal.onError(error)
}
}
}

// RTP8

// RTP8e
Expand Down
Loading