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

Test RTL5*. #262

Merged
merged 1 commit into from
Mar 2, 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
20 changes: 20 additions & 0 deletions ably-ios/ARTRealtimeChannel.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
@interface ARTRealtimeChannel () {
ARTRealtimePresence *_realtimePresence;
CFRunLoopTimerRef _attachTimer;
CFRunLoopTimerRef _detachTimer;
__GENERIC(ARTEventEmitter, NSNull *, ARTErrorInfo *) *_attachedEventEmitter;
__GENERIC(ARTEventEmitter, NSNull *, ARTErrorInfo *) *_detachedEventEmitter;
}
Expand Down Expand Up @@ -259,6 +260,7 @@ - (void)emit:(ARTChannelEvent)event with:(ARTErrorInfo *)data {

- (void)transition:(ARTRealtimeChannelState)state status:(ARTStatus *)status {
[self cancelAttachTimer];
[self cancelDetachTimer];
if (self.state == state) {
return;
}
Expand All @@ -274,8 +276,14 @@ - (void)cancelAttachTimer {
_attachTimer = nil;
}

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

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

/**
Expand Down Expand Up @@ -510,10 +518,22 @@ - (void)detach:(void (^)(ARTErrorInfo * _Nullable))cb {
detachMessage.action = ARTProtocolMessageDetach;
detachMessage.channel = self.name;

__block BOOL timeouted = false;

[self.realtime send:detachMessage cb:nil];
if (cb) [_detachedEventEmitter once:cb];
// Set state: Detaching
[self transition:ARTRealtimeChannelDetaching status:[ARTStatus state:ARTStateOk]];

_detachTimer = [self.realtime startTimer:^{
timeouted = true;
ARTErrorInfo *errorInfo = [ARTErrorInfo createWithCode:ARTStateDetachTimedOut message:@"detach timed out"];
ARTStatus *status = [ARTStatus state:ARTStateDetachTimedOut info:errorInfo];
_errorReason = errorInfo;
[self transition:ARTRealtimeChannelFailed status:status];
[_detachedEventEmitter emit:[NSNull null] with:errorInfo];
} interval:[ARTDefault realtimeRequestTimeout]];

}

- (void)detach {
Expand Down
1 change: 1 addition & 0 deletions ably-ios/ARTStatus.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ typedef NS_ENUM(NSUInteger, ARTState) {
ARTStateNeverConnected,
ARTStateConnectionTimedOut,
ARTStateAttachTimedOut,
ARTStateDetachTimedOut,
ARTStateNotAttached,
ARTStateInvalidArgs,
ARTStateCryptoBadPadding,
Expand Down
111 changes: 111 additions & 0 deletions ablySpec/RealtimeClientChannel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,82 @@ class RealtimeClientChannel: QuickSpec {
}

describe("detach") {
// RTL5a
it("if state is INITIALISED, DETACHED or DETACHING nothing is done") {
let client = ARTRealtime(options: AblyTests.commonAppSetup())
defer { client.close() }

var errorInfo: ARTErrorInfo?
let channel = client.channels.get("test")

expect(channel.state).to(equal(ARTRealtimeChannelState.Initialised))
channel.detach { errorInfo in
expect(errorInfo).to(beNil())
}
expect(channel.state).to(equal(ARTRealtimeChannelState.Initialised))

channel.attach()
expect(channel.state).toEventually(equal(ARTRealtimeChannelState.Attaching), timeout: testTimeout)

channel.detach { errorInfo in
expect(errorInfo).to(beNil())
expect(channel.state).to(equal(ARTRealtimeChannelState.Detached))
}

expect(channel.state).to(equal(ARTRealtimeChannelState.Detaching))
channel.detach { errorInfo in
expect(errorInfo).to(beNil())
expect(channel.state).to(equal(ARTRealtimeChannelState.Detached))
}
expect(channel.state).to(equal(ARTRealtimeChannelState.Detaching))

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

waitUntil(timeout: testTimeout) { done in
channel.detach { errorInfo in
expect(errorInfo).to(beNil())
expect(channel.state).to(equal(ARTRealtimeChannelState.Detached))
done()
}
}
}

// RTL5b
it("results in an error if the connection state is FAILED") {
let client = ARTRealtime(options: AblyTests.commonAppSetup())
defer { client.close() }

let channel = client.channels.get("test")
client.onError(AblyTests.newErrorProtocolMessage())
expect(client.connection.state).to(equal(ARTRealtimeConnectionState.Failed))
expect(channel.detach()).toNot(beNil())
}

// RTL5d
it("should send a DETACH ProtocolMessage, change state to DETACHING and change state to DETACHED after confirmation") {
let options = AblyTests.commonAppSetup()
options.autoConnect = false
let client = ARTRealtime(options: options)
client.setTransportClass(TestProxyTransport.self)
client.connect()
defer { client.close() }

expect(client.connection.state).toEventually(equal(ARTRealtimeConnectionState.Connected), timeout: testTimeout)
let transport = client.transport as! TestProxyTransport

let channel = client.channels.get("test")
channel.attach()
expect(channel.state).toEventually(equal(ARTRealtimeChannelState.Attached), timeout: testTimeout)
channel.detach()

expect(channel.state).to(equal(ARTRealtimeChannelState.Detaching))
expect(transport.protocolMessagesSent.filter({ $0.action == .Detach })).to(haveCount(1))

expect(channel.state).toEventually(equal(ARTRealtimeChannelState.Detached), timeout: testTimeout)
expect(transport.protocolMessagesReceived.filter({ $0.action == .Detached })).to(haveCount(1))
}

// RTL5e
it("if called with a callback should call it once detached") {
let client = ARTRealtime(options: AblyTests.commonAppSetup())
defer { client.close() }
Expand All @@ -546,6 +622,7 @@ class RealtimeClientChannel: QuickSpec {
}
}

// RTL5e
it("if called with a callback and already detaching should call the callback once detached") {
let client = ARTRealtime(options: AblyTests.commonAppSetup())
defer { client.close() }
Expand All @@ -566,6 +643,7 @@ class RealtimeClientChannel: QuickSpec {
}
}

// RTL5e
it("if called with a callback and already detached should should call the callback with nil error") {
let client = ARTRealtime(options: AblyTests.commonAppSetup())
defer { client.close() }
Expand All @@ -584,6 +662,39 @@ class RealtimeClientChannel: QuickSpec {
}
}
}

// RTL5f
it("should transition the channel state to FAILED if DETACHED ProtocolMessage is not received") {
ARTDefault.setRealtimeRequestTimeout(3.0)
let options = AblyTests.commonAppSetup()
options.autoConnect = false
let client = ARTRealtime(options: options)
client.setTransportClass(TestProxyTransport.self)
client.connect()
defer { client.close() }

expect(client.connection.state).toEventually(equal(ARTRealtimeConnectionState.Connected), timeout: testTimeout)
let transport = client.transport as! TestProxyTransport
transport.actionsIgnored += [.Detached]

let channel = client.channels.get("test")
channel.attach()
expect(channel.state).toEventually(equal(ARTRealtimeChannelState.Attached), timeout: testTimeout)

var callbackCalled = false
channel.detach { 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: testTimeout)
expect(channel.errorReason).toNot(beNil())
expect(callbackCalled).to(beTrue())
let end = NSDate()
expect(start.dateByAddingTimeInterval(3.0)).to(beCloseTo(end, within: 0.5))
}

}

// RTL6
Expand Down