Skip to content

Commit

Permalink
Merge pull request #244 from ably/RTN15a
Browse files Browse the repository at this point in the history
RTN15a
  • Loading branch information
ricardopereira committed Mar 2, 2016
2 parents 3d10de2 + 8f6be8b commit b44346a
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 5 deletions.
3 changes: 3 additions & 0 deletions ably-ios/ARTRealtime+Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ ART_ASSUME_NONNULL_BEGIN
/// First `msgSerial` pending message.
@property (readwrite, assign, nonatomic) int64_t pendingMessageStartSerial;

/// Client is trying to resume the last connection
@property (readwrite, assign, nonatomic) BOOL resuming;

@property (nonatomic, copy, art_nullable) ARTRealtimePingCb pingCb;
@property (readonly, getter=getClientOptions) ARTClientOptions *options;

Expand Down
4 changes: 1 addition & 3 deletions ably-ios/ARTRealtime.m
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,7 @@ - (void)failQueuedMessages:(ARTStatus *)error;

#pragma mark - ARTRealtime implementation

@implementation ARTRealtime {
BOOL _resuming;
}
@implementation ARTRealtime

- (instancetype)initWithKey:(NSString *)key {
return [self initWithOptions:[[ARTClientOptions alloc] initWithKey:key]];
Expand Down
62 changes: 61 additions & 1 deletion ablySpec/RealtimeClientConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -782,7 +782,7 @@ class RealtimeClientConnection: QuickSpec {
delay(1.0) { done() }
}

client.simulateLostConnection()
client.simulateLostConnectionAndState()
expect(gotPublishedCallback).to(beFalse())
expect(client.connection.state).toEventually(equal(ARTRealtimeConnectionState.Connected), timeout: testTimeout)
expect(client.connection.id).toNot(equal(oldConnectionId))
Expand Down Expand Up @@ -1393,6 +1393,66 @@ class RealtimeClientConnection: QuickSpec {

}

// RTN15
context("connection failures once CONNECTED") {

// RTN15a
it("should not receive published messages until the connection reconnects successfully") {
let options = AblyTests.commonAppSetup()
options.autoConnect = false
options.disconnectedRetryTimeout = 1.0

let client1 = ARTRealtime(options: options)
defer { client1.close() }
let channel1 = client1.channels.get("test")

var states = [ARTRealtimeConnectionState]()
client1.connection.on() { stateChange in
states = states + [stateChange!.current]
}
client1.connect()

let client2 = ARTRealtime(options: options)
client2.connect()
defer { client2.close() }
let channel2 = client2.channels.get("test")

channel1.subscribe { message in
fail("Shouldn't receive the messsage")
}

expect(channel1.state).toEventually(equal(ARTRealtimeChannelState.Attached), timeout: testTimeout)

let firstConnection: (id: String, key: String) = (client1.connection.id!, client1.connection.key!)

// Connection state cannot be resumed
client1.simulateLostConnectionAndState()

channel2.publish(nil, data: "message") { errorInfo in
expect(errorInfo).to(beNil())
}

waitUntil(timeout: testTimeout) { done in
client1.connection.once(.Connecting) { _ in
expect(client1.resuming).to(beTrue())
done()
}
}

waitUntil(timeout: testTimeout) { done in
client1.connection.once(.Connected) { _ in
expect(client1.resuming).to(beFalse())
expect(client1.connection.id).toNot(equal(firstConnection.id))
expect(client1.connection.key).toNot(equal(firstConnection.key))
done()
}
}

expect(states).to(equal([.Connecting, .Connected, .Disconnected, .Connecting, .Connected]))
}

}

// RTN18
context("state change side effects") {

Expand Down
2 changes: 1 addition & 1 deletion ablySpec/TestUtilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ extension JSON {

extension ARTRealtime {

func simulateLostConnection() {
func simulateLostConnectionAndState() {
//1. Abruptly disconnect
//2. Change the `Connection#id` and `Connection#key` before the client
// library attempts to reconnect and resume the connection
Expand Down

0 comments on commit b44346a

Please sign in to comment.