Skip to content
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 @@ -9,7 +9,6 @@
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CancellationException;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
Expand All @@ -24,6 +23,12 @@
// the thread that makes it, and a stream by closing it. Either way, the
// request then ends with a CancellationException, so that an aborted read can
// be told apart from a complete one.
//
// The timeout of this class must not cover starting a container: if it
// interrupts Testcontainers while it looks for Docker, Testcontainers never
// tries again, and every later test that needs a container fails. Tests that
// need EventSourcingDB itself start it in @BeforeEach elsewhere, e.g. in
// WriteEventsTest, which this timeout does not cover.
@Timeout(10)
class CancellationTest {
static List<Operation> all() {
Expand Down Expand Up @@ -124,29 +129,6 @@ void aStreamThatIsClosedBeforeItStartsDoesNotSendTheRequest(Operation operation)
}
}

@Test
void aWriteOfAnInterruptedThreadWritesNothing() {
var container = Database.start();
try {
var client = container.getClient();
var candidate = new EventCandidate(
"https://www.eventsourcingdb.io", "/test", "io.eventsourcingdb.test", Map.of("value", 23));

Thread.currentThread().interrupt();
try {
assertThrows(CancellationException.class, () -> client.writeEvents(List.of(candidate)));
} finally {
assertTrue(Thread.interrupted());
}

try (var events = client.readEvents("/", new ReadEventsOptions(true))) {
assertEquals(0, events.count());
}
} finally {
container.stop();
}
}

@Test
void anInterruptEndsAWriteWhoseResponseStopsInTheMiddle() throws InterruptedException {
try (var server = FakeServer.start((exchange, fakeServer) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CancellationException;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -70,6 +71,20 @@ void writesMultipleEvents() {
assertEquals(new EventData(42), writtenEvents.get(1).data(EventData.class));
}

@Test
void writesNothingIfTheThreadIsInterrupted() {
Thread.currentThread().interrupt();
try {
assertThrows(CancellationException.class, () -> client.writeEvents(List.of(candidate(23))));
} finally {
assertTrue(Thread.interrupted());
}

try (var events = client.readEvents("/", new ReadEventsOptions(true))) {
assertEquals(0, events.count());
}
}

@Test
void writesTheTraceContextOfAnEvent() {
var traceParent = "00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01";
Expand Down
Loading