Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(session): avoid cache exceptional session creation future #189

Merged
merged 3 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,14 @@ public CompletableFuture<Session> getSession(long shardId) {
new IllegalStateException("session manager has been closed"));
}

return sessionsByShardId.computeIfAbsent(shardId, s -> factory.create(shardId));
return sessionsByShardId.compute(
shardId,
(key, existing) -> {
if (existing != null && !existing.isCompletedExceptionally()) {
return existing;
}
return factory.create(shardId);
});
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,29 @@ void existingSession() {
verifyNoMoreInteractions(factory, session);
}

@Test
void existingSessionWithFailure() {
var shardId = 1L;
// first failed
when(factory.create(shardId))
.thenReturn(CompletableFuture.failedFuture(new IllegalStateException("failed")));
var session1 = manager.getSession(shardId);
assertThat(session1).isCompletedExceptionally();
verify(factory, times(1)).create(shardId);

// second should be success
when(factory.create(shardId)).thenReturn(CompletableFuture.completedFuture(session));
var session2 = manager.getSession(shardId);
assertThat(session2).isCompletedWithValue(session);
verify(factory, times(2)).create(shardId);

// third should be cache
var session3 = manager.getSession(shardId);
assertThat(session3).isSameAs(session2);
verify(factory, times(2)).create(shardId);
verifyNoMoreInteractions(factory, session);
}

@Test
void close() throws Exception {
var shardId = 5L;
Expand Down
Loading