Skip to content

Commit

Permalink
test onetime reads under load
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan committed Mar 16, 2024
1 parent 246b002 commit 2501aa8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ private Mono<Paste> trackAccess(Paste paste) {
return result
.map(p -> p.trackView(LocalDateTime.now()))
.flatMap(pasteRepository::save)
.doOnSuccess(burntPaste -> log.info("OneTime paste {} viewed and burnt", burntPaste.getId()));
.doOnSuccess(burntPaste -> log.info("OneTime paste {} viewed and burnt", burntPaste.getId()))
.onErrorResume(OptimisticLockingFailureException.class, throwable -> Mono.empty());
}

trackingService.trackView(paste.getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
import org.springframework.test.web.reactive.server.WebTestClient;
import reactor.core.publisher.Mono;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Stream;

import static java.time.Duration.ofMillis;
import static java.time.LocalDateTime.now;
import static java.time.LocalDateTime.parse;
Expand Down Expand Up @@ -66,6 +69,31 @@ void getOneTimePasteTwice() {
.expectStatus().isNotFound();
}

@Test
@DisplayName("GET /{pasteId} - one-time paste is read-once even under load")
void getOneTimePasteConcurrently() {
var oneTimePaste = givenOneTimePaste();
var okCount = new AtomicInteger();

Runnable call = () -> {
try {
webClient.get()
.uri("/api/v1/paste/" + oneTimePaste.getId())
.exchange()
.expectStatus().isNotFound();
} catch (AssertionError e) {
okCount.incrementAndGet();
}
};

Stream.generate(() -> call)
.parallel()
.limit(100)
.forEach(Runnable::run);

assertThat(okCount.get()).isOne();
}

@Test
@DisplayName("GET / - one-time paste is never listed")
void findAllPastes() {
Expand Down

0 comments on commit 2501aa8

Please sign in to comment.