When I try using an event in my openedx-platform tests, I found that throwing an exception in an event handler has no effect, and the exception is silently ignored.
This is because openedx-events uses send_robust by default even in tests, which suppresses exceptions thrown in the event receivers. This seems to go against the original intention of #29 which said:
This PR changes which send method to use when sending an event, so they don't fail during runtime. Instead, they will be allowed to fail in a testing/dev environment.
I found that I can work around this by adding EventsIsolationMixin to my test class, and adding this setup code:
@classmethod
def setUpClass(cls):
"""Test setup"""
super().setUpClass()
# By default, errors thrown in signal handlers get suppressed. We want to see them though!
cls.allow_send_events_failure(*(s.event_type for s in OpenEdxPublicSignal.all_events()))
But I think that this shouldn't be necessary - suppressing exceptions during tests is confusing and unexpected behavior that takes a while to debug. Also, the code shown above "leaks" because it affects any other tests that will run afterward. The events API provides a way to "allow send events failure" but it doesn't provide a way to undo that.
I recommend that you just scrap all of that and add a setting called SUPPRESS_EVENT_EXCEPTIONS or EVENT_SENDING_ROBUST_BY_DEFAULT which is True in envs/production.py and otherwise defaults to False.
Other issues
-
"allow send events failure" is very ambiguous. Does that mean "allow events to fail, by ignoring their exceptions", or does that mean "allow exceptions (failures) to be reported" ? Calling this "suppress exceptions" would be way more clear.
-
I'm not the only one confused. The documentation of OpenEdxPublicSignal.allow_send_event_failure says:
|
def allow_send_event_failure(self): |
|
""" |
|
Allow Django signal to fail. Meaning, uses send_robust instead of send. |
|
|
|
More information on send_robust in the Django official documentation. |
|
""" |
|
self._allow_send_event_failure = True |
But this is the opposite of what it actually does!
|
if self._allow_send_event_failure or settings.DEBUG or not send_robust: |
|
return super().send(sender=None, **kwargs) |
|
|
|
responses = super().send_robust(sender=None, **kwargs) |
When I try using an event in my openedx-platform tests, I found that throwing an exception in an event handler has no effect, and the exception is silently ignored.
This is because openedx-events uses
send_robustby default even in tests, which suppresses exceptions thrown in the event receivers. This seems to go against the original intention of #29 which said:I found that I can work around this by adding
EventsIsolationMixinto my test class, and adding this setup code:But I think that this shouldn't be necessary - suppressing exceptions during tests is confusing and unexpected behavior that takes a while to debug. Also, the code shown above "leaks" because it affects any other tests that will run afterward. The events API provides a way to "allow send events failure" but it doesn't provide a way to undo that.
I recommend that you just scrap all of that and add a setting called
SUPPRESS_EVENT_EXCEPTIONSorEVENT_SENDING_ROBUST_BY_DEFAULTwhich is True inenvs/production.pyand otherwise defaults toFalse.Other issues
"allow send events failure" is very ambiguous. Does that mean "allow events to fail, by ignoring their exceptions", or does that mean "allow exceptions (failures) to be reported" ? Calling this "suppress exceptions" would be way more clear.
I'm not the only one confused. The documentation of
OpenEdxPublicSignal.allow_send_event_failuresays:openedx-events/openedx_events/tooling.py
Lines 267 to 273 in 34d3fce
But this is the opposite of what it actually does!
openedx-events/openedx_events/tooling.py
Lines 168 to 171 in 34d3fce