queue: Round-trip events through orjson in tests.

This ensures that tests don't rely on non-JSON'able datatypes being
able to be put in an event.
This commit is contained in:
Alex Vandiver
2025-03-06 16:46:42 +00:00
committed by Tim Abbott
parent b3862c5008
commit 1eb4b65b5a
2 changed files with 20 additions and 2 deletions

View File

@@ -225,4 +225,15 @@ def send_event_rollback_unsafe(
def send_event_on_commit(
realm: Realm, event: Mapping[str, Any], users: Iterable[int] | Iterable[Mapping[str, Any]]
) -> None:
if not settings.USING_RABBITMQ:
# In tests, round-trip the event through JSON, as happens with
# RabbitMQ. zerver.lib.queue also enforces this, but the
# on-commit nature of the event sending makes it difficult to
# trace which event was at fault -- so we also check it
# immediately, here.
try:
event = orjson.loads(orjson.dumps(event))
except TypeError:
print(event)
raise
transaction.on_commit(lambda: send_event_rollback_unsafe(realm, event, users))