|
| 1 | +import asyncio |
| 2 | +import collections |
| 3 | +import logging |
| 4 | +import os |
| 5 | +import threading |
| 6 | +import time |
| 7 | +import unittest |
| 8 | + |
| 9 | +import pytest |
| 10 | + |
| 11 | +from integration_tests.env_variable_names import SLACK_SDK_TEST_CLASSIC_APP_BOT_TOKEN |
| 12 | +from integration_tests.helpers import async_test, is_not_specified |
| 13 | +from slack import RTMClient, WebClient |
| 14 | + |
| 15 | + |
| 16 | +class TestRTMClient(unittest.TestCase): |
| 17 | + """Runs integration tests with real Slack API |
| 18 | +
|
| 19 | + https://github.com/slackapi/python-slackclient/issues/701 |
| 20 | + """ |
| 21 | + |
| 22 | + def setUp(self): |
| 23 | + self.logger = logging.getLogger(__name__) |
| 24 | + self.bot_token = os.environ[SLACK_SDK_TEST_CLASSIC_APP_BOT_TOKEN] |
| 25 | + |
| 26 | + def tearDown(self): |
| 27 | + # Reset the decorators by @RTMClient.run_on |
| 28 | + RTMClient._callbacks = collections.defaultdict(list) |
| 29 | + |
| 30 | + # @pytest.mark.skipif(condition=is_not_specified(), reason="to avoid rate_limited errors") |
| 31 | + @pytest.mark.skip() |
| 32 | + def test_receiving_all_messages(self): |
| 33 | + self.rtm_client = RTMClient(token=self.bot_token, loop=asyncio.new_event_loop()) |
| 34 | + self.web_client = WebClient(token=self.bot_token) |
| 35 | + |
| 36 | + self.call_count = 0 |
| 37 | + |
| 38 | + @RTMClient.run_on(event="message") |
| 39 | + def send_reply(**payload): |
| 40 | + self.logger.debug(payload) |
| 41 | + web_client, data = payload["web_client"], payload["data"] |
| 42 | + web_client.reactions_add(channel=data["channel"], timestamp=data["ts"], name="eyes") |
| 43 | + self.call_count += 1 |
| 44 | + |
| 45 | + def connect(): |
| 46 | + self.logger.debug("Starting RTM Client...") |
| 47 | + self.rtm_client.start() |
| 48 | + |
| 49 | + rtm = threading.Thread(target=connect) |
| 50 | + rtm.setDaemon(True) |
| 51 | + |
| 52 | + rtm.start() |
| 53 | + time.sleep(3) |
| 54 | + |
| 55 | + total_num = 10 |
| 56 | + |
| 57 | + sender_completion = [] |
| 58 | + |
| 59 | + def sent_bulk_message(): |
| 60 | + for i in range(total_num): |
| 61 | + text = f"Sent by <https://slack.dev/python-slackclient/|python-slackclient>! ({i})" |
| 62 | + self.web_client.chat_postMessage(channel="#random", text=text) |
| 63 | + time.sleep(0.1) |
| 64 | + sender_completion.append(True) |
| 65 | + |
| 66 | + num_of_senders = 3 |
| 67 | + senders = [] |
| 68 | + for sender_num in range(num_of_senders): |
| 69 | + sender = threading.Thread(target=sent_bulk_message) |
| 70 | + sender.setDaemon(True) |
| 71 | + sender.start() |
| 72 | + senders.append(sender) |
| 73 | + |
| 74 | + while len(sender_completion) < num_of_senders: |
| 75 | + time.sleep(1) |
| 76 | + |
| 77 | + expected_call_count = total_num * num_of_senders |
| 78 | + wait_seconds = 0 |
| 79 | + max_wait = 20 |
| 80 | + while self.call_count < expected_call_count and wait_seconds < max_wait: |
| 81 | + time.sleep(1) |
| 82 | + wait_seconds += 1 |
| 83 | + |
| 84 | + self.assertEqual(total_num * num_of_senders, self.call_count, "The RTM handler failed") |
| 85 | + |
| 86 | + @pytest.mark.skipif(condition=is_not_specified(), reason="to avoid rate_limited errors") |
| 87 | + @async_test |
| 88 | + async def test_receiving_all_messages_async(self): |
| 89 | + self.rtm_client = RTMClient(token=self.bot_token, run_async=True) |
| 90 | + self.web_client = WebClient(token=self.bot_token, run_async=False) |
| 91 | + |
| 92 | + self.call_count = 0 |
| 93 | + |
| 94 | + @RTMClient.run_on(event="message") |
| 95 | + async def send_reply(**payload): |
| 96 | + self.logger.debug(payload) |
| 97 | + web_client, data = payload["web_client"], payload["data"] |
| 98 | + await web_client.reactions_add(channel=data["channel"], timestamp=data["ts"], name="eyes") |
| 99 | + self.call_count += 1 |
| 100 | + |
| 101 | + # intentionally not waiting here |
| 102 | + self.rtm_client.start() |
| 103 | + |
| 104 | + await asyncio.sleep(3) |
| 105 | + |
| 106 | + total_num = 10 |
| 107 | + |
| 108 | + sender_completion = [] |
| 109 | + |
| 110 | + def sent_bulk_message(): |
| 111 | + for i in range(total_num): |
| 112 | + text = f"Sent by <https://slack.dev/python-slackclient/|python-slackclient>! ({i})" |
| 113 | + self.web_client.chat_postMessage(channel="#random", text=text) |
| 114 | + time.sleep(0.1) |
| 115 | + sender_completion.append(True) |
| 116 | + |
| 117 | + num_of_senders = 3 |
| 118 | + senders = [] |
| 119 | + for sender_num in range(num_of_senders): |
| 120 | + sender = threading.Thread(target=sent_bulk_message) |
| 121 | + sender.setDaemon(True) |
| 122 | + sender.start() |
| 123 | + senders.append(sender) |
| 124 | + |
| 125 | + while len(sender_completion) < num_of_senders: |
| 126 | + await asyncio.sleep(1) |
| 127 | + |
| 128 | + expected_call_count = total_num * num_of_senders |
| 129 | + wait_seconds = 0 |
| 130 | + max_wait = 20 |
| 131 | + while self.call_count < expected_call_count and wait_seconds < max_wait: |
| 132 | + await asyncio.sleep(1) |
| 133 | + wait_seconds += 1 |
| 134 | + |
| 135 | + self.assertEqual(total_num * num_of_senders, self.call_count, "The RTM handler failed") |
0 commit comments