19.0 vanilla

This commit is contained in:
Ernad Husremovic 2026-03-09 09:30:27 +01:00
parent d1963a3c3a
commit 2d3ee4855a
7430 changed files with 2687981 additions and 2965473 deletions

View file

@ -1,11 +1,16 @@
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo.tests import BaseCase
import json
import selectors
import threading
from ..models.bus import json_dump, get_notify_payloads, NOTIFY_PAYLOAD_MAX_LENGTH
import odoo
from odoo.tests import TransactionCase
from ..models.bus import json_dump, get_notify_payloads, NOTIFY_PAYLOAD_MAX_LENGTH, ODOO_NOTIFY_FUNCTION
class NotifyTests(BaseCase):
class NotifyTests(TransactionCase):
def test_get_notify_payloads(self):
"""
@ -47,3 +52,56 @@ class NotifyTests(BaseCase):
"as it contains only 1 channel")
with self.assertRaises(AssertionError):
check_payloads_size(payloads)
def test_postcommit(self):
"""Asserts all ``postcommit`` channels are fetched with a single listen."""
if ODOO_NOTIFY_FUNCTION != "pg_notify":
return
channels = []
stop_event = threading.Event()
selector_ready_event = threading.Event()
def single_listen():
nonlocal channels
with (
odoo.sql_db.db_connect("postgres").cursor() as cr,
selectors.DefaultSelector() as sel,
):
cr.execute("listen imbus")
cr.commit()
conn = cr._cnx
sel.register(conn, selectors.EVENT_READ)
selector_ready_event.set()
found = False
while not stop_event.is_set() and not found:
if sel.select(timeout=5):
conn.poll()
while conn.notifies:
if notify_channels := [
c
for c in json.loads(conn.notifies.pop().payload)
if c[0] == self.env.cr.dbname
]:
channels = notify_channels
found = True
break
thread = threading.Thread(target=single_listen)
thread.start()
selector_ready_event.wait(timeout=5)
self.env["bus.bus"].search([]).unlink()
self.env["bus.bus"]._sendone("channel 1", "test 1", {})
self.env["bus.bus"]._sendone("channel 2", "test 2", {})
self.env["bus.bus"]._sendone("channel 1", "test 3", {})
self.assertEqual(self.env["bus.bus"].search_count([]), 0)
self.assertEqual(channels, [])
self.env.cr.precommit.run() # trigger the creation of bus.bus records
self.assertEqual(self.env["bus.bus"].search_count([]), 3)
self.assertEqual(channels, [])
self.env.cr.postcommit.run() # notify
thread.join(timeout=5)
stop_event.set()
self.assertEqual(self.env["bus.bus"].search_count([]), 3)
self.assertEqual(
channels, [[self.env.cr.dbname, "channel 1"], [self.env.cr.dbname, "channel 2"]]
)