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 @@ -300,7 +300,8 @@ private ProducerRecord<byte[], byte[]> convertTransformedRecord(SourceRecord rec
private boolean sendRecords() {
int processed = 0;
recordBatch(toSend.size());
final SourceRecordWriteCounter counter = new SourceRecordWriteCounter(toSend.size(), sourceTaskMetricsGroup);
final SourceRecordWriteCounter counter =
toSend.size() > 0 ? new SourceRecordWriteCounter(toSend.size(), sourceTaskMetricsGroup) : null;
for (final SourceRecord preTransformRecord : toSend) {
maybeThrowProducerSendException();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
import static org.junit.Assert.assertTrue;

@PowerMockIgnore({"javax.management.*",
"org.apache.log4j.*",
"org.apache.kafka.connect.runtime.isolation.*"})
@RunWith(PowerMockRunner.class)
public class WorkerSourceTaskTest extends ThreadedTest {
Expand Down Expand Up @@ -350,6 +351,51 @@ public List<SourceRecord> answer() throws Throwable {
PowerMock.verifyAll();
}

@Test
public void testPollReturnsNoRecords() throws Exception {
// Test that the task handles an empty list of records
createWorkerTask();

sourceTask.initialize(EasyMock.anyObject(SourceTaskContext.class));
EasyMock.expectLastCall();
sourceTask.start(TASK_PROPS);
EasyMock.expectLastCall();
statusListener.onStartup(taskId);
EasyMock.expectLastCall();

// We'll wait for some data, then trigger a flush
final CountDownLatch pollLatch = expectEmptyPolls(1, new AtomicInteger());
expectOffsetFlush(true);

sourceTask.stop();
EasyMock.expectLastCall();
expectOffsetFlush(true);

statusListener.onShutdown(taskId);
EasyMock.expectLastCall();

producer.close(EasyMock.anyObject(Duration.class));
EasyMock.expectLastCall();

transformationChain.close();
EasyMock.expectLastCall();

PowerMock.replayAll();

workerTask.initialize(TASK_CONFIG);
Future<?> taskFuture = executor.submit(workerTask);

assertTrue(awaitLatch(pollLatch));
assertTrue(workerTask.commitOffsets());
workerTask.stop();
assertTrue(workerTask.awaitStop(1000));

taskFuture.get();
assertPollMetrics(0);

PowerMock.verifyAll();
}

@Test
public void testCommit() throws Exception {
// Test that the task commits properly when prompted
Expand Down Expand Up @@ -767,6 +813,24 @@ public void testHeadersWithCustomConverter() throws Exception {
PowerMock.verifyAll();
}

private CountDownLatch expectEmptyPolls(int minimum, final AtomicInteger count) throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(minimum);
// Note that we stub these to allow any number of calls because the thread will continue to
// run. The count passed in + latch returned just makes sure we get *at least* that number of
// calls
EasyMock.expect(sourceTask.poll())
.andStubAnswer(new IAnswer<List<SourceRecord>>() {
@Override
public List<SourceRecord> answer() throws Throwable {
count.incrementAndGet();
latch.countDown();
Thread.sleep(10);
return Collections.emptyList();
}
});
return latch;
}

private CountDownLatch expectPolls(int minimum, final AtomicInteger count) throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(minimum);
// Note that we stub these to allow any number of calls because the thread will continue to
Expand Down