Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check if port is int and raise exception #8575

Merged
merged 17 commits into from
May 17, 2021

Conversation

jppgomes
Copy link
Contributor

@jppgomes jppgomes commented Apr 29, 2021

Proposed changes:

Status (please check what you already did):

  • added some tests for the functionality
  • updated the documentation
  • updated the changelog (please check changelog for instructions)
  • reformat files using black (please check Readme for instructions)

@sara-tagger
Copy link
Collaborator

Thanks for submitting a pull request 🚀 @ArjaanBuijk will take a look at it as soon as possible ✨

@ArjaanBuijk
Copy link
Contributor

@ancalita ,
Would you be able to review this update?

@ArjaanBuijk ArjaanBuijk removed their request for review April 30, 2021 15:08
Copy link
Member

@ancalita ancalita left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution! 🚀
In addition to adding a unit test to reproduce the Sentry error which checks that the added code catches it and raises RasaException, please also add a changelog entry at rasa/changelog by following the README instructions at that location.

@@ -169,6 +170,10 @@ async def _connect(self) -> aio_pika.RobustConnection:
last_exception = None
for _ in range(self._connection_attempts):
try:

if not isinstance(self.port, int):
raise RasaException("Port has to be a integer.")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the RasaException can be raised a bit earlier - for example in the constructor L75 where you can wrap the integer casting in a try-except block:

try:
     self.port = int(port)
except ValueError as e:
     raise RasaException("Port could not be converted to integer.") from e

You can try this by adding a new unit test at rasa/tests/core/test_broker.py and reproducing the Sentry error behaviour to see if this try-except block catches it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, thanks for the tips, would it be something like this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was fast ⚡
It's looking in the right direction - I would also add an extra assertion after pytest.raises:

with pytest.raises(RasaException) as e:
     # to do

assert "Port could not be converted to integer." in str(e.value)

@CLAassistant
Copy link

CLAassistant commented May 4, 2021

CLA assistant check
All committers have signed the CLA.

@@ -310,6 +311,23 @@ async def test_no_pika_logs_if_no_debug_mode(caplog: LogCaptureFixture):
for record in caplog.records
)

broker = PikaEventBroker(
"host", "username", "password", retry_delay_in_seconds=1, connection_attempts=1
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not exactly sure what these new lines are for 🤔 If you added by mistake, could you please remove?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jppgomes This also needs to be resolved.

)


def test_raise_exception_port(monkeypatch: MonkeyPatch):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there is no need for MonkeyPatch, this argument can be removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes.. sorry, fixed!!

def test_raise_exception_port(monkeypatch: MonkeyPatch):

with pytest.raises(RasaException):
PikaEventBroker(
Copy link
Member

@ancalita ancalita May 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm aware you cannot see the full stacktrace from Sentry, not sure why it got clipped, but I would actually use exactly what gets called further up in the stacktrace: the EventBroker factory from here.
You can create an instance of EndpointConfig which you can pass as argument to the broker factory:
EndpointConfig(username="username", password="password", type="pika", port="PORT")

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In addition, note of caution - the EventBroker factory is async so you'll need to use the keyword await when calling it. As well as making the test async too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, i'll do this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ancalita can you check if it is right now?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jppgomes Sure, you could also re-request review by clicking on the arrows-in-a-circle icon to the right of the picture avatar under Reviewers section (top right).
Does the test pass?
Please also add this assertion check in the test:

with pytest.raises(RasaException) as e:
     ...

assert "Port could not be converted to integer." in str(e.value)

changelog/8575.bugfix.md Outdated Show resolved Hide resolved
@ancalita
Copy link
Member

ancalita commented May 5, 2021

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you all sign our Contributor License Agreement before we can accept your contribution.
1 out of 2 committers have signed the CLA.

✅ jppgomes
❌ C1300283 Joao Pedro Gomes Cabral Ferreira
C1300283 Joao Pedro Gomes Cabral Ferreira seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

@jppgomes There appears to be an issue with the license agreement not signed - did you commit changes from different devices maybe? You need to make sure that you've set an email address on your machine - this is an useful guide.
After this is done, you might need to redo the commits to attach the email (via rebase).

@jppgomes jppgomes requested a review from ancalita May 5, 2021 13:38
@@ -22,6 +22,7 @@
from rasa.core.brokers.kafka import KafkaEventBroker
from rasa.core.brokers.pika import PikaEventBroker, DEFAULT_QUEUE_NAME
from rasa.core.brokers.sql import SQLEventBroker
from rasa.shared.exceptions import RasaException
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i just noticed there is actually an existing import from the same module in L27 - could you add the , RasaException to L27 and remove duplicate import in L25?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

@jppgomes jppgomes requested a review from ancalita May 6, 2021 13:47
Copy link
Member

@ancalita ancalita left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thank you 💯

@ancalita
Copy link
Member

Hey @jppgomes I see you've run into some flakey CI test failures, it's fine to restart the jobs: go to Details of any failed check and you'll see somewhere on the right side Re-run jobs, select Re-run all jobs and that will restart the process. If you have this issue again, let me know and I can have a look.

@ancalita
Copy link
Member

Hi @jppgomes could you please update branch with the base branch, that's something i can't do - I'll monitor the CI tests and restart if necessary.

@jppgomes
Copy link
Contributor Author

ok @ancalita

@jppgomes
Copy link
Contributor Author

@ancalita it looks like everything is fine now

@ancalita ancalita merged commit 327851b into RasaHQ:main May 17, 2021
@ErickGiffoni ErickGiffoni deleted the 8309/check_if_port_is_int branch August 5, 2021 19:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants