From 8144b03ac4d349f2be41c23d1fced02556cc0054 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=A9rome=20Perrin?= <jerome@nexedi.com>
Date: Tue, 7 May 2024 10:39:17 +0900
Subject: [PATCH] testCMFActivity: support pickle protocol > 0 in
 test_insert_max_payload

This test assumed that activating a method with a string of length `n+x`
as argument would be serialized in `x` more bytes that activating the
same method with a string of length `n` as argument, which is only true
for protocol 0 for multiple reasons ( short strings and long strings
are serialized differently, "frames" are used with protocol 5) and
db.string_literal seems to introduce a difference.

Change the test to mock Message.dump to produce longer dumps instead of
relying on this assumption.
---
 product/CMFActivity/tests/testCMFActivity.py | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)

diff --git a/product/CMFActivity/tests/testCMFActivity.py b/product/CMFActivity/tests/testCMFActivity.py
index d68848e965..47348b2ea9 100644
--- a/product/CMFActivity/tests/testCMFActivity.py
+++ b/product/CMFActivity/tests/testCMFActivity.py
@@ -1902,18 +1902,30 @@ class TestCMFActivity(ERP5TypeTestCase, LogInterceptor):
         activity_tool.activate(activity=activity, group_id=str(i)
                               ).doSomething(arg)
       activity_tool.activate(activity=activity, group_id='~'
-                            ).doSomething(' ' * n)
+                            ).doSomething(last_message_arg)
       self.tic()
       self.assertEqual(len(invoke_list), N)
-      invoke_list.remove(n)
+      invoke_list.remove(len(last_message_arg))
       self.assertEqual(set(invoke_list), {len(arg)})
       del invoke_list[:]
     activity_tool.__class__.doSomething = \
       lambda self, arg: invoke_list.append(len(arg))
+    original_dump = Message.dump
+    def dump(m):
+      dumped = original_dump(m)
+      if m.args == (last_message_arg, ):
+        dumped += b' ' * n
+      return dumped
+    Message.dump = staticmethod(dump)
     try:
       DB.query = query
       for activity in ActivityTool.activity_dict:
+        # We'll activate N-1 times with `arg`, which is a long string that
+        # should produce large messsage dumps and 1 time with
+        # `last_message_arg`, for which dump is patched to artificially
+        # produce a message dump `n` bytes longer than usual.
         arg = ' ' * (max_allowed_packet // N)
+        last_message_arg = 'last'
         # Find the size of the last message argument, such that all messages
         # are inserted in a single query whose size is to the maximum allowed.
         n = 0
@@ -1934,6 +1946,7 @@ class TestCMFActivity(ERP5TypeTestCase, LogInterceptor):
     finally:
       del activity_tool.__class__.doSomething
       DB.query = original_query
+      Message.dump = original_dump
 
   def test_115_TestSerializationTagSQLDictPreventsParallelExecution(self):
     """
-- 
2.30.9