|
| 1 | +from odoo import api, SUPERUSER_ID |
| 2 | +from odoo.tests import TransactionCase |
| 3 | +from unittest.mock import patch |
| 4 | + |
| 5 | + |
| 6 | +class TestConcurrency(TransactionCase): |
| 7 | + def test_local_status_update(self): |
| 8 | + """ |
| 9 | + This test ensures that a parent build global state will eventually be updated |
| 10 | + even if updated concurrenctly in 2 different transactions, without transaction error |
| 11 | + """ |
| 12 | + with self.registry.cursor() as cr0: |
| 13 | + env0 = api.Environment(cr0, SUPERUSER_ID, {}) |
| 14 | + host = env0.ref('runbot_populate.main_host') |
| 15 | + host._process_messages() # ensure queue is empty |
| 16 | + parent_build = env0.ref('runbot_populate.build_base') |
| 17 | + build_child1 = env0.ref('runbot_populate.build_child1') |
| 18 | + build_child2 = env0.ref('runbot_populate.build_child2') |
| 19 | + parent_build.local_state = 'done' |
| 20 | + self.assertEqual(host.host_message_ids.mapped('message'), []) |
| 21 | + build_child1.local_state = 'testing' |
| 22 | + build_child2.local_state = 'testing' |
| 23 | + self.assertEqual(host.host_message_ids.mapped('message'), ['global_updated', 'global_updated']) |
| 24 | + self.assertEqual(host.host_message_ids.build_id, parent_build) |
| 25 | + host._process_messages() |
| 26 | + self.assertEqual(parent_build.global_state, 'waiting') |
| 27 | + env0.cr.commit() # youplahé |
| 28 | + |
| 29 | + with self.registry.cursor() as cr1: |
| 30 | + env1 = api.Environment(cr1, SUPERUSER_ID, {}) |
| 31 | + with self.registry.cursor() as cr2: |
| 32 | + env2 = api.Environment(cr2, SUPERUSER_ID, {}) |
| 33 | + build_child_cr1 = env1['runbot.build'].browse(build_child1.id) |
| 34 | + build_child_cr2 = env2['runbot.build'].browse(build_child2.id) |
| 35 | + self.assertEqual(build_child_cr1.parent_id.global_state, 'waiting') |
| 36 | + self.assertEqual(build_child_cr1.parent_id.children_ids.mapped('local_state'), ['testing', 'testing']) |
| 37 | + self.assertEqual(build_child_cr2.parent_id.global_state, 'waiting') |
| 38 | + self.assertEqual(build_child_cr2.parent_id.children_ids.mapped('local_state'), ['testing', 'testing']) |
| 39 | + build_child_cr1.local_state = 'done' |
| 40 | + build_child_cr2.local_state = 'done' |
| 41 | + # from the point of view of each transaction, the other one local_state didn't changed |
| 42 | + self.assertEqual(build_child_cr1.parent_id.children_ids.mapped('local_state'), ['testing', 'done']) |
| 43 | + self.assertEqual(build_child_cr2.parent_id.global_state, 'waiting') |
| 44 | + self.assertEqual(build_child_cr2.parent_id.children_ids.mapped('local_state'), ['done', 'testing']) |
| 45 | + env1.cr.commit() |
| 46 | + env2.cr.commit() |
| 47 | + env0.cr.commit() # not usefull just to ensure we have efefct of other transactions |
| 48 | + env0.cache.invalidate() |
| 49 | + self.assertEqual(parent_build.children_ids.mapped('local_state'), ['done', 'done']) |
| 50 | + self.assertEqual(parent_build.children_ids.mapped('global_state'), ['done', 'done']) |
| 51 | + self.assertEqual(host.host_message_ids.mapped('message'), ['global_updated', 'global_updated']) |
| 52 | + self.assertEqual(host.host_message_ids.build_id, parent_build) |
| 53 | + # at this point, this assertion is true, but not expected : self.assertEqual(parent_build.global_state, 'waiting') |
| 54 | + host._process_messages() |
| 55 | + self.assertEqual(parent_build.global_state, 'done') |
0 commit comments