Skip to content

Commit

Permalink
Merge pull request #252 from ably/fix-rtl7c
Browse files Browse the repository at this point in the history
Fix RTL7c: channel.subscribe fails when channel is FAILED.
  • Loading branch information
ricardopereira committed Feb 26, 2016
2 parents 87a0d19 + e805ecd commit 8889a63
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 75 deletions.
12 changes: 6 additions & 6 deletions ably-ios/ARTRealtimeChannel.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ ART_ASSUME_NONNULL_BEGIN
- (void)detach;
- (void)detach:(art_nullable void (^)(ARTErrorInfo *__art_nullable))cb;

- (__GENERIC(ARTEventListener, ARTMessage *) *)subscribe:(void (^)(ARTMessage *message))cb;
- (__GENERIC(ARTEventListener, ARTMessage *) *)subscribeWithAttachCallback:(art_nullable void (^)(ARTErrorInfo *__art_nullable))onAttach cb:(void (^)(ARTMessage *message))cb;
- (__GENERIC(ARTEventListener, ARTMessage *) *)subscribe:(NSString *)name cb:(void (^)(ARTMessage *message))cb;
- (__GENERIC(ARTEventListener, ARTMessage *) *)subscribe:(NSString *)name onAttach:(art_nullable void (^)(ARTErrorInfo *__art_nullable))onAttach cb:(void (^)(ARTMessage *message))cb;
- (__GENERIC(ARTEventListener, ARTMessage *) *__art_nullable)subscribe:(void (^)(ARTMessage *message))cb;
- (__GENERIC(ARTEventListener, ARTMessage *) *__art_nullable)subscribeWithAttachCallback:(art_nullable void (^)(ARTErrorInfo *__art_nullable))onAttach cb:(void (^)(ARTMessage *message))cb;
- (__GENERIC(ARTEventListener, ARTMessage *) *__art_nullable)subscribe:(NSString *)name cb:(void (^)(ARTMessage *message))cb;
- (__GENERIC(ARTEventListener, ARTMessage *) *__art_nullable)subscribe:(NSString *)name onAttach:(art_nullable void (^)(ARTErrorInfo *__art_nullable))onAttach cb:(void (^)(ARTMessage *message))cb;

- (void)unsubscribe;
- (void)unsubscribe:(__GENERIC(ARTEventListener, ARTMessage *) *)listener;
- (void)unsubscribe:(NSString *)name listener:(__GENERIC(ARTEventListener, ARTMessage *) *)listener;
- (void)unsubscribe:(__GENERIC(ARTEventListener, ARTMessage *) *__art_nullable)listener;
- (void)unsubscribe:(NSString *)name listener:(__GENERIC(ARTEventListener, ARTMessage *) *__art_nullable)listener;

- (void)history:(void(^)(__GENERIC(ARTPaginatedResult, ARTMessage *) *__art_nullable result, NSError *__art_nullable error))callback;
- (BOOL)history:(art_nullable ARTRealtimeHistoryQuery *)query callback:(void(^)(__GENERIC(ARTPaginatedResult, ARTMessage *) *__art_nullable result, NSError *__art_nullable error))callback error:(NSError *__art_nullable *__art_nullable)errorPtr;
Expand Down
8 changes: 8 additions & 0 deletions ably-ios/ARTRealtimeChannel.m
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,10 @@ - (void)throwOnDisconnectedOrFailed {
}

- (ARTEventListener<ARTMessage *> *)subscribeWithAttachCallback:(void (^)(ARTErrorInfo * _Nullable))onAttach cb:(void (^)(ARTMessage * _Nonnull))cb {
if (self.state == ARTRealtimeChannelFailed) {
if (onAttach) onAttach([ARTErrorInfo createWithCode:0 message:@"attempted to subscribe while channel is in Failed state."]);
return nil;
}
[self attach:onAttach];
return [self.messagesEventEmitter on:cb];
}
Expand All @@ -202,6 +206,10 @@ - (void)throwOnDisconnectedOrFailed {
}

- (ARTEventListener<ARTMessage *> *)subscribe:(NSString *)name onAttach:(void (^)(ARTErrorInfo * _Nullable))onAttach cb:(void (^)(ARTMessage * _Nonnull))cb {
if (self.state == ARTRealtimeChannelFailed) {
if (onAttach) onAttach([ARTErrorInfo createWithCode:0 message:@"attempted to subscribe while channel is in Failed state."]);
return nil;
}
[self attach:onAttach];
return [self.messagesEventEmitter on:name call:cb];
}
Expand Down
63 changes: 0 additions & 63 deletions ably-iosTests/ARTRealtimeResumeTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -48,69 +48,6 @@ - (void)tearDown {
[super tearDown];
}

/**
testSimple:
- Client A & Client B connect, attach to channel Y
- Client A is forcibly disconnected and does not (yet) reconnect and attempt resume
- Client B then publishes some messages on channel Y, and waits until the messages are received on channel Y
- Client A reconnects and resumes the connection. As connection resume should be working, it then receives the messages on channel Y whilst the client was disconnected, and channel Y of course remains attached
*/
- (void)testSimple {
NSString *channelName = @"resumeChannel";
NSString *message1 = @"message1";
NSString *message2 = @"message2";

XCTestExpectation *expectation = [self expectationWithDescription:@"testSimple"];
[ARTTestUtil setupApp:[ARTTestUtil clientOptions] cb:^(ARTClientOptions *options) {
_realtime = [[ARTRealtime alloc] initWithOptions:options];
_realtime2 = [[ARTRealtime alloc] initWithOptions:options];

ARTRealtimeChannel *channelA = [_realtime.channels get:channelName];
ARTRealtimeChannel *channelB = [_realtime2.channels get:channelName];

[channelA on:^(ARTErrorInfo *errorInfo) {
if (channelA.state == ARTRealtimeChannelAttached) {
// 2. Attach channel of Client B
[channelB attach];
}
}];

[channelB on:^(ARTErrorInfo *errorInfo) {
if (channelB.state == ARTRealtimeChannelAttached) {
// 3. Client B sends message and if OK then force A to disconnect
[channelB publish:nil data:message1 cb:^(ARTErrorInfo *errorInfo) {
XCTAssertNil(errorInfo);
// Forcibly disconnect
[_realtime onError:[ARTTestUtil newErrorProtocolMessage]];
}];
}
}];

[_realtime.connection on:^(ARTConnectionStateChange *stateChange) {
ARTRealtimeConnectionState state = stateChange.current;
if (state == ARTRealtimeFailed) {
// 4. Client A is disconnected and B sends message
[channelB publish:nil data:message2 cb:^(ARTErrorInfo *errorInfo) {
XCTAssertNil(errorInfo);
[channelA subscribe:^(ARTMessage *message) {
// 6. Check if Client A receives the last message
if ([message.data isEqual:message2]) {
[expectation fulfill];
}
}];
// 5. Client A will reconnect
[_realtime connect];
}];
}
else if (state == ARTRealtimeConnected) {
// 1. Attach channel of Client A
[channelA attach];
}
}];
}];
[self waitForExpectationsWithTimeout:[ARTTestUtil timeout] handler:nil];
}

-(void) testSimpleDisconnected {
XCTestExpectation *expectation = [self expectationWithDescription:@"testSimpleDisconnected"];
NSString * channelName = @"resumeChannel";
Expand Down
15 changes: 9 additions & 6 deletions ablySpec/RealtimeClientChannel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1050,7 +1050,7 @@ class RealtimeClientChannel: QuickSpec {
}

// RTL7c
pending("should result in an error if channel is in the FAILED state") {
it("should result in an error if channel is in the FAILED state") {
let client = ARTRealtime(options: AblyTests.commonAppSetup())
defer { client.close() }

Expand All @@ -1059,11 +1059,14 @@ class RealtimeClientChannel: QuickSpec {
expect(channel.state).to(equal(ARTRealtimeChannelState.Failed))

waitUntil(timeout: testTimeout) { done in
channel.subscribe { message in
// FIXME: error handling
//https://github.com/ably/ably-ios/pull/208#discussion_r53043622
done()
}
channel.subscribeWithAttachCallback({ errorInfo in
expect(errorInfo).toNot(beNil())

channel.subscribe("foo", onAttach: { errorInfo in
expect(errorInfo).toNot(beNil())
done()
}) { _ in }
}) { _ in }
}
}

Expand Down

0 comments on commit 8889a63

Please sign in to comment.