|
| 1 | +import asyncio |
| 2 | +import unittest |
| 3 | +from unittest.mock import patch |
| 4 | + |
| 5 | +from slack_sdk.socket_mode.async_client import AsyncBaseSocketModeClient |
| 6 | +from tests.slack_sdk_async.helpers import async_test |
| 7 | + |
| 8 | + |
| 9 | +class _FakeSocketModeClient(AsyncBaseSocketModeClient): |
| 10 | + def __init__(self): |
| 11 | + self.connect_operation_lock = asyncio.Lock() |
| 12 | + self.trace_enabled = False |
| 13 | + self.wss_uri = "wss://example.com/original" |
| 14 | + self.connected = False |
| 15 | + self.connect_started = asyncio.Event() |
| 16 | + self.allow_connect_to_finish = asyncio.Event() |
| 17 | + |
| 18 | + async def is_connected(self) -> bool: |
| 19 | + return self.connected |
| 20 | + |
| 21 | + async def issue_new_wss_url(self) -> str: |
| 22 | + return "wss://example.com/new" |
| 23 | + |
| 24 | + async def connect(self): |
| 25 | + self.connect_started.set() |
| 26 | + await self.allow_connect_to_finish.wait() |
| 27 | + |
| 28 | + async def disconnect(self): |
| 29 | + pass |
| 30 | + |
| 31 | + async def session_id(self) -> str: |
| 32 | + return "test-session" |
| 33 | + |
| 34 | + |
| 35 | +class TestAsyncBaseSocketModeClient(unittest.TestCase): |
| 36 | + @async_test |
| 37 | + async def test_cancelled_waiter_does_not_release_another_task_lock(self): |
| 38 | + client = _FakeSocketModeClient() |
| 39 | + |
| 40 | + holder = asyncio.ensure_future(client.connect_to_new_endpoint(force=True)) |
| 41 | + await asyncio.wait_for(client.connect_started.wait(), timeout=5) |
| 42 | + self.assertTrue( |
| 43 | + client.connect_operation_lock.locked(), |
| 44 | + "the holder never acquired the connect lock before entering connect()", |
| 45 | + ) |
| 46 | + |
| 47 | + waiter = asyncio.ensure_future(client.connect_to_new_endpoint()) |
| 48 | + await asyncio.sleep(0.1) |
| 49 | + |
| 50 | + waiter.cancel() |
| 51 | + with self.assertRaises(asyncio.CancelledError): |
| 52 | + await waiter |
| 53 | + |
| 54 | + self.assertFalse( |
| 55 | + holder.done(), |
| 56 | + "the holder finished before connect() was allowed to complete", |
| 57 | + ) |
| 58 | + self.assertTrue( |
| 59 | + client.connect_operation_lock.locked(), |
| 60 | + "the cancelled waiter released a lock it never acquired", |
| 61 | + ) |
| 62 | + |
| 63 | + client.allow_connect_to_finish.set() |
| 64 | + await asyncio.wait_for(holder, timeout=5) |
| 65 | + self.assertFalse( |
| 66 | + client.connect_operation_lock.locked(), |
| 67 | + "the holder did not release the connect lock after connect() finished", |
| 68 | + ) |
| 69 | + |
| 70 | + @async_test |
| 71 | + async def test_reconnect_is_skipped_when_the_lock_cannot_be_acquired(self): |
| 72 | + client = _FakeSocketModeClient() |
| 73 | + # connect() must not block here: on the unpatched client the acquire succeeds and |
| 74 | + # this test has to FAIL rather than hang, since a hanging control cannot be read. |
| 75 | + client.allow_connect_to_finish.set() |
| 76 | + |
| 77 | + with patch.object(asyncio, "wait_for", side_effect=asyncio.TimeoutError): |
| 78 | + await client.connect_to_new_endpoint() |
| 79 | + |
| 80 | + self.assertFalse(client.connect_started.is_set(), "reconnected without holding the lock") |
| 81 | + self.assertEqual(client.wss_uri, "wss://example.com/original", "the endpoint was rotated anyway") |
| 82 | + self.assertFalse(client.connect_operation_lock.locked(), "released a lock it never acquired") |
| 83 | + |
| 84 | + @async_test |
| 85 | + async def test_force_reconnects_even_when_the_lock_cannot_be_acquired(self): |
| 86 | + client = _FakeSocketModeClient() |
| 87 | + # connect() must not block: force drives the reconnect even though the acquire timed out. |
| 88 | + client.allow_connect_to_finish.set() |
| 89 | + |
| 90 | + with patch.object(asyncio, "wait_for", side_effect=asyncio.TimeoutError): |
| 91 | + await client.connect_to_new_endpoint(force=True) |
| 92 | + |
| 93 | + self.assertTrue(client.connect_started.is_set(), "force did not reconnect after the acquire timed out") |
| 94 | + self.assertEqual(client.wss_uri, "wss://example.com/new", "force did not rotate the endpoint") |
| 95 | + self.assertFalse( |
| 96 | + client.connect_operation_lock.locked(), |
| 97 | + "held or released a lock it never acquired", |
| 98 | + ) |
| 99 | + |
| 100 | + @async_test |
| 101 | + async def test_reconnect_skipped_when_already_connected(self): |
| 102 | + client = _FakeSocketModeClient() |
| 103 | + client.connected = True |
| 104 | + client.allow_connect_to_finish.set() |
| 105 | + |
| 106 | + await client.connect_to_new_endpoint() |
| 107 | + self.assertFalse(client.connect_started.is_set(), "reconnected while already connected without force") |
| 108 | + self.assertEqual(client.wss_uri, "wss://example.com/original", "rotated the endpoint without force") |
| 109 | + self.assertFalse( |
| 110 | + client.connect_operation_lock.locked(), |
| 111 | + "the connect lock was not released after skipping the reconnect", |
| 112 | + ) |
| 113 | + |
| 114 | + @async_test |
| 115 | + async def test_force_reconnects_even_when_already_connected(self): |
| 116 | + client = _FakeSocketModeClient() |
| 117 | + client.connected = True |
| 118 | + client.allow_connect_to_finish.set() |
| 119 | + |
| 120 | + await client.connect_to_new_endpoint(force=True) |
| 121 | + self.assertTrue(client.connect_started.is_set(), "force did not trigger a reconnect") |
| 122 | + self.assertEqual(client.wss_uri, "wss://example.com/new", "force did not rotate the endpoint") |
| 123 | + self.assertFalse( |
| 124 | + client.connect_operation_lock.locked(), |
| 125 | + "the connect lock was not released after a forced reconnect", |
| 126 | + ) |
0 commit comments