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 @@ -4,7 +4,7 @@
import java.util.List;

/** */
public interface EventStore {
public interface EventStore extends AutoCloseable {
/**
* Initialize the event store
*
Expand Down Expand Up @@ -57,4 +57,11 @@ <K extends Object, V extends Object> EventConsumer<K, V> createConsumer(
*/
<K extends Object, V extends Object> EventProducer<K, V> createProducer(
String topicName, EventProducerConfig config);

/**
* Releases what {@link #init} opened, such as connections to the store. Producers and consumers
* created by this store are not closed; close each of them. The default does nothing.
*/
@Override
default void close() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,13 @@ public <K extends Object, V extends Object> EventProducer<K, V> createProducer(
producer.init(producerConfig);
return producer;
}

/** Closes the admin client opened by {@link #init}, with its connections and thread. */
@Override
public void close() {
if (adminClient != null) {
adminClient.close();
adminClient = null;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package org.hypertrace.core.eventstore.kafka;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.typesafe.config.ConfigFactory;
import java.util.Map;
import org.hypertrace.core.eventstore.EventStore;
import org.hypertrace.core.eventstore.EventStoreConfig;
import org.junit.jupiter.api.Test;

class KafkaEventStoreCloseTest {

// The admin client names its thread after its client id, which init sets to
// KafkaEventStore-<uuid>.
private static final String ADMIN_THREAD = "kafka-admin-client-thread | KafkaEventStore-";

@Test
void closeStopsTheAdminClientThatInitStarted() throws Exception {
long before = adminThreads();
EventStore store = initialised();
assertTrue(adminThreads() > before, "init did not start an admin client");

store.close();

assertEquals(before, adminThreads(), "close left the admin client running");
}

@Test
void closeBeforeInitDoesNothing() {
new KafkaEventStore().close();
}

@Test
void closingTwiceDoesNothingTheSecondTime() {
long before = adminThreads();
EventStore store = initialised();
store.close();
store.close();
assertEquals(before, adminThreads());
}

private static EventStore initialised() {
EventStore store = new KafkaEventStore();
// The admin client starts without a broker, so an address nothing listens on is enough.
store.init(
new EventStoreConfig(ConfigFactory.parseMap(Map.of("bootstrap.servers", "localhost:1"))));
return store;
}

private static long adminThreads() {
return Thread.getAllStackTraces().keySet().stream()
.filter(t -> t.getName().startsWith(ADMIN_THREAD) && t.isAlive())
.count();
}
}
Loading