[FIX] bus : notification should be committed before awake longtpolling thread

If calling more than once `sendmany` (or `sendone`) on the same http request, the `NOTIFY imbus` will tell pertinent polling thread to
fetch their notifications in the bus table, which will not contain the new notifications, since they are not yet committed. For some reason, this
doesn't happen when calling only once `sendmany`. Committing the notifications breaks the tests since no rollback is possible.
So, `self._cr.commit()` should not be called in a test environement.

In this case, we cannot use `isinstance(self._cr, TestCursor)` since TestCursor is only avaiblable for http request in test mode. Here the cursor is a normal cursor, and to determine if we are in test mode, there is no other option than checking the config of the odoo server.
This commit is contained in:
Jérome Maes
2015-10-06 09:44:38 +02:00
parent 22631a152a
commit 2df240ded0
+7
View File
@@ -55,6 +55,13 @@ class ImBus(models.Model):
if random.random() < 0.01:
self.gc()
if channels:
# The notifications must be commited in database because when calling `NOTIFY imbus`, some pertinent
# threads will be awakened and will fetch the notification in the bus table, but since the transaction
# is not commited, there will be nothing to fetch, the longpolling will return empty list of notification.
# For some reason, this happen when `sendmany` is called more than once on the same request.
# `self._cr.commit()` is prevented in a test environement, to allow test rollback.
if not openerp.tools.config['test_enable']:
self._cr.commit()
with openerp.sql_db.db_connect('postgres').cursor() as cr2:
cr2.execute("notify imbus, %s", (json_dump(list(channels)),))