From 60464a4cea19aa9e1ac074fdfb641b38b7bbbbc9 Mon Sep 17 00:00:00 2001 From: Abhijeet Prasad Bodas Date: Thu, 3 Jun 2021 11:38:27 +0530 Subject: [PATCH] tests: Run on_commit hooks immediately in tornado_redirected_to_list. Further commits will hook `send_event` calls to `on_commit` in some cases. This change will make it easier to test such situations. We don't need to actually capture the callbacks, because the events sent are already tested via the list in which they are captured by `tornado_redirected_to_list`. --- zerver/lib/test_classes.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/zerver/lib/test_classes.py b/zerver/lib/test_classes.py index 9348477795..eece234332 100644 --- a/zerver/lib/test_classes.py +++ b/zerver/lib/test_classes.py @@ -1280,7 +1280,13 @@ Output: # So explicitly change parameter name to 'notice' to work around this problem django_tornado_api.process_notification = lambda notice: lst.append(notice) - yield + # Some `send_event` calls need to be executed only after the current transaction + # commits (using `on_commit` hooks). Because the transaction in Django tests never + # commits (rather, gets rolled back after the test completes), such events would + # never be sent in tests, and we would be unable to verify them. Hence, we use + # this helper to make sure the `send_event` calls actually run. + with self.captureOnCommitCallbacks(execute=True): + yield django_tornado_api.process_notification = real_event_queue_process_notification