Skip to content

Commit

Permalink
Fix cache of events
Browse files Browse the repository at this point in the history
The cache entries for events were including instance of the components,
hence, they included the work instance / odoo environment.

Only the component class and the events it provides are new cached, the
instances of components being instanciated when needed.

Reported on:
OCA/connector-magento#255 (comment)
  • Loading branch information
guewen authored and thienvh332 committed Oct 9, 2024
1 parent 93c22e7 commit bca4e0b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
12 changes: 10 additions & 2 deletions component_event/components/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ def on_record_create(self, record, fields=None):
import logging
import operator

from collections import defaultdict
from functools import wraps

from odoo.addons.component.core import AbstractComponent, Component
Expand Down Expand Up @@ -229,7 +230,7 @@ def _complete_component_build(cls):
name)
)
def _collect_events(self, name):
events = set([])
events = defaultdict(set)
collection_name = (self.work.collection._name
if self.work._collection is not None
else None)
Expand All @@ -240,6 +241,13 @@ def _collect_events(self, name):
)
for cls in component_classes:
if cls.has_event(name):
events[cls].add(name)
return events

def _init_collected_events(self, class_events):
events = set()
for cls, names in class_events.iteritems():
for name in names:
component = cls(self.work)
events.add(getattr(component, name))
return events
Expand All @@ -249,7 +257,7 @@ def collect_events(self, name):
if not name.startswith('on_'):
raise ValueError("an event name always starts with 'on_'")

events = self._collect_events(name)
events = self._init_collected_events(self._collect_events(name))
return CollectedEvents(events)


Expand Down
12 changes: 9 additions & 3 deletions component_event/tests/test_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,9 @@ def setUp(self):
# we don't mind about the collection and the model here,
# the events we test are global
env = mock.MagicMock()
work = EventWorkContext(model_name='res.users', env=env,
components_registry=self.comp_registry)
self.collecter = self.comp_registry['base.event.collecter'](work)
self.work = EventWorkContext(model_name='res.users', env=env,
components_registry=self.comp_registry)
self.collecter = self.comp_registry['base.event.collecter'](self.work)

def test_event(self):
class MyEventListener(Component):
Expand Down Expand Up @@ -193,6 +193,9 @@ def on_record_create(self):
collected = self.collecter.collect_events('on_record_create')
# CollectedEvents.events contains the collected events
self.assertEquals(1, len(collected.events))
event = list(collected.events)[0]
self.assertEquals(self.work, event.im_self.work)
self.assertEquals(self.work.env, event.im_self.work.env)

# build and register a new listener
class MyOtherEventListener(Component):
Expand All @@ -213,6 +216,9 @@ def on_record_create(self):
collected = collecter.collect_events('on_record_create')
# CollectedEvents.events contains the collected events
self.assertEquals(1, len(collected.events))
event = list(collected.events)[0]
self.assertEquals(work, event.im_self.work)
self.assertEquals(env, event.im_self.work.env)

# if we empty the cache, as it on the class, both collecters
# should now find the 2 events
Expand Down

0 comments on commit bca4e0b

Please sign in to comment.