From 3a7d070966a55bd1bb113e2ec262ddeffe144c6b Mon Sep 17 00:00:00 2001 From: Allen Li Date: Wed, 4 Sep 2024 21:19:43 +0000 Subject: [PATCH] [gerrit_utils] Add caching to ShouldUseSSO This only lasts per process invocation, so we don't worry about cache size (and it's a minor performance save). Bug: b/350806563 Change-Id: Ie8e1aa2933c5582a3a2e2f75f04590f6bb432c4c Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/5833280 Commit-Queue: Allen Li Reviewed-by: Josip Sokcevic --- gerrit_util.py | 2 ++ tests/gerrit_util_test.py | 6 ++---- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/gerrit_util.py b/gerrit_util.py index 3b9470c98..9801765ed 100644 --- a/gerrit_util.py +++ b/gerrit_util.py @@ -11,6 +11,7 @@ from __future__ import annotations import base64 import contextlib +import functools import http.cookiejar import json import logging @@ -173,6 +174,7 @@ class SSOHelper(object): ssoHelper = SSOHelper() +@functools.lru_cache(maxsize=None) def ShouldUseSSO(host: str, email: str) -> bool: """Return True if we should use SSO for the given Gerrit host and user.""" LOGGER.debug("Determining whether we should use SSO...") diff --git a/tests/gerrit_util_test.py b/tests/gerrit_util_test.py index 4d92842f2..2e05cf3b9 100755 --- a/tests/gerrit_util_test.py +++ b/tests/gerrit_util_test.py @@ -731,17 +731,15 @@ class ShouldUseSSOTest(unittest.TestCase): def setUp(self) -> None: self.newauth = mock.patch('newauth.Enabled', return_value=True) self.newauth.start() - self.cwd = mock.patch('os.getcwd', return_value='/fake/cwd') self.cwd.start() - self.sso = mock.patch('gerrit_util.ssoHelper.find_cmd', return_value='/fake/git-remote-sso') self.sso.start() - scm_mock.GIT(self) - self.addCleanup(mock.patch.stopall) + + gerrit_util.ShouldUseSSO.cache_clear() return super().setUp() def tearDown(self) -> None: