Skip to content

Commit

Permalink
Core: Add invalidate for cached account errors (#3113)
Browse files Browse the repository at this point in the history
  • Loading branch information
osulzhenko authored Jun 3, 2024
1 parent 2319ff7 commit 17ba578
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ private static Map<String, String> getFromCacheOrAddMissedIds(String accountId,

public void invalidateAccountCache(String accountId) {
accountCache.remove(accountId);
accountToErrorCache.remove(accountId);
logger.debug("Account with id {} was invalidated", accountId);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,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<Account> 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<Account> future = target.getAccountById("accountId", timeout);
target.invalidateAccountCache("accountId");
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
Expand Down Expand Up @@ -149,6 +195,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<Account> 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.invalidateAccountCache("accountId");
final Future<Account> 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
Expand Down

0 comments on commit 17ba578

Please sign in to comment.