From b0681d45d94f5c26557286e8479e1ed09800b9d1 Mon Sep 17 00:00:00 2001 From: osulzhenko Date: Wed, 10 Apr 2024 18:06:34 +0300 Subject: [PATCH 1/3] Add invalidate for cached account errors --- .../org/prebid/server/settings/CachingApplicationSettings.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/org/prebid/server/settings/CachingApplicationSettings.java b/src/main/java/org/prebid/server/settings/CachingApplicationSettings.java index 3b71936c7cd..85685aef2a8 100644 --- a/src/main/java/org/prebid/server/settings/CachingApplicationSettings.java +++ b/src/main/java/org/prebid/server/settings/CachingApplicationSettings.java @@ -243,11 +243,13 @@ private static Map getFromCacheOrAddMissedIds(String accountId, public void invalidateAccountCache(String accountId) { accountCache.remove(accountId); + accountToErrorCache.remove(accountId); logger.debug("Account with id {0} was invalidated", accountId); } public void invalidateAllAccountCache() { accountCache.clear(); + accountToErrorCache.clear(); logger.debug("All accounts cache were invalidated"); } From a137099f27abd6d6cc554eff8d30479e0cbfd22d Mon Sep 17 00:00:00 2001 From: osulzhenko Date: Thu, 11 Apr 2024 18:15:15 +0300 Subject: [PATCH 2/3] add jUnit tests --- .../CachingApplicationSettingsTest.java | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git a/src/test/java/org/prebid/server/settings/CachingApplicationSettingsTest.java b/src/test/java/org/prebid/server/settings/CachingApplicationSettingsTest.java index a51d3fbfef7..c99c6b078ac 100644 --- a/src/test/java/org/prebid/server/settings/CachingApplicationSettingsTest.java +++ b/src/test/java/org/prebid/server/settings/CachingApplicationSettingsTest.java @@ -110,6 +110,52 @@ public void getAccountByIdShouldReturnResultFromCacheOnSuccessiveCalls() { verifyNoMoreInteractions(delegateSettings); } + @Test + public void getAccountByIdShouldReturnResultFromSeparateCallWhenCacheWasInvalidatedForAccount() { + // given + final Account account = Account.builder() + .id("accountId") + .auction(AccountAuctionConfig.builder() + .priceGranularity("med") + .build()) + .build(); + given(delegateSettings.getAccountById(eq("accountId"), same(timeout))) + .willReturn(Future.succeededFuture(account)); + + // when + final Future future = target.getAccountById("accountId", timeout); + target.invalidateAccountCache(account.getId()); + target.getAccountById("accountId", timeout); + + // then + assertThat(future.succeeded()).isTrue(); + assertThat(future.result()).isSameAs(account); + verify(delegateSettings, times(2)).getAccountById(eq("accountId"), same(timeout)); + } + + @Test + public void getAccountByIdShouldReturnResultFromSeparateCallWhenCacheWasInvalidatedForAllAccounts() { + // given + final Account account = Account.builder() + .id("accountId") + .auction(AccountAuctionConfig.builder() + .priceGranularity("med") + .build()) + .build(); + given(delegateSettings.getAccountById(eq("accountId"), same(timeout))) + .willReturn(Future.succeededFuture(account)); + + // when + final Future future = target.getAccountById("accountId", timeout); + target.invalidateAllAccountCache(); + target.getAccountById("accountId", timeout); + + // then + assertThat(future.succeeded()).isTrue(); + assertThat(future.result()).isSameAs(account); + verify(delegateSettings, times(2)).getAccountById(eq("accountId"), same(timeout)); + } + @Test public void getAccountByIdShouldPropagateFailure() { // given @@ -148,6 +194,46 @@ public void getAccountByIdShouldCachePreBidException() { .hasMessage("error"); } + @Test + public void getAccountByIdShouldThrowSeparatePreBidExceptionWhenCacheWasInvalidatedForAccount() { + // given + given(delegateSettings.getAccountById(anyString(), any())) + .willReturn(Future.failedFuture(new PreBidException("error"))); + + // when + target.getAccountById("accountId", timeout); + target.invalidateAccountCache("accountId"); + final Future lastFuture = target + .getAccountById("accountId", timeout); + + // then + verify(delegateSettings, times(2)).getAccountById(eq("accountId"), same(timeout)); + assertThat(lastFuture.failed()).isTrue(); + assertThat(lastFuture.cause()) + .isInstanceOf(PreBidException.class) + .hasMessage("error"); + } + + @Test + public void getAccountByIdShouldThrowSeparatePreBidExceptionWhenCacheWasInvalidatedForAllAccounts() { + // given + given(delegateSettings.getAccountById(anyString(), any())) + .willReturn(Future.failedFuture(new PreBidException("error"))); + + // when + target.getAccountById("accountId", timeout); + target.invalidateAllAccountCache(); + final Future lastFuture = target + .getAccountById("accountId", timeout); + + // then + verify(delegateSettings, times(2)).getAccountById(eq("accountId"), same(timeout)); + assertThat(lastFuture.failed()).isTrue(); + assertThat(lastFuture.cause()) + .isInstanceOf(PreBidException.class) + .hasMessage("error"); + } + @Test public void getAccountByIdShouldNotCacheNotPreBidException() { // given From 95e99b143730a3339e1368136efe5c8c14d36dd8 Mon Sep 17 00:00:00 2001 From: osulzhenko Date: Mon, 3 Jun 2024 13:32:43 +0300 Subject: [PATCH 3/3] update test cases --- .../server/settings/CachingApplicationSettingsTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/org/prebid/server/settings/CachingApplicationSettingsTest.java b/src/test/java/org/prebid/server/settings/CachingApplicationSettingsTest.java index adda8004545..66f867ebaf3 100644 --- a/src/test/java/org/prebid/server/settings/CachingApplicationSettingsTest.java +++ b/src/test/java/org/prebid/server/settings/CachingApplicationSettingsTest.java @@ -148,7 +148,7 @@ public void getAccountByIdShouldReturnResultFromSeparateCallWhenCacheWasInvalida // when final Future future = target.getAccountById("accountId", timeout); - target.invalidateAllAccountCache(); + target.invalidateAccountCache("accountId"); target.getAccountById("accountId", timeout); // then @@ -223,7 +223,7 @@ public void getAccountByIdShouldThrowSeparatePreBidExceptionWhenCacheWasInvalida // when target.getAccountById("accountId", timeout); - target.invalidateAllAccountCache(); + target.invalidateAccountCache("accountId"); final Future lastFuture = target .getAccountById("accountId", timeout);