Skip to content

Commit

Permalink
Fix RTL4f.
Browse files Browse the repository at this point in the history
  • Loading branch information
tcard committed Feb 17, 2016
1 parent 17576b9 commit 3e7c3b5
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 11 deletions.
3 changes: 3 additions & 0 deletions ably-ios/ARTRealtime+Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ ART_ASSUME_NONNULL_BEGIN
// Message sending
- (void)send:(ARTProtocolMessage *)msg cb:(art_nullable ARTStatusCallback)cb;

- (CFRunLoopTimerRef)startTimer:(void(^)())onTimeout interval:(NSTimeInterval)interval;
- (void)cancelTimer:(CFRunLoopTimerRef)timer;

@end

ART_ASSUME_NONNULL_END
4 changes: 0 additions & 4 deletions ably-ios/ARTRealtime.m
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,6 @@ - (ARTStatus *)defaultError;
- (void)sendQueuedMessages;
- (void)failQueuedMessages:(ARTStatus *)error;

// Util
- (CFRunLoopTimerRef)startTimer:(void(^)())onTimeout interval:(NSTimeInterval)interval;
- (void)cancelTimer:(CFRunLoopTimerRef)timer;

@end


Expand Down
35 changes: 32 additions & 3 deletions ably-ios/ARTRealtimeChannel.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@
#import "ARTQueuedMessage.h"
#import "ARTNSArray+ARTFunctional.h"
#import "ARTStatus.h"
#import "ARTDefault.h"

@interface ARTRealtimeChannel () {
ARTRealtimePresence *_realtimePresence;
CFRunLoopTimerRef _attachTimer;
}

@end
Expand Down Expand Up @@ -212,6 +214,7 @@ - (void)emit:(ARTRealtimeChannelState)event with:(ARTErrorInfo *)data {
}

- (void)transition:(ARTRealtimeChannelState)state status:(ARTStatus *)status {
[self cancelAttachTimer];
if (self.state == state) {
return;
}
Expand All @@ -221,6 +224,15 @@ - (void)transition:(ARTRealtimeChannelState)state status:(ARTStatus *)status {
[self.statesEventEmitter emit:[NSNumber numberWithInt:state] with:status.errorInfo];
}

- (void)cancelAttachTimer {
[self.realtime cancelTimer:_attachTimer];
_attachTimer = nil;
}

- (void)dealloc {
[self cancelAttachTimer];
}

/**
Checks that a channelSerial is the final serial in a sequence of sync messages,
by checking that there is nothing after the colon
Expand Down Expand Up @@ -297,6 +309,7 @@ - (void)setDetached:(ARTProtocolMessage *)message {
}

- (void)releaseChannel {
[self cancelAttachTimer];
[self detachChannel:[ARTStatus state:ARTStateOk]];
[self.realtime.channels release:self.name];
}
Expand Down Expand Up @@ -402,11 +415,27 @@ - (void)attach:(void (^)(ARTErrorInfo * _Nullable))cb {
attachMessage.action = ARTProtocolMessageAttach;
attachMessage.channel = self.name;

[self.realtime send:attachMessage cb:(cb ? ^(ARTStatus *status) {
cb(status.errorInfo);
} : nil)];
__block BOOL attached = false;
__block BOOL timeouted = false;

[self.realtime send:attachMessage cb:^(ARTStatus *status) {
attached = true;
if (cb && !timeouted) cb(status.errorInfo);
}];
// Set state: Attaching
[self transition:ARTRealtimeChannelAttaching status:[ARTStatus state:ARTStateOk]];

_attachTimer = [self.realtime startTimer:^{
_attachTimer = nil;
if (!attached) {
timeouted = true;
ARTErrorInfo *errorInfo = [ARTErrorInfo createWithCode:ARTStateAttachTimedOut message:@"attach timed out"];
ARTStatus *status = [ARTStatus state:ARTStateAttachTimedOut info:errorInfo];
_errorReason = errorInfo;
[self transition:ARTRealtimeChannelFailed status:status];
if (cb) cb(errorInfo);
}
} interval:[ARTDefault realtimeRequestTimeout]];
}

- (void)detach:(void (^)(ARTErrorInfo * _Nullable))cb {
Expand Down
1 change: 1 addition & 0 deletions ably-ios/ARTStatus.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ typedef NS_ENUM(NSUInteger, ARTState) {
ARTStateAccessRefused,
ARTStateNeverConnected,
ARTStateConnectionTimedOut,
ARTStateAttachTimedOut,
ARTStateNotAttached,
ARTStateInvalidArgs,
ARTStateCryptoBadPadding,
Expand Down
15 changes: 11 additions & 4 deletions ablySpec/RealtimeClientChannel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ class RealtimeClientChannel: QuickSpec {
}

// RTL4f
pending("should transition the channel state to FAILED if ATTACHED ProtocolMessage is not received") {
it("should transition the channel state to FAILED if ATTACHED ProtocolMessage is not received") {
ARTDefault.setRealtimeRequestTimeout(3.0)
let options = AblyTests.commonAppSetup()
options.autoConnect = false
Expand All @@ -355,12 +355,19 @@ class RealtimeClientChannel: QuickSpec {
let transport = client.transport as! TestProxyTransport
transport.actionsIgnored += [.Attached]

var callbackCalled = false
let channel = client.channels.get("test")
channel.attach()
channel.attach { errorInfo in
expect(errorInfo).toNot(beNil())
expect(errorInfo).to(equal(channel.errorReason))
callbackCalled = true
}
let start = NSDate()
expect(channel.state).toEventually(equal(ARTRealtimeChannelState.Failed), timeout: ARTDefault.realtimeRequestTimeout())
expect(channel.state).toEventually(equal(ARTRealtimeChannelState.Failed), timeout: testTimeout)
expect(channel.errorReason).toNot(beNil())
expect(callbackCalled).to(beTrue())
let end = NSDate()
expect(start.dateByAddingTimeInterval(3.0)).to(beCloseTo(end))
expect(start.dateByAddingTimeInterval(3.0)).to(beCloseTo(end, within: 0.5))
}

}
Expand Down

0 comments on commit 3e7c3b5

Please sign in to comment.