Skip to content

Commit

Permalink
Raise an error when trying to run a normal function with run_async=True
Browse files Browse the repository at this point in the history
  • Loading branch information
seratch committed May 15, 2020
1 parent c8866d4 commit 227f949
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
7 changes: 7 additions & 0 deletions slack/rtm/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,13 @@ async def _dispatch_event(self, event, data=None):
rtm_client=self, web_client=self._web_client, data=data
)
else:
if self.run_async is True:
raise client_err.SlackRequestError(
f'The callback "{callback.__name__}" is NOT a coroutine. '
"Running such with run_async=True is unsupported. "
"Consider adding async/await to the method "
"or going with run_async=False if your app is not really non-blocking."
)
payload = {
"rtm_client": self,
"web_client": self._web_client,
Expand Down
45 changes: 45 additions & 0 deletions tests/rtm/test_rtm_client_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import slack
import slack.errors as e
from tests.helpers import async_test
from tests.rtm.mock_web_api_server import setup_mock_web_api_server, cleanup_mock_web_api_server


Expand Down Expand Up @@ -240,3 +241,47 @@ def assert_on_close(**payload):

self.client.start()
self.assertTrue(self.called)

@async_test
async def test_run_async_valid(self):
client = slack.RTMClient(
token="xoxb-valid",
base_url="http://localhost:8765",
run_async=True,
)
client._web_client = slack.WebClient(
token="xoxb-valid",
base_url="http://localhost:8888",
run_async=True,
)
self.called = False

@slack.RTMClient.run_on(event="open")
async def handle_open_event(**payload):
self.called = True

client.start() # intentionally no await here
await asyncio.sleep(3)
self.assertTrue(self.called)

@async_test
async def test_run_async_invalid(self):
client = slack.RTMClient(
token="xoxb-valid",
base_url="http://localhost:8765",
run_async=True,
)
client._web_client = slack.WebClient(
token="xoxb-valid",
base_url="http://localhost:8888",
run_async=True,
)
self.called = False

@slack.RTMClient.run_on(event="open")
def handle_open_event(**payload):
self.called = True

client.start() # intentionally no await here
await asyncio.sleep(3)
self.assertFalse(self.called)

0 comments on commit 227f949

Please sign in to comment.