Skip to content

Commit c384190

Browse files
authored
Merge pull request #7717 from RasaHQ/event-broker-async-warning
fix EventBroker async warning
2 parents 4d5f7cc + 4140221 commit c384190

File tree

5 files changed

+61
-5
lines changed

5 files changed

+61
-5
lines changed

changelog/7717.bugfix.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix wrong warning `The method 'EventBroker.close' was changed to be asynchronous` when
2+
the `EventBroker.close` was actually asynchronous.

rasa/core/exporter.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ async def publish_events(self) -> int:
8787
logger.exception(e)
8888
raise PublishingError(current_timestamp)
8989

90-
if asyncio.iscoroutinefunction(self.event_broker.close):
90+
if not asyncio.iscoroutinefunction(self.event_broker.close):
9191
rasa.shared.utils.io.raise_deprecation_warning(
92-
f"The method '{EventBroker.__name__}.{EventBroker.close.__name__} was "
92+
f"The method '{EventBroker.__name__}.{EventBroker.close.__name__}' was "
9393
f"changed to be asynchronous. Please adapt your custom event broker "
9494
f"accordingly. Support for synchronous implementations will be removed "
9595
f"in Rasa Open Source 3.0.0."

rasa/core/run.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -307,9 +307,9 @@ async def close_resources(app: Sanic, _: AbstractEventLoop) -> None:
307307

308308
event_broker = current_agent.tracker_store.event_broker
309309
if event_broker:
310-
if asyncio.iscoroutinefunction(event_broker.close):
310+
if not asyncio.iscoroutinefunction(event_broker.close):
311311
rasa.shared.utils.io.raise_deprecation_warning(
312-
f"The method '{EventBroker.__name__}.{EventBroker.close.__name__} was "
312+
f"The method '{EventBroker.__name__}.{EventBroker.close.__name__}' was "
313313
f"changed to be asynchronous. Please adapt your custom event broker "
314314
f"accordingly. Support for synchronous implementations will be removed "
315315
f"in Rasa Open Source 3.0.0."

tests/core/test_exporter.py

+28-1
Original file line numberDiff line numberDiff line change
@@ -288,5 +288,32 @@ async def test_publishing_error():
288288

289289
# run the export function
290290
with pytest.raises(PublishingError):
291-
# noinspection PyProtectedMember
291+
await exporter.publish_events()
292+
293+
294+
async def test_closing_broker():
295+
exporter = MockExporter(event_broker=SQLEventBroker())
296+
297+
# noinspection PyProtectedMember
298+
exporter._fetch_events_within_time_range = Mock(return_value=[])
299+
300+
# run the export function
301+
with pytest.warns(None) as warnings:
302+
await exporter.publish_events()
303+
304+
assert len(warnings) == 0
305+
306+
307+
async def test_closing_broker_sync():
308+
class TestBroker(SQLEventBroker):
309+
def close(self) -> None:
310+
pass
311+
312+
exporter = MockExporter(event_broker=TestBroker())
313+
314+
# noinspection PyProtectedMember
315+
exporter._fetch_events_within_time_range = Mock(return_value=[])
316+
317+
# run the export function
318+
with pytest.warns(FutureWarning):
292319
await exporter.publish_events()

tests/core/test_run.py

+27
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from unittest.mock import Mock
2+
13
import pytest
24
from typing import Text
35

@@ -7,6 +9,7 @@
79
from asyncio import AbstractEventLoop
810
from pathlib import Path
911
from rasa.core import run, interpreter, policies
12+
from rasa.core.brokers.sql import SQLEventBroker
1013
from rasa.core.utils import AvailableEndpoints
1114

1215
CREDENTIALS_FILE = "examples/moodbot/credentials.yml"
@@ -82,3 +85,27 @@ async def test_load_agent_on_start_with_bad_model_file(
8285
assert isinstance(agent.interpreter, rasa.shared.nlu.interpreter.RegexInterpreter)
8386
assert agent.policy_ensemble is None
8487
assert isinstance(agent.domain, rasa.shared.core.domain.Domain)
88+
89+
90+
async def test_close_resources(loop: AbstractEventLoop):
91+
broker = SQLEventBroker()
92+
app = Mock()
93+
app.agent.tracker_store.event_broker = broker
94+
95+
with pytest.warns(None) as warnings:
96+
await run.close_resources(app, loop)
97+
98+
assert len(warnings) == 0
99+
100+
101+
async def test_close_resources_with_sync(loop: AbstractEventLoop):
102+
class TestBroker(SQLEventBroker):
103+
def close(self) -> None:
104+
pass
105+
106+
broker = TestBroker()
107+
app = Mock()
108+
app.agent.tracker_store.event_broker = broker
109+
110+
with pytest.warns(FutureWarning):
111+
await run.close_resources(app, loop)

0 commit comments

Comments
 (0)