Skip to content

Commit

Permalink
WCM-633: handle cache los in object log sweep
Browse files Browse the repository at this point in the history
  • Loading branch information
stollero committed Jan 16, 2025
1 parent 21655f3 commit b57d11f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
9 changes: 7 additions & 2 deletions core/src/zeit/objectlog/objectlog.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import pendulum
import persistent
import transaction
import ZODB.POSException
import zope.app.keyreference.interfaces
import zope.component
import zope.interface
Expand Down Expand Up @@ -71,8 +72,12 @@ def clean(self, timedelta):
remove = []
for key in self._object_log:
log = self._object_log[key]
for time_key in list(log.keys(max=reference_time)):
del log[time_key]
try:
for time_key in list(log.keys(max=reference_time)):
del log[time_key]
except ZODB.POSException.POSKeyError:
logger.warning('ZODB.POSException.POSKeyError, removing lost key %s', key)
remove.append(key)
if not log:
remove.append(key)
for key in remove:
Expand Down
24 changes: 23 additions & 1 deletion core/src/zeit/objectlog/tests/test_objectlog.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
from datetime import datetime
from datetime import datetime, timedelta
from unittest import mock

import transaction
import ZODB.Connection
import ZODB.POSException
import zope.component

import zeit.objectlog.interfaces
import zeit.objectlog.objectlog
import zeit.objectlog.testing


class FakeLogItem:
# broken LogItem that mimics the cache loss behaviour of ZODB
def keys(self, *args, **kw):
raise ZODB.POSException.POSKeyError('foo')


class FakeObjectLog(dict):
def __setitem__(self, key, _):
return super().__setitem__(key, FakeLogItem())


class ObjectLog(zeit.objectlog.testing.FunctionalTestCase):
def setUp(self):
super().setUp()
Expand All @@ -30,3 +45,10 @@ def test_delete_removes_entries_of_object(self):
log.delete(content2)
self.assertEqual(0, len(list(log.get_log(content2))))
self.assertEqual(1, len(list(log.get_log(self.content))))

def test_clean(self):
log = zope.component.getUtility(zeit.objectlog.interfaces.IObjectLog)
with mock.patch.object(log, '_object_log', FakeObjectLog()):
zeit.objectlog.interfaces.ILog(self.content).log('one')
log.clean(timedelta(days=0))
self.assertEqual(0, len(list(log.get_log(self.content))))

0 comments on commit b57d11f

Please sign in to comment.