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

Add Driver.disconnect function #80

Merged
merged 6 commits into from
Mar 21, 2021
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
5 changes: 4 additions & 1 deletion src/mattermostdriver/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ async def my_event_handler(message):
loop.run_until_complete(self.websocket.connect(event_handler))
return loop

def disconnect(self):
"""Disconnects the driver from the server, stopping the websocket event loop."""
self.websocket.disconnect()

def login(self):
"""
Logs the user in.
Expand Down Expand Up @@ -418,4 +422,3 @@ def integration_actions(self):
:return: Instance of :class:`~endpoints.integration_actions.IntegrationActions`
"""
return IntegrationActions(self.client)

9 changes: 8 additions & 1 deletion src/mattermostdriver/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def __init__(self, options, token):
if options['debug']:
log.setLevel(logging.DEBUG)
self._token = token
self._alive = False

async def connect(self, event_handler):
"""
Expand Down Expand Up @@ -56,7 +57,8 @@ async def _start_loop(self, websocket, event_handler):
forcing us to reconnect.
"""
log.debug('Starting websocket loop')
while True:
self._alive = True
while self._alive:
try:
await asyncio.wait_for(
self._wait_for_message(websocket, event_handler),
Expand All @@ -67,6 +69,11 @@ async def _start_loop(self, websocket, event_handler):
log.debug("Sending heartbeat...")
continue

def disconnect(self):
"""Sets `self._alive` to False so the loop in `self._start_loop` will finish."""
log.info("Disconnecting websocket")
self._alive = False

async def _authenticate_websocket(self, websocket, event_handler):
"""
Sends a authentication challenge over a websocket.
Expand Down