Skip to content

Commit

Permalink
Make Bodhi able to clear models.Release._all_releases cache
Browse files Browse the repository at this point in the history
fix fedora-infra#2177

Signed-off-by: Sebastian Wojciechowski <[email protected]>
  • Loading branch information
sebwoj committed Jun 28, 2019
1 parent 413b446 commit a5a320e
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 5 deletions.
5 changes: 5 additions & 0 deletions bodhi/server/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,11 @@ def all_releases(cls):
return cls._all_releases
_all_releases = None

@classmethod
def clear_all_releases_cache(cls):
"""Clear up Release cache."""
cls._all_releases = None

@classmethod
def get_tags(cls, session):
"""
Expand Down
3 changes: 3 additions & 0 deletions bodhi/server/services/releases.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,9 @@ def save_release(request):
)
setattr(r, k, v)

# We have to invalidate the release cache after change
Release.clear_all_releases_cache()

except Exception as e:
log.exception(e)
request.errors.add('body', 'release',
Expand Down
4 changes: 2 additions & 2 deletions bodhi/tests/server/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ class BaseTestCaseMixin:
def _setup_method(self):
"""Set up Bodhi for testing."""
# Ensure "cached" objects are cleared before each test.
models.Release._all_releases = None
models.Release.clear_all_releases_cache()
models.Release._tag_cache = None

if engine is None:
Expand Down Expand Up @@ -293,7 +293,7 @@ def create_release(self, version, create_automatic_updates=False):
package_manager=models.PackageManager.unspecified,
testing_repository=None)
self.db.add(release)
models.Release._all_releases = None
models.Release.clear_all_releases_cache()
models.Release._tag_cache = None
self.db.flush()
return release
Expand Down
2 changes: 1 addition & 1 deletion bodhi/tests/server/consumers/test_composer.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def setUp(self):
self.handler = ComposerHandler(db_factory=self.db_factory, compose_dir=self.tempdir)

# Reset "cached" objects before each test.
Release._all_releases = None
Release.clear_all_releases_cache()
Release._tag_cache = None

self.expected_sems = 0
Expand Down
10 changes: 10 additions & 0 deletions bodhi/tests/server/services/test_releases.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,16 @@ def test_new_release(self):

self.assertEqual(r.state, ReleaseState.disabled)

# Let's check Release cache content
releases = Release.all_releases()
disabled_releases = releases["disabled"]
self.assertEqual(disabled_releases[0]["name"], "F42")
self.assertEqual(disabled_releases[1]["name"], "F22")
self.assertEqual(len(disabled_releases), 2)
current_releases = releases["current"]
self.assertEqual(current_releases[0]["name"], "F17")
self.assertEqual(len(current_releases), 1)

def test_list_releases_by_current_state(self):
""" Test that we can filter releases using the 'current' state """
res = self.app.get('/releases/', {"state": 'current'})
Expand Down
4 changes: 2 additions & 2 deletions bodhi/tests/server/test___init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,8 @@ def test_sets_up_home_page_cache(self, _generate_home_page_stats):
@mock.patch.dict('bodhi.server.config.config', {'warm_cache_on_start': True})
def test_warms_up_releases_cache(self):
"""main() should warm up the _all_releases cache."""
# Let's force the release cache to None
models.Release._all_releases = None
# Let's clear the release cache
models.Release.clear_all_releases_cache()

server.main({}, testing='guest', session=self.db)

Expand Down
24 changes: 24 additions & 0 deletions bodhi/tests/server/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,12 @@ def test_all_releases(self):
# Make sure it's the same cached object
self.assertIs(releases, model.Release.all_releases())

def test_clear_all_releases_cache(self):
model.Release.all_releases()
self.assertIsNotNone(model.Release._all_releases)
model.Release.clear_all_releases_cache()
self.assertIsNone(model.Release._all_releases)


class TestReleaseCritpathMinKarma(BaseTestCase):
"""Tests for the Release.critpath_min_karma property."""
Expand Down Expand Up @@ -806,6 +812,12 @@ def test_all_releases(self):
# Make sure it's the same cached object
self.assertIs(releases, model.Release.all_releases())

def test_clear_all_releases_cache(self):
model.Release.all_releases()
self.assertIsNotNone(model.Release._all_releases)
model.Release.clear_all_releases_cache()
self.assertIsNone(model.Release._all_releases)


class TestReleaseContainer(ModelTest):
"""Unit test case for the ``Release`` model for container releases."""
Expand Down Expand Up @@ -840,6 +852,12 @@ def test_all_releases(self):
# Make sure it's the same cached object
self.assertIs(releases, model.Release.all_releases())

def test_clear_all_releases_cache(self):
model.Release.all_releases()
self.assertIsNotNone(model.Release._all_releases)
model.Release.clear_all_releases_cache()
self.assertIsNone(model.Release._all_releases)


class TestReleaseFlatpak(ModelTest):
"""Unit test case for the ``Release`` model for flatpak releases."""
Expand Down Expand Up @@ -874,6 +892,12 @@ def test_all_releases(self):
# Make sure it's the same cached object
self.assertIs(releases, model.Release.all_releases())

def test_clear_all_releases_cache(self):
model.Release.all_releases()
self.assertIsNotNone(model.Release._all_releases)
model.Release.clear_all_releases_cache()
self.assertIsNone(model.Release._all_releases)


class MockWiki(object):
""" Mocked simplemediawiki.MediaWiki class. """
Expand Down

0 comments on commit a5a320e

Please sign in to comment.