forked from slackapi/python-slack-sdk
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_rtm_client.py
339 lines (284 loc) · 12.6 KB
/
test_rtm_client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
import asyncio
import collections
import logging
import os
import threading
import time
import traceback
import unittest
import psutil
import pytest
from integration_tests.env_variable_names import SLACK_SDK_TEST_CLASSIC_APP_BOT_TOKEN, \
SLACK_SDK_TEST_RTM_TEST_CHANNEL_ID
from integration_tests.helpers import async_test
from slack import RTMClient, WebClient
class TestRTMClient(unittest.TestCase):
"""Runs integration tests with real Slack API"""
def setUp(self):
if not hasattr(self, "logger"):
self.logger = logging.getLogger(__name__)
self.channel_id = os.environ[SLACK_SDK_TEST_RTM_TEST_CHANNEL_ID]
self.bot_token = os.environ[SLACK_SDK_TEST_CLASSIC_APP_BOT_TOKEN]
if not hasattr(self, "cpu_monitor") or not TestRTMClient.cpu_monitor.is_alive():
def run_cpu_monitor(self):
self.logger.debug("Starting CPU monitor in another thread...")
TestRTMClient.cpu_usage = 0
while True:
p = psutil.Process(os.getpid())
current_cpu_usage: float = p.cpu_percent(interval=0.5)
self.logger.debug(current_cpu_usage)
if current_cpu_usage > TestRTMClient.cpu_usage:
TestRTMClient.cpu_usage = current_cpu_usage
TestRTMClient.cpu_monitor = threading.Thread(target=run_cpu_monitor, args=[self])
TestRTMClient.cpu_monitor.setDaemon(True)
TestRTMClient.cpu_monitor.start()
def tearDown(self):
# Reset the decorators by @RTMClient.run_on
RTMClient._callbacks = collections.defaultdict(list)
# Stop the Client
if not self.rtm_client._stopped:
self.rtm_client.stop()
def test_basic_operations(self):
self.sent_text: str = None
self.rtm_client = RTMClient(
token=self.bot_token,
run_async=False,
loop=asyncio.new_event_loop(), # TODO: this doesn't work without this
)
self.web_client = WebClient(
token=self.bot_token,
run_async=False,
loop=asyncio.new_event_loop(), # TODO: this doesn't work without this
)
@RTMClient.run_on(event="message")
def send_reply(**payload):
self.logger.debug(payload)
self.sent_text = payload['data']['text']
def connect():
self.logger.debug("Starting RTM Client...")
self.rtm_client.start()
t = threading.Thread(target=connect)
t.setDaemon(True)
t.start()
try:
self.assertIsNone(self.sent_text)
time.sleep(5)
text = "This message was sent by <https://slack.dev/python-slackclient/|python-slackclient>! (test_basic_operations)"
new_message = self.web_client.chat_postMessage(channel=self.channel_id, text=text)
self.assertFalse("error" in new_message)
time.sleep(5)
self.assertEqual(self.sent_text, text)
finally:
t.join(.3)
@async_test
async def test_basic_operations_async(self):
self.sent_text: str = None
self.rtm_client = RTMClient(token=self.bot_token, run_async=True)
self.async_web_client = WebClient(token=self.bot_token, run_async=True)
@RTMClient.run_on(event="message")
async def send_reply(**payload):
self.logger.debug(payload)
self.sent_text = payload['data']['text']
# intentionally not waiting here
self.rtm_client.start()
self.assertIsNone(self.sent_text)
await asyncio.sleep(5)
text = "This message was sent by <https://slack.dev/python-slackclient/|python-slackclient>! (test_basic_operations_async)"
new_message = await self.async_web_client.chat_postMessage(
channel="CHE2DUW5V",
text=text
)
self.assertFalse("error" in new_message)
await asyncio.sleep(5)
self.assertEqual(self.sent_text, text)
# TODO: Fix this issue
@pytest.mark.skip("This needs to be fixed - https://github.com/slackapi/python-slackclient/issues/569")
def test_cpu_usage(self):
self.rtm_client = RTMClient(
token=self.bot_token,
run_async=False,
loop=asyncio.new_event_loop())
self.web_client = WebClient(
token=self.bot_token,
run_async=False,
loop=asyncio.new_event_loop())
self.call_count = 0
TestRTMClient.cpu_usage = 0
@RTMClient.run_on(event="message")
def send_reply(**payload):
self.logger.debug(payload)
event = payload["data"]
if "text" in event:
if not str(event["text"]).startswith("Current CPU usage:"):
web_client = payload["web_client"]
for i in range(0, 3):
new_message = web_client.chat_postMessage(
channel=event["channel"],
text=f"Current CPU usage: {TestRTMClient.cpu_usage} % (test_cpu_usage)"
)
self.logger.debug(new_message)
self.call_count += 1
def connect():
self.logger.debug("Starting RTM Client...")
self.rtm_client.start()
rtm = threading.Thread(target=connect)
rtm.setDaemon(True)
rtm.start()
time.sleep(5)
text = "This message was sent by <https://slack.dev/python-slackclient/|python-slackclient>! (test_cpu_usage)"
new_message = self.web_client.chat_postMessage(channel=self.channel_id, text=text)
self.assertFalse("error" in new_message)
time.sleep(5)
self.assertLess(TestRTMClient.cpu_usage, 30, "Too high CPU usage detected")
self.assertEqual(self.call_count, 3, "The RTM handler failed")
# > self.assertLess(TestRTMClient.cpu_usage, 30, "Too high CPU usage detected")
# E AssertionError: 100.2 not less than 30 : Too high CPU usage detected
#
# integration_tests/rtm/test_rtm_client.py:160: AssertionError
@async_test
async def test_cpu_usage_async(self):
self.rtm_client = RTMClient(token=self.bot_token, run_async=True)
self.web_client = WebClient(token=self.bot_token, run_async=True)
self.call_count = 0
TestRTMClient.cpu_usage = 0
@RTMClient.run_on(event="message")
async def send_reply_async(**payload):
self.logger.debug(payload)
event = payload["data"]
if "text" in event:
if not str(event["text"]).startswith("Current CPU usage:"):
web_client = payload["web_client"]
for i in range(0, 3):
new_message = await web_client.chat_postMessage(
channel=event["channel"],
text=f"Current CPU usage: {TestRTMClient.cpu_usage} % (test_cpu_usage_async)"
)
self.logger.debug(new_message)
self.call_count += 1
# intentionally not waiting here
self.rtm_client.start()
await asyncio.sleep(5)
text = "This message was sent by <https://slack.dev/python-slackclient/|python-slackclient>! (test_cpu_usage_async)"
new_message = await self.web_client.chat_postMessage(channel=self.channel_id, text=text)
self.assertFalse("error" in new_message)
await asyncio.sleep(5)
self.assertLess(TestRTMClient.cpu_usage, 30, "Too high CPU usage detected")
self.assertEqual(self.call_count, 3, "The RTM handler failed")
# -----------------------
# Issue #631
# https://github.com/slackapi/python-slackclient/issues/631
# -----------------------
@pytest.mark.skip() # TODO: this issue needs to be fixed
def test_issue_631_sharing_event_loop(self):
self.success = None
self.text = "This message was sent to verify issue #631"
self.loop = asyncio.get_event_loop()
self.rtm_client = RTMClient(
token=self.bot_token,
run_async=False, # even though run_async=False, handlers for RTM events can be a coroutine
loop=self.loop,
)
# @RTMClient.run_on(event="message")
# def send_reply(**payload):
# self.logger.debug(payload)
# data = payload['data']
# web_client = payload['web_client']
# web_client._event_loop = self.loop
# # Maybe you will also need the following line uncommented
# # web_client.run_async = True
#
# if self.text in data['text']:
# channel_id = data['channel']
# thread_ts = data['ts']
# try:
# self.success = web_client.chat_postMessage(
# channel=channel_id,
# text="Thanks!",
# thread_ts=thread_ts
# )
# except Exception as e:
# # slack.rtm.client:client.py:446 When calling '#send_reply()'
# # in the 'test_rtm_client' module the following error was raised: This event loop is already running
# self.logger.error(traceback.format_exc())
# raise e
# Solution (1) for #631
@RTMClient.run_on(event="message")
# even though run_async=False, handlers for RTM events can be a coroutine
async def send_reply(**payload):
self.logger.debug(payload)
data = payload['data']
web_client = payload['web_client']
web_client._event_loop = self.loop
try:
if "text" in data and self.text in data["text"]:
channel_id = data['channel']
thread_ts = data['ts']
self.success = await web_client.chat_postMessage(
channel=channel_id,
text="Thanks!",
thread_ts=thread_ts
)
except Exception as e:
self.logger.error(traceback.format_exc())
raise e
def connect():
self.logger.debug("Starting RTM Client...")
self.rtm_client.start()
t = threading.Thread(target=connect)
t.setDaemon(True)
t.start()
try:
self.assertIsNone(self.success)
time.sleep(5)
self.web_client = WebClient(
token=self.bot_token,
run_async=False,
loop=asyncio.new_event_loop(), # TODO: this doesn't work without this
)
new_message = self.web_client.chat_postMessage(channel=self.channel_id, text=self.text)
self.assertFalse("error" in new_message)
time.sleep(5)
self.assertIsNotNone(self.success)
finally:
t.join(.3)
# Solution (2) for #631
@pytest.mark.skip("this is just for your reference")
@async_test
async def test_issue_631_sharing_event_loop_async(self):
self.loop = asyncio.get_event_loop()
self.success = None
self.text = "This message was sent to verify issue #631"
self.rtm_client = RTMClient(
token=self.bot_token,
run_async=True, # To turn this on, the test method needs to be an async function + @async_test decorator
)
@RTMClient.run_on(event="message")
async def send_reply(**payload):
self.logger.debug(payload)
data = payload['data']
web_client = payload['web_client']
web_client._event_loop = self.loop
try:
if "text" in data and self.text in data["text"]:
channel_id = data['channel']
thread_ts = data['ts']
self.success = await web_client.chat_postMessage(
channel=channel_id,
text="Thanks!",
thread_ts=thread_ts
)
except Exception as e:
self.logger.error(traceback.format_exc())
raise e
# intentionally not waiting here
self.rtm_client.start()
self.assertIsNone(self.success)
await asyncio.sleep(5)
self.web_client = WebClient(
token=self.bot_token,
run_async=True, # all need to be async here
)
new_message = await self.web_client.chat_postMessage(channel=self.channel_id, text=self.text)
self.assertFalse("error" in new_message)
await asyncio.sleep(5)
self.assertIsNotNone(self.success)