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 suite websocket #169

Merged
merged 9 commits into from
Jan 27, 2016
16 changes: 6 additions & 10 deletions ably-ios/ARTRealtime+Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,17 @@ ART_ASSUME_NONNULL_BEGIN
@property (readonly, strong, nonatomic) NSMutableArray *stateSubscriptions;

// Transport Events
- (void)onHeartbeat:(ARTProtocolMessage *)message;
- (void)onConnected:(ARTProtocolMessage *)message withErrorInfo:(art_nullable ARTErrorInfo *)errorInfo;
- (void)onDisconnected:(ARTProtocolMessage *)message;
- (void)onError:(ARTProtocolMessage *)message withErrorInfo:(art_nullable ARTErrorInfo *)errorInfo;
- (void)onHeartbeat;
- (void)onConnected:(ARTProtocolMessage *)message;
- (void)onDisconnected;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you not missing onClosed:(ART ProtocolMessage *)message?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

onClosed doesn't exist right from the beginning. I'm assuming the tests are calling the close() method.

- (void)onSuspended;
- (void)onError:(ARTProtocolMessage *)message;
- (void)onAck:(ARTProtocolMessage *)message;
- (void)onNack:(ARTProtocolMessage *)message;
- (void)onChannelMessage:(ARTProtocolMessage *)message withErrorInfo:(art_nullable ARTErrorInfo *)errorInfo;

- (void)onSuspended;
- (void)onChannelMessage:(ARTProtocolMessage *)message;

- (int64_t)connectionSerial;

- (void)transition:(ARTRealtimeConnectionState)state;
- (void)transition:(ARTRealtimeConnectionState)state withErrorInfo:(art_nullable ARTErrorInfo *)errorInfo;

// FIXME: Connection should manage the transport
- (void)setTransportClass:(Class)transportClass;
- (ARTConnection *)connection;
Expand Down
35 changes: 17 additions & 18 deletions ably-ios/ARTRealtime.m
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ -(void) cancelPingTimer {
self.pingTimeout = nil;
}

- (void)onHeartbeat:(ARTProtocolMessage *)message {
- (void)onHeartbeat {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you remove the ProtocolMessage?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because the method was not using it.

[self.logger verbose:@"ARTRealtime heartbeat received"];
if(self.pingCb) {
[self cancelPingTimer];
Expand All @@ -461,15 +461,15 @@ - (void)onHeartbeat:(ARTProtocolMessage *)message {
}
}

- (void)onConnected:(ARTProtocolMessage *)message withErrorInfo:(ARTErrorInfo *)errorInfo {
- (void)onConnected:(ARTProtocolMessage *)message {
// Resuming
if ([self isFromResume]) {
if (errorInfo && ![message.connectionId isEqualToString:self.connectionId]) {
if (message.error && ![message.connectionId isEqualToString:self.connectionId]) {
[self.logger warn:@"ARTRealtime: connection has reconnected, but resume failed. Detaching all channels"];
// Fatal error, detach all channels
for (NSString *channelName in self.allChannels) {
ARTRealtimeChannel *channel = [self.allChannels objectForKey:channelName];
[channel detachChannel:[ARTStatus state:ARTStateConnectionDisconnected info:errorInfo]];
[channel detachChannel:[ARTStatus state:ARTStateConnectionDisconnected info:message.error]];
}

self.options.resumeKey = nil;
Expand All @@ -481,8 +481,8 @@ - (void)onConnected:(ARTProtocolMessage *)message withErrorInfo:(ARTErrorInfo *)
}
}
}
else if (errorInfo) {
[self.logger warn:@"ARTRealtime: connection has resumed with non-fatal error %@", errorInfo.message];
else if (message.error) {
[self.logger warn:@"ARTRealtime: connection has resumed with non-fatal error %@", message.error.message];
// The error will be emitted on `transition`
}
}
Expand All @@ -496,7 +496,7 @@ - (void)onConnected:(ARTProtocolMessage *)message withErrorInfo:(ARTErrorInfo *)
self.msgSerial = 0;
self.pendingMessageStartSerial = 0;
}
[self transition:ARTRealtimeConnected withErrorInfo:errorInfo];
[self transition:ARTRealtimeConnected withErrorInfo:message.error];
break;
default:
NSAssert(false, @"Invalid Realtime state: expected Connecting has current state");
Expand All @@ -512,12 +512,11 @@ - (NSString *)connectionId {
return _connectionId;
}

- (void)onDisconnected:(ARTProtocolMessage *)message {
- (void)onDisconnected {
[self.logger info:@"ARTRealtime disconnected"];
switch (self.state) {
case ARTRealtimeConnected:
self.connectionId = nil;
self.msgSerial = 0;
[self transition:ARTRealtimeDisconnected];
break;
default:
Expand All @@ -526,13 +525,13 @@ - (void)onDisconnected:(ARTProtocolMessage *)message {
}
}

- (void)onError:(ARTProtocolMessage *)message withErrorInfo:(ARTErrorInfo *)errorInfo {
- (void)onError:(ARTProtocolMessage *)message {
// TODO work out which states this can be received in
if (message.channel) {
[self onChannelMessage:message withErrorInfo:errorInfo];
[self onChannelMessage:message];
} else {
self.connectionId = nil;
[self transition:ARTRealtimeFailed withErrorInfo:errorInfo];
[self transition:ARTRealtimeFailed withErrorInfo:message.error];
}
}

Expand All @@ -544,7 +543,7 @@ - (void)onNack:(ARTProtocolMessage *)message {
[self nack:message];
}

- (void)onChannelMessage:(ARTProtocolMessage *)message withErrorInfo:(ARTErrorInfo *)errorInfo {
- (void)onChannelMessage:(ARTProtocolMessage *)message {
// TODO work out which states this can be received in / error info?
ARTRealtimeChannel *channel = [self.allChannels objectForKey:message.channel];
[channel onChannelMessage:message];
Expand Down Expand Up @@ -805,19 +804,19 @@ - (void)realtimeTransport:(id)transport didReceiveMessage:(ARTProtocolMessage *)

switch (message.action) {
case ARTProtocolMessageHeartbeat:
[self onHeartbeat:message];
[self onHeartbeat];
break;
case ARTProtocolMessageError:
[self onError:message withErrorInfo:message.error];
[self onError:message];
break;
case ARTProtocolMessageConnected:
// Set Auth#clientId
[[self auth] setProtocolClientId:message.clientId];
// Event
[self onConnected:message withErrorInfo:message.error];
[self onConnected:message];
break;
case ARTProtocolMessageDisconnected:
[self onDisconnected:message];
[self onDisconnected];
break;
case ARTProtocolMessageAck:
[self onAck:message];
Expand All @@ -829,7 +828,7 @@ - (void)realtimeTransport:(id)transport didReceiveMessage:(ARTProtocolMessage *)
[self transition:ARTRealtimeClosed];
break;
default:
[self onChannelMessage:message withErrorInfo:message.error];
[self onChannelMessage:message];
break;
}
}
Expand Down
6 changes: 3 additions & 3 deletions ably-iosTests/ARTRealtimeAttachTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ - (void) testAttachFailsOnFailedConnection {
if (state == ARTRealtimeChannelAttached) {
if(!hasFailed) {
XCTAssertEqual(ARTStateOk, reason.state);
[realtime onError:nil withErrorInfo:nil];
[realtime onError:[ARTTestUtil newErrorProtocolMessage]];
}
}
else if(state == ARTRealtimeChannelFailed) {
Expand Down Expand Up @@ -306,7 +306,7 @@ - (void)testAttachingChannelFails {
ARTRealtimeChannel *channel1 = [realtime channel:@"channel"];
[channel1 subscribeToStateChanges:^(ARTRealtimeChannelState state, ARTStatus *reason) {
if (state == ARTRealtimeChannelAttaching) {
[realtime onError:nil withErrorInfo:nil];
[realtime onError:[ARTTestUtil newErrorProtocolMessage]];
}
else {
XCTAssertEqual(ARTRealtimeChannelFailed, state);
Expand All @@ -325,7 +325,7 @@ - (void)testAttachedChannelFails {
ARTRealtimeChannel *channel1 = [realtime channel:@"channel"];
[channel1 subscribeToStateChanges:^(ARTRealtimeChannelState state, ARTStatus *reason) {
if (state == ARTRealtimeChannelAttached) {
[realtime onError:nil withErrorInfo:nil];
[realtime onError:[ARTTestUtil newErrorProtocolMessage]];
}
else if(state != ARTRealtimeChannelAttaching) {
XCTAssertEqual(ARTRealtimeChannelFailed, state);
Expand Down
7 changes: 3 additions & 4 deletions ably-iosTests/ARTRealtimeChannelTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ - (void) testFailingFailsChannel {
ARTRealtimeChannel *channel = [realtime channel:@"channel"];
[channel subscribeToStateChanges:^(ARTRealtimeChannelState state, ARTStatus *reason) {
if(state == ARTRealtimeChannelAttached) {
[realtime onError:nil withErrorInfo:nil];
[realtime onError:[ARTTestUtil newErrorProtocolMessage]];
}
else if(state == ARTRealtimeChannelFailed) {
[channel publish:@"will_fail" cb:^(ARTStatus *status) {
Expand Down Expand Up @@ -297,8 +297,7 @@ - (void)testAttachFails {
if (state == ARTRealtimeConnected) {
[channel subscribeToStateChanges:^(ARTRealtimeChannelState state, ARTStatus *reason) {
if (state == ARTRealtimeChannelAttached) {
// FIXME: create proper methods to transition each state.
[realtime onError:nil withErrorInfo:nil];
[realtime onError:[ARTTestUtil newErrorProtocolMessage]];
}
}];
[channel attach];
Expand All @@ -324,7 +323,7 @@ - (void)testDetachFails {
if (state == ARTRealtimeConnected) {
[channel subscribeToStateChanges:^(ARTRealtimeChannelState state, ARTStatus *reason) {
if (state == ARTRealtimeChannelAttached) {
[realtime onError:nil withErrorInfo:nil];
[realtime onError:[ARTTestUtil newErrorProtocolMessage]];
}
}];
[channel attach];
Expand Down
6 changes: 3 additions & 3 deletions ably-iosTests/ARTRealtimeConnectTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ - (void)testConnectStates {
[realtime close];
}
else {
[realtime onDisconnected:nil];
[realtime onDisconnected];
}
}
else if(state == ARTRealtimeDisconnected) {
Expand All @@ -228,7 +228,7 @@ - (void)testConnectStates {
}
else if(state == ARTRealtimeSuspended) {
gotSuspended = true;
[realtime onError:nil withErrorInfo:nil];
[realtime onError:[ARTTestUtil newErrorProtocolMessage]];
}
else if(state == ARTRealtimeClosing) {
gotClosing = true;
Expand Down Expand Up @@ -268,7 +268,7 @@ - (void)testConnectPingError {
if(state == ARTRealtimeClosed) {
hasClosed = true;
XCTAssertThrows([realtime ping:^(ARTStatus *s) {}]);
[realtime onError:nil withErrorInfo:nil];
[realtime onError:[ARTTestUtil newErrorProtocolMessage]];
}
if(state == ARTRealtimeFailed) {
XCTAssertTrue(hasClosed);
Expand Down
2 changes: 1 addition & 1 deletion ably-iosTests/ARTRealtimeMessageTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ - (void)testMessageQueue {
connectingHappened = true;
[channel publish:connectingMessage cb:^(ARTStatus *status) {
XCTAssertEqual(ARTStateOk, status.state);
[realtime onDisconnected:nil];
[realtime onDisconnected];
}];
}
}
Expand Down
10 changes: 5 additions & 5 deletions ably-iosTests/ARTRealtimePresenceTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,7 @@ - (void)testEnterFailsOnError {
ARTRealtimeChannel *channel = [realtime channel:channelName];
[realtime.eventEmitter on:^(ARTRealtimeConnectionState state, ARTErrorInfo *errorInfo) {
if(state == ARTRealtimeConnected) {
[realtime onError:nil withErrorInfo:nil];
[realtime onError:[ARTTestUtil newErrorProtocolMessage]];
[channel.presence enter:presenceEnter cb:^(ARTStatus *status) {
XCTAssertEqual(ARTStateError, status.state);
[exp fulfill];
Expand All @@ -774,12 +774,12 @@ -(void) testGetFailsOnDetachedOrFailed {
__block bool hasDisconnected = false;
[realtime.eventEmitter on:^(ARTRealtimeConnectionState state, ARTErrorInfo *errorInfo) {
if(state == ARTRealtimeConnected) {
[realtime onDisconnected:nil];
[realtime onDisconnected];
}
else if(state == ARTRealtimeDisconnected) {
hasDisconnected = true;
XCTAssertThrows([channel.presence get:^(ARTPaginatedResult *result, NSError *error) {}]);
[realtime onError:nil withErrorInfo:nil];
[realtime onError:[ARTTestUtil newErrorProtocolMessage]];
}
else if(state == ARTRealtimeFailed) {
XCTAssertTrue(hasDisconnected);
Expand Down Expand Up @@ -825,7 +825,7 @@ - (void)testEnterClientIdFailsOnError {
ARTRealtimeChannel *channel = [realtime channel:@"channelName"];
[realtime.eventEmitter on:^(ARTRealtimeConnectionState state, ARTErrorInfo *errorInfo) {
if(state == ARTRealtimeConnected) {
[realtime onError:nil withErrorInfo:nil];
[realtime onError:[ARTTestUtil newErrorProtocolMessage]];
[channel.presence enterClient:@"clientId" data:@"" cb:^(ARTStatus *status) {
XCTAssertEqual(ARTStateError, status.state);
[exp fulfill];
Expand Down Expand Up @@ -1115,7 +1115,7 @@ - (void)testSyncResumes {
if(!firstSyncMessageReceived) {
XCTAssertFalse([channel2.presenceMap isSyncComplete]); //confirm we still have more syncing to do.
firstSyncMessageReceived = true;
[realtime2 onError:nil withErrorInfo:nil];
[realtime2 onError:[ARTTestUtil newErrorProtocolMessage]];
}
else if([channel2.presenceMap isSyncComplete] && !syncComplete) {
XCTAssertTrue(hasFailed);
Expand Down
3 changes: 1 addition & 2 deletions ably-iosTests/ARTRealtimeRecoverTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ - (void)testRecoverDisconnected {
_realtime = [[ARTRealtime alloc] initWithOptions:options];

__block NSString *firstConnectionId = nil;
__block bool gotFirstMessage = false;
[_realtime.eventEmitter on:^(ARTRealtimeConnectionState state, ARTErrorInfo *errorInfo) {
if (state == ARTRealtimeConnected) {
firstConnectionId = [_realtime connectionId];
Expand All @@ -80,7 +79,7 @@ - (void)testRecoverDisconnected {
// Sending a message
[channel publish:c1Message cb:^(ARTStatus *status) {
XCTAssertEqual(ARTStateOk, status.state);
[_realtime onDisconnected:nil];
[_realtime onDisconnected];
}];
}
else if (state == ARTRealtimeDisconnected) {
Expand Down
4 changes: 2 additions & 2 deletions ably-iosTests/ARTRealtimeResumeTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ - (void)testSimple {
[channelB publish:message1 cb:^(ARTStatus *status) {
XCTAssertEqual(ARTStateOk, status.state);
// Forcibly disconnect
[_realtime onError:nil withErrorInfo:nil];
[_realtime onError:[ARTTestUtil newErrorProtocolMessage]];
}];
}
}];
Expand Down Expand Up @@ -142,7 +142,7 @@ -(void) testSimpleDisconnected {
NSString * msg = [message content];
if([msg isEqualToString:message2]) {
//disconnect connection1
[_realtime onError:nil withErrorInfo:nil];
[_realtime onError:[ARTTestUtil newErrorProtocolMessage]];
[channel2 publish:message3 cb:^(ARTStatus *status) {
[channel2 publish:message4 cb:^(ARTStatus *status) {
[_realtime connect];
Expand Down
2 changes: 2 additions & 0 deletions ably-iosTests/ARTTestUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,6 @@ typedef void (^ARTRealtimeTestCallback)(ARTRealtime *realtime, ARTRealtimeConnec
+ (NSString *)getCrypto256Json;
+ (NSString *)getErrorsJson;

+ (ARTProtocolMessage *)newErrorProtocolMessage;

@end
8 changes: 8 additions & 0 deletions ably-iosTests/ARTTestUtil.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#import "ARTRealtimeChannel.h"
#import "ARTRealtimePresence.h"
#import "ARTPayload.h"
#import "ARTProtocolMessage.h"
#import "ARTEventEmitter.h"
#import "ARTURLSessionServerTrust.h"

Expand Down Expand Up @@ -280,4 +281,11 @@ + (void)testRealtimeV2:(XCTestCase *)testCase withDebug:(BOOL)debug callback:(AR
[testCase waitForExpectationsWithTimeout:[ARTTestUtil timeout] handler:nil];
}

+ (ARTProtocolMessage *)newErrorProtocolMessage {
ARTProtocolMessage* protocolMessage = [[ARTProtocolMessage alloc] init];
protocolMessage.action = ARTProtocolMessageError;
protocolMessage.error = [ARTErrorInfo createWithCode:0 message:@"Fail test"];
return protocolMessage;
}

@end
Loading