Program: 37693
Project: 37693
Buggy source:
/** 
 * Verifies that when we call connect that it makes the appropriate calls to ConsumerStateManager to initialize. This test has the ConsumerStateManager (a mock) return ConsumerState for every partition on the topic. We verify that our internal kafka client then knows to start reading from the previously saved consumer state offsets
 */
@Test public void testConnectWithMultiplePartitionsOnTopicWithAllPreviouslySavedState(){
  final String consumerId="String_Node_Str";
  final TopicPartition partition0=new TopicPartition(topicName,0);
  final TopicPartition partition1=new TopicPartition(topicName,1);
  final TopicPartition partition2=new TopicPartition(topicName,2);
  final long lastCommittedOffsetPartition0=1234L;
  final long lastCommittedOffsetPartition1=4321L;
  final long lastCommittedOffsetPartition2=1337L;
  final long expectedPartition0Offset=lastCommittedOffsetPartition0 + 1;
  final long expectedPartition1Offset=lastCommittedOffsetPartition1 + 1;
  final long expectedPartition2Offset=lastCommittedOffsetPartition2 + 1;
  List<String> brokerHosts=Lists.newArrayList(kafkaTestServer.getKafkaServer().serverConfig().advertisedHostName() + "String_Node_Str" + kafkaTestServer.getKafkaServer().serverConfig().advertisedPort());
  final SidelineConsumerConfig config=new SidelineConsumerConfig(brokerHosts,consumerId,topicName);
  config.setKafkaConsumerProperty("String_Node_Str","String_Node_Str");
  KafkaConsumer<byte[],byte[]> mockKafkaConsumer=mock(KafkaConsumer.class);
  List<PartitionInfo> mockPartitionInfos=Lists.newArrayList(new PartitionInfo(topicName,0,new Node(0,"String_Node_Str",9092),new Node[0],new Node[0]),new PartitionInfo(topicName,1,new Node(0,"String_Node_Str",9092),new Node[0],new Node[0]),new PartitionInfo(topicName,2,new Node(0,"String_Node_Str",9092),new Node[0],new Node[0]));
  when(mockKafkaConsumer.partitionsFor(eq(topicName))).thenReturn(mockPartitionInfos);
  PersistenceManager mockPersistenceManager=mock(PersistenceManager.class);
  ConsumerState consumerState=new ConsumerState();
  consumerState.setOffset(partition0,lastCommittedOffsetPartition0);
  consumerState.setOffset(partition1,lastCommittedOffsetPartition1);
  consumerState.setOffset(partition2,lastCommittedOffsetPartition2);
  when(mockPersistenceManager.retrieveConsumerState(eq(consumerId))).thenReturn(consumerState);
  SidelineConsumer sidelineConsumer=new SidelineConsumer(config,mockPersistenceManager,mockKafkaConsumer);
  sidelineConsumer.open(null);
  verify(mockKafkaConsumer,times(1)).assign(eq(Lists.newArrayList(partition0,partition1,partition2)));
  verify(mockKafkaConsumer,never()).seekToBeginning(anyList());
  InOrder inOrderVerification=inOrder(mockKafkaConsumer);
  inOrderVerification.verify(mockKafkaConsumer,times(1)).seek(eq(partition0),eq(expectedPartition0Offset));
  inOrderVerification.verify(mockKafkaConsumer,times(1)).seek(eq(partition1),eq(expectedPartition1Offset));
  inOrderVerification.verify(mockKafkaConsumer,times(1)).seek(eq(partition2),eq(expectedPartition2Offset));
}

Fixed version (ground truth):
/** 
 * Verifies that when we call connect that it makes the appropriate calls to ConsumerStateManager to initialize. This test has the ConsumerStateManager (a mock) return ConsumerState for every partition on the topic. We verify that our internal kafka client then knows to start reading from the previously saved consumer state offsets
 */
@Test public void testConnectWithMultiplePartitionsOnTopicWithAllPreviouslySavedState(){
  final String consumerId="String_Node_Str";
  final TopicPartition partition0=new TopicPartition(topicName,0);
  final TopicPartition partition1=new TopicPartition(topicName,1);
  final TopicPartition partition2=new TopicPartition(topicName,2);
  final long lastCommittedOffsetPartition0=1234L;
  final long lastCommittedOffsetPartition1=4321L;
  final long lastCommittedOffsetPartition2=1337L;
  final long expectedPartition0Offset=lastCommittedOffsetPartition0 + 1;
  final long expectedPartition1Offset=lastCommittedOffsetPartition1 + 1;
  final long expectedPartition2Offset=lastCommittedOffsetPartition2 + 1;
  List<String> brokerHosts=Lists.newArrayList(kafkaTestServer.getKafkaServer().serverConfig().advertisedHostName() + "String_Node_Str" + kafkaTestServer.getKafkaServer().serverConfig().advertisedPort());
  final SidelineConsumerConfig config=new SidelineConsumerConfig(brokerHosts,consumerId,topicName);
  KafkaConsumer<byte[],byte[]> mockKafkaConsumer=mock(KafkaConsumer.class);
  List<PartitionInfo> mockPartitionInfos=Lists.newArrayList(new PartitionInfo(topicName,0,new Node(0,"String_Node_Str",9092),new Node[0],new Node[0]),new PartitionInfo(topicName,1,new Node(0,"String_Node_Str",9092),new Node[0],new Node[0]),new PartitionInfo(topicName,2,new Node(0,"String_Node_Str",9092),new Node[0],new Node[0]));
  when(mockKafkaConsumer.partitionsFor(eq(topicName))).thenReturn(mockPartitionInfos);
  PersistenceManager mockPersistenceManager=mock(PersistenceManager.class);
  ConsumerState consumerState=new ConsumerState();
  consumerState.setOffset(partition0,lastCommittedOffsetPartition0);
  consumerState.setOffset(partition1,lastCommittedOffsetPartition1);
  consumerState.setOffset(partition2,lastCommittedOffsetPartition2);
  when(mockPersistenceManager.retrieveConsumerState(eq(consumerId))).thenReturn(consumerState);
  SidelineConsumer sidelineConsumer=new SidelineConsumer(config,mockPersistenceManager,mockKafkaConsumer);
  sidelineConsumer.open(null);
  verify(mockKafkaConsumer,times(1)).assign(eq(Lists.newArrayList(partition0,partition1,partition2)));
  verify(mockKafkaConsumer,never()).seekToBeginning(anyList());
  InOrder inOrderVerification=inOrder(mockKafkaConsumer);
  inOrderVerification.verify(mockKafkaConsumer,times(1)).seek(eq(partition0),eq(expectedPartition0Offset));
  inOrderVerification.verify(mockKafkaConsumer,times(1)).seek(eq(partition1),eq(expectedPartition1Offset));
  inOrderVerification.verify(mockKafkaConsumer,times(1)).seek(eq(partition2),eq(expectedPartition2Offset));
}