Skip to content

Commit

Permalink
Merge pull request #493 from ably/cm-notify-on-transport-deactivation
Browse files Browse the repository at this point in the history
fix(ConnectionManager): notify state upon transport deactiviation
  • Loading branch information
owenpearson authored May 5, 2023
2 parents 04eb1c3 + aa97420 commit 373214b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion ably/realtime/connectionmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ def on_heartbeat(self, id: Optional[str]) -> None:

def deactivate_transport(self, reason: Optional[AblyException] = None):
self.transport = None
self.enact_state_change(ConnectionState.DISCONNECTED, reason)
self.notify_state(ConnectionState.DISCONNECTED, reason)

def request_state(self, state: ConnectionState, force=False) -> None:
log.info(f'ConnectionManager.request_state(): state = {state}')
Expand Down
27 changes: 27 additions & 0 deletions test/ably/realtime/realtimeconnection_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,3 +370,30 @@ async def test_connection_client_id_query_params(self):
assert ably.auth.client_id == client_id

await ably.close()

async def test_lost_connection_lifecycle(self):
ably = await TestApp.get_ably_realtime(realtime_request_timeout=2000, disconnected_retry_timeout=2000)

# when client connectivity is lost, the transport will become aware of a connectivity issue
# when it stops seeing activity from realtime within maxIdleInterval, therefore setting the max idle
# interval arbitrarily low will simulate client behaviour when connectivity is lost.
def on_transport_pending(transport):
original_on_protocol_message = transport.on_protocol_message

async def on_protocol_message(msg):
if msg["action"] == ProtocolMessageAction.CONNECTED:
msg["connectionDetails"]["maxIdleInterval"] = 1000

await original_on_protocol_message(msg)

transport.on_protocol_message = on_protocol_message

ably.connection.connection_manager.once('transport.pending', on_transport_pending)

# should transition to disconnected due to lack of activity from realtime
await ably.connection.once_async(ConnectionState.DISCONNECTED)

# should re-establish connection after disconnected_retry_timeout
await ably.connection.once_async(ConnectionState.CONNECTED)

await ably.close()

0 comments on commit 373214b

Please sign in to comment.