Skip to content
Open
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 @@ -116,10 +116,10 @@ private void doInitializeInternal() {

@Override
public void doOutput(Collection<Record<Event>> records) {
reentrantLock.lock();
if (records.isEmpty()) {
return;
}
reentrantLock.lock();
try {
// TODO: Looks like this call to prepareTopicAndSchema is unnecessary as it is
// done in createProducer().
Expand All @@ -133,8 +133,9 @@ public void doOutput(Collection<Record<Event>> records) {
} catch (Exception e) {
LOG.error("Failed to setup the Kafka sink Plugin.", e);
throw new RuntimeException(e.getMessage());
} finally {
reentrantLock.unlock();
}
reentrantLock.unlock();
}

private void prepareTopicAndSchema() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
Expand Down Expand Up @@ -173,6 +176,30 @@ public void doOutputEmptyRecordsTest() {

}

@Test
public void doOutputReleasesLockWhenRecordsAreEmpty() {
final KafkaSink objectUnderTest = createObjectUnderTest();

objectUnderTest.doOutput(Arrays.asList());

assertThat(getLock(objectUnderTest).isLocked(), equalTo(false));
}

@Test
public void doOutputReleasesLockWhenSubmitFails() {
final Collection records = Arrays.asList(new Record(event));
when(executorService.submit(any(ProducerWorker.class))).thenThrow(new RuntimeException());
final KafkaSink objectUnderTest = createObjectUnderTest();

assertThrows(RuntimeException.class, () -> objectUnderTest.doOutput(records));

assertThat(getLock(objectUnderTest).isLocked(), equalTo(false));
}

private ReentrantLock getLock(final KafkaSink kafkaSink) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @ashwinhs, the fix looks good. A small thought: could the tests check behavior instead of reading the lock field? For example, after an empty or failing doOutput, a doOutput from another thread should still finish. That way the tests do not depend on a private field name.

return (ReentrantLock) ReflectionTestUtils.getField(kafkaSink, "reentrantLock");
}

@Test
public void shutdownTest() {
final KafkaSink objectUnderTest = createObjectUnderTest();
Expand Down
Loading