Skip to content

Commit

Permalink
Added check on CONNECTING state in the _connect method. Added addit…
Browse files Browse the repository at this point in the history
…ional fiels to the state description + test.
  • Loading branch information
maratal committed Aug 11, 2024
1 parent 7fceb3e commit 770ddfd
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
4 changes: 4 additions & 0 deletions Source/ARTRealtime.m
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,10 @@ - (void)connect {
}

- (void)_connect {
if (self.connection.state_nosync == ARTRealtimeConnecting) {
ARTLogError(self.logger, @"R:%p Ignoring new connection attempt - already in the CONNECTING state.", self);
return;
}
if (self.connection.state_nosync == ARTRealtimeClosing) {
// New connection
_transport = nil;
Expand Down
2 changes: 1 addition & 1 deletion Source/ARTTypes.m
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ - (instancetype)initWithCurrent:(ARTRealtimeConnectionState)current previous:(AR
}

- (NSString *)description {
return [NSString stringWithFormat:@"%@ - \n\t current: %@; \n\t previous: %@; \n\t reason: %@; \n\t retryIn: %f; \n\t retryAttempt: %@; \n", [super description], ARTRealtimeConnectionStateToStr(_current), ARTRealtimeConnectionStateToStr(_previous), _reason, _retryIn, _retryAttempt];
return [NSString stringWithFormat:@"%@ - \n\t event: %@; \n\t current: %@; \n\t previous: %@; \n\t reason: %@; \n\t retryIn: %f; \n\t retryAttempt: %@; \n", [super description], ARTRealtimeConnectionEventToStr(_event), ARTRealtimeConnectionStateToStr(_current), ARTRealtimeConnectionStateToStr(_previous), _reason, _retryIn, _retryAttempt];
}

- (void)setRetryIn:(NSTimeInterval)retryIn {
Expand Down
30 changes: 30 additions & 0 deletions Test/Tests/RealtimeClientConnectionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,36 @@ class RealtimeClientConnectionTests: XCTestCase {
}
}

func test__021b__Connection__event_emitter__should_never_emit_a_ConnectionState_event_for_a_state_equal_to_the_previous_state_CONNECTING() throws {
let test = Test()
let options = try AblyTests.commonAppSetup(for: test)
options.autoConnect = false

let client = ARTRealtime(options: options)
defer { client.dispose(); client.close() }

var events: [ARTRealtimeConnectionState] = []

waitUntil(timeout: testTimeout) { done in
client.connection.on { stateChange in
XCTAssertNil(stateChange.reason)
let state = stateChange.current
switch state {
case .connecting:
events += [state]
case .connected:
done()
default:
break
}
}
client.connect()
client.connect()
}
XCTAssertEqual(events.count, 1)
XCTAssertEqual(events[0].rawValue, ARTRealtimeConnectionState.connecting.rawValue, "Should be CONNECTING state")
}

// RTN4b
func test__022__Connection__event_emitter__should_emit_states_on_a_new_connection() throws {
let test = Test()
Expand Down

0 comments on commit 770ddfd

Please sign in to comment.