|
1 | 1 | import asyncio |
| 2 | +import logging |
2 | 3 | import unittest |
| 4 | +from unittest.mock import MagicMock |
3 | 5 |
|
4 | 6 | from slack_sdk.socket_mode.aiohttp import SocketModeClient |
5 | 7 | from slack_sdk.web.async_client import AsyncWebClient |
@@ -31,6 +33,41 @@ async def test_init_close(self): |
31 | 33 | finally: |
32 | 34 | await client.close() |
33 | 35 |
|
| 36 | + @async_test |
| 37 | + async def test_connect_returns_when_closed(self): |
| 38 | + # Regression test for #1913: connect() must not loop forever once the client is closed. |
| 39 | + client = SocketModeClient( |
| 40 | + app_token="xapp-A111-222-xyz", |
| 41 | + web_client=self.web_client, |
| 42 | + auto_reconnect_enabled=False, |
| 43 | + ping_interval=0.01, |
| 44 | + ) |
| 45 | + client.wss_uri = "ws://localhost:8888/link" |
| 46 | + await client.close() |
| 47 | + await asyncio.wait_for(client.connect(), timeout=1.0) |
| 48 | + self.assertTrue(client.closed) |
| 49 | + |
| 50 | + @async_test |
| 51 | + async def test_connect_returns_when_exception_raised_after_close(self): |
| 52 | + client = SocketModeClient( |
| 53 | + app_token="xapp-A111-222-xyz", |
| 54 | + web_client=self.web_client, |
| 55 | + auto_reconnect_enabled=False, |
| 56 | + ping_interval=0.01, |
| 57 | + ) |
| 58 | + client.logger = MagicMock() |
| 59 | + client.logger.level = logging.DEBUG |
| 60 | + |
| 61 | + async def close_then_raise(*args, **kwargs): |
| 62 | + await client.close() |
| 63 | + raise RuntimeError("Session is closed") |
| 64 | + |
| 65 | + client.issue_new_wss_url = close_then_raise |
| 66 | + |
| 67 | + await asyncio.wait_for(client.connect(), timeout=1.0) |
| 68 | + self.assertTrue(client.closed) |
| 69 | + client.logger.exception.assert_not_called() |
| 70 | + |
34 | 71 | @async_test |
35 | 72 | async def test_init_with_loop(self): |
36 | 73 | client = SocketModeClient( |
|
0 commit comments