Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

queue: Round-trip events through orjson in tests.

Alex Vandiver1eb4b65b5ab7
diff --git a/zerver/lib/queue.py b/zerver/lib/queue.py
index 48d247f12ce..1551a1a4bd6 100644
--- a/zerver/lib/queue.py
+++ b/zerver/lib/queue.py
@@ -446,13 +446,20 @@ def queue_json_publish_rollback_unsafe(
if settings.USING_RABBITMQ:
get_queue_client().json_publish(queue_name, event)
elif processor:
- processor(event)
+ # Round-trip through orjson to simulate what RabbitMQ does.
+ processor(orjson.loads(orjson.dumps(event)))
else:
# The else branch is only hit during tests, where rabbitmq is not enabled.
# Must be imported here: A top section import leads to circular imports
from zerver.worker.queue_processors import get_worker
- get_worker(queue_name, disable_timeout=True).consume_single_event(event)
+ # As above, we round-trip the event through orjson to emulate
+ # what happens with RabbitMQ enqueueing and dequeueing the
+ # event. This ensures that we don't rely on non-JSON'able
+ # datatypes in the events.
+ get_worker(queue_name, disable_timeout=True).consume_single_event(
+ orjson.loads(orjson.dumps(event))
+ )
def queue_event_on_commit(queue_name: str, event: dict[str, Any]) -> None:
diff --git a/zerver/tornado/django_api.py b/zerver/tornado/django_api.py
index 61707fd116a..252eedad784 100644
--- a/zerver/tornado/django_api.py
+++ b/zerver/tornado/django_api.py
@@ -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))