use of java.util.Collections.singletonMap in project kafka by apache.
the class StoreChangelogReaderTest method shouldRequestEndOffsetsAndHandleTimeoutException.
@Test
public void shouldRequestEndOffsetsAndHandleTimeoutException() {
final TaskId taskId = new TaskId(0, 0);
final Task mockTask = mock(Task.class);
mockTask.maybeInitTaskTimeoutOrThrow(anyLong(), anyObject());
EasyMock.expectLastCall();
EasyMock.expect(storeMetadata.offset()).andReturn(5L).anyTimes();
EasyMock.expect(activeStateManager.changelogOffsets()).andReturn(singletonMap(tp, 5L));
EasyMock.expect(activeStateManager.taskId()).andReturn(taskId).anyTimes();
EasyMock.replay(mockTask, activeStateManager, storeMetadata, store);
final AtomicBoolean functionCalled = new AtomicBoolean(false);
final MockAdminClient adminClient = new MockAdminClient() {
@Override
public ListOffsetsResult listOffsets(final Map<TopicPartition, OffsetSpec> topicPartitionOffsets, final ListOffsetsOptions options) {
if (functionCalled.get()) {
return super.listOffsets(topicPartitionOffsets, options);
} else {
functionCalled.set(true);
throw new TimeoutException("KABOOM!");
}
}
};
adminClient.updateEndOffsets(Collections.singletonMap(tp, 10L));
final MockConsumer<byte[], byte[]> consumer = new MockConsumer<byte[], byte[]>(OffsetResetStrategy.EARLIEST) {
@Override
public Map<TopicPartition, OffsetAndMetadata> committed(final Set<TopicPartition> partitions) {
throw new AssertionError("Should not trigger this function");
}
};
final StoreChangelogReader changelogReader = new StoreChangelogReader(time, config, logContext, adminClient, consumer, callback);
changelogReader.register(tp, activeStateManager);
changelogReader.restore(Collections.singletonMap(taskId, mockTask));
assertEquals(StoreChangelogReader.ChangelogState.REGISTERED, changelogReader.changelogMetadata(tp).state());
assertNull(changelogReader.changelogMetadata(tp).endOffset());
assertTrue(functionCalled.get());
verify(mockTask);
EasyMock.resetToDefault(mockTask);
mockTask.clearTaskTimeout();
EasyMock.replay(mockTask);
changelogReader.restore(Collections.singletonMap(taskId, mockTask));
assertEquals(StoreChangelogReader.ChangelogState.RESTORING, changelogReader.changelogMetadata(tp).state());
assertEquals(10L, (long) changelogReader.changelogMetadata(tp).endOffset());
assertEquals(6L, consumer.position(tp));
verify(mockTask);
}
use of java.util.Collections.singletonMap in project kafka by apache.
the class StoreChangelogReaderTest method shouldRequestCommittedOffsetsAndHandleTimeoutException.
@Test
public void shouldRequestCommittedOffsetsAndHandleTimeoutException() {
final TaskId taskId = new TaskId(0, 0);
final Task mockTask = mock(Task.class);
if (type == ACTIVE) {
mockTask.clearTaskTimeout();
}
mockTask.maybeInitTaskTimeoutOrThrow(anyLong(), anyObject());
EasyMock.expectLastCall();
EasyMock.expect(stateManager.changelogAsSource(tp)).andReturn(true).anyTimes();
EasyMock.expect(storeMetadata.offset()).andReturn(5L).anyTimes();
EasyMock.expect(stateManager.changelogOffsets()).andReturn(singletonMap(tp, 5L));
EasyMock.expect(stateManager.taskId()).andReturn(taskId).anyTimes();
EasyMock.replay(mockTask, stateManager, storeMetadata, store);
final AtomicBoolean functionCalled = new AtomicBoolean(false);
final MockConsumer<byte[], byte[]> consumer = new MockConsumer<byte[], byte[]>(OffsetResetStrategy.EARLIEST) {
@Override
public Map<TopicPartition, OffsetAndMetadata> committed(final Set<TopicPartition> partitions) {
if (functionCalled.get()) {
return partitions.stream().collect(Collectors.toMap(Function.identity(), partition -> new OffsetAndMetadata(10L)));
} else {
functionCalled.set(true);
throw new TimeoutException("KABOOM!");
}
}
};
adminClient.updateEndOffsets(Collections.singletonMap(tp, 20L));
final StoreChangelogReader changelogReader = new StoreChangelogReader(time, config, logContext, adminClient, consumer, callback);
changelogReader.setMainConsumer(consumer);
changelogReader.register(tp, stateManager);
changelogReader.restore(Collections.singletonMap(taskId, mockTask));
assertEquals(type == ACTIVE ? StoreChangelogReader.ChangelogState.REGISTERED : StoreChangelogReader.ChangelogState.RESTORING, changelogReader.changelogMetadata(tp).state());
if (type == ACTIVE) {
assertNull(changelogReader.changelogMetadata(tp).endOffset());
} else {
assertEquals(0L, (long) changelogReader.changelogMetadata(tp).endOffset());
}
assertTrue(functionCalled.get());
verify(mockTask);
resetToDefault(mockTask);
if (type == ACTIVE) {
mockTask.clearTaskTimeout();
mockTask.clearTaskTimeout();
expectLastCall();
}
replay(mockTask);
changelogReader.restore(Collections.singletonMap(taskId, mockTask));
assertEquals(StoreChangelogReader.ChangelogState.RESTORING, changelogReader.changelogMetadata(tp).state());
assertEquals(type == ACTIVE ? 10L : 0L, (long) changelogReader.changelogMetadata(tp).endOffset());
assertEquals(6L, consumer.position(tp));
verify(mockTask);
}
Aggregations