Skip to content

Event handler errors are suppressed by default in tests #569

Description

@bradenmacdonald

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

  1. "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.

  2. 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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions