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

Fix: should attach if the channel is in the FAILED state #507

Merged
merged 4 commits into from
Sep 29, 2016
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
13 changes: 10 additions & 3 deletions Source/ARTRealtimeChannel.m
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,11 @@ - (void)transition:(ARTRealtimeChannelState)state status:(ARTStatus *)status {
[_attachedEventEmitter emit:[NSNull null] with:status.errorInfo];
[_detachedEventEmitter emit:[NSNull null] with:status.errorInfo];
}
else if (state == ARTRealtimeChannelDetaching) {
NSString *msg = @"channel is being DETACHED";
[self.realtime.logger debug:__FILE__ line:__LINE__ message:@"R:%p C:%p %@", _realtime, self, msg];
[_attachedEventEmitter emit:[NSNull null] with:[ARTErrorInfo createWithCode:90000 message:msg]];
}

[self emit:(ARTChannelEvent)state with:status.errorInfo];
}
Expand Down Expand Up @@ -544,13 +549,15 @@ - (void)attach:(void (^)(ARTErrorInfo * _Nullable))callback {
[self.realtime.logger debug:__FILE__ line:__LINE__ message:@"R:%p C:%p already attached", _realtime, self];
if (callback) callback(nil);
return;
case ARTRealtimeChannelDetaching:
case ARTRealtimeChannelFailed: {
NSString *msg = @"can't attach when in DETACHING or FAILED state";
case ARTRealtimeChannelDetaching: {
NSString *msg = @"can't attach when in DETACHING state";
[self.realtime.logger debug:__FILE__ line:__LINE__ message:@"R:%p C:%p %@", _realtime, self, msg];
if (callback) callback([ARTErrorInfo createWithCode:90000 message:msg]);
return;
}
case ARTRealtimeChannelFailed:
_errorReason = nil;
break;
default:
break;
}
Expand Down
10 changes: 10 additions & 0 deletions Source/ARTRealtimePresence.m
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ - (void)get:(void (^)(NSArray<ARTPresenceMessage *> *, ARTErrorInfo *))callback

- (void)get:(ARTRealtimePresenceQuery *)query callback:(void (^)(NSArray<ARTPresenceMessage *> *, ARTErrorInfo *))callback {
[_channel throwOnDisconnectedOrFailed];

switch (_channel.state) {
case ARTRealtimeChannelFailed:
case ARTRealtimeChannelDetached:
if (callback) callback(nil, [ARTErrorInfo createWithCode:0 message:@"invalid channel state"]);
return;
default:
break;
}

[_channel attach:^(ARTErrorInfo *error) {
if (error) {
callback(nil, error);
Expand Down
6 changes: 5 additions & 1 deletion Spec/RealtimeClientChannel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -380,13 +380,17 @@ class RealtimeClientChannel: QuickSpec {
errorMsg.channel = channel.name
client.onError(errorMsg)
expect(channel.state).to(equal(ARTRealtimeChannelState.Failed))
expect(channel.errorReason).toNot(beNil())

waitUntil(timeout: testTimeout) { done in
channel.attach { error in
expect(error).toNot(beNil())
expect(error).to(beNil())
done()
}
}

expect(channel.state).to(equal(ARTRealtimeChannelState.Attached))
expect(channel.errorReason).to(beNil())
}
}

Expand Down
85 changes: 80 additions & 5 deletions Spec/RealtimeClientPresence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1746,11 +1746,26 @@ class RealtimeClientPresence: QuickSpec {
defer { client.dispose(); client.close() }
let channel = client.channels.get("test")

channel.onError(AblyTests.newErrorProtocolMessage())
let pm = AblyTests.newErrorProtocolMessage()
channel.onError(pm)

guard let protocolError = pm.error else {
fail("Protocol error is empty"); return
}
guard let channelError = channel.errorReason else {
fail("Channel error is empty"); return
}
expect(channelError.message).to(equal(protocolError.message))
expect(channel.state).to(equal(ARTRealtimeChannelState.Failed))

waitUntil(timeout: testTimeout) { done in
channel.presence.get() { members, error in
expect(error!.message).to(contain("can't attach when in DETACHING or FAILED state"))
guard let error = error else {
fail("Error is empty"); done(); return
}
expect(error.message).to(equal("invalid channel state"))
expect(channel.errorReason).to(equal(protocolError))
expect(channel.state).to(equal(ARTRealtimeChannelState.Failed))
expect(members).to(beNil())
done()
}
Expand All @@ -1764,14 +1779,74 @@ class RealtimeClientPresence: QuickSpec {
let channel = client.channels.get("test")

waitUntil(timeout: testTimeout) { done in
let protoError = AblyTests.newErrorProtocolMessage()
let pm = AblyTests.newErrorProtocolMessage()
guard let protocolError = pm.error else {
fail("Protocol error is empty"); done(); return
}
channel.presence.get() { members, error in
expect(error).to(equal(protoError.error))
guard let error = error else {
fail("Error is empty"); done(); return
}
expect(error.message).to(equal(protocolError.message))
expect(members).to(beNil())
done()
}
channel.onError(protoError)
channel.onError(pm)
}

expect(channel.state).to(equal(ARTRealtimeChannelState.Failed))
}

// RTP11b
it("should result in an error if the channel is in the DETACHED state") {
let client = ARTRealtime(options: AblyTests.commonAppSetup())
defer { client.dispose(); client.close() }
let channel = client.channels.get("test")

waitUntil(timeout: testTimeout) { done in
channel.attach()
channel.detach() { error in
expect(error).to(beNil())
done()
}
}

waitUntil(timeout: testTimeout) { done in
channel.presence.get() { members, error in
guard let error = error else {
fail("Error is empty"); done(); return
}
expect(error.message).to(equal("invalid channel state"))
expect(members).to(beNil())
expect(channel.state).to(equal(ARTRealtimeChannelState.Detached))
done()
}
}
}

// RTP11b
it("should result in an error if the channel moves to the DETACHED state") {
let client = ARTRealtime(options: AblyTests.commonAppSetup())
defer { client.dispose(); client.close() }
let channel = client.channels.get("test")

waitUntil(timeout: testTimeout) { done in
let partialDone = AblyTests.splitDone(2, done: done)
channel.presence.get() { members, error in
guard let error = error else {
fail("Error is nil"); done(); return
}
expect(error.message).to(equal("channel is being DETACHED"))
expect(members).to(beNil())
partialDone()
}
channel.detach() { error in
expect(error).to(beNil())
partialDone()
}
}

expect(channel.state).to(equal(ARTRealtimeChannelState.Detached))
}

// RTP11c
Expand Down