Program: 37691
Project: 37691
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 testConnectWithMultiplePartitionsOnTopicWithSomePreviouslySavedState(){
  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 TopicPartition partition3=new TopicPartition(topicName,3);
  final long lastCommittedOffsetPartition0=1234L;
  final long lastCommittedOffsetPartition2=1337L;
  final long expectedPartition0Offset=lastCommittedOffsetPartition0 + 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]),new PartitionInfo(topicName,3,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(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,partition3)));
  verify(mockKafkaConsumer,times(1)).seekToBeginning(eq(Lists.newArrayList(partition1,partition3)));
  InOrder inOrderVerification=inOrder(mockKafkaConsumer);
  inOrderVerification.verify(mockKafkaConsumer,times(1)).seek(eq(partition0),eq(expectedPartition0Offset));
  inOrderVerification.verify(mockKafkaConsumer,times(1)).seek(eq(partition2),eq(expectedPartition2Offset));
  verify(mockKafkaConsumer,never()).seek(eq(partition1),anyLong());
  verify(mockKafkaConsumer,never()).seek(eq(partition3),anyLong());
}

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 testConnectWithMultiplePartitionsOnTopicWithSomePreviouslySavedState(){
  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 TopicPartition partition3=new TopicPartition(topicName,3);
  final long lastCommittedOffsetPartition0=1234L;
  final long lastCommittedOffsetPartition2=1337L;
  final long expectedPartition0Offset=lastCommittedOffsetPartition0 + 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]),new PartitionInfo(topicName,3,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(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,partition3)));
  verify(mockKafkaConsumer,times(1)).seekToBeginning(eq(Lists.newArrayList(partition1,partition3)));
  InOrder inOrderVerification=inOrder(mockKafkaConsumer);
  inOrderVerification.verify(mockKafkaConsumer,times(1)).seek(eq(partition0),eq(expectedPartition0Offset));
  inOrderVerification.verify(mockKafkaConsumer,times(1)).seek(eq(partition2),eq(expectedPartition2Offset));
  verify(mockKafkaConsumer,never()).seek(eq(partition1),anyLong());
  verify(mockKafkaConsumer,never()).seek(eq(partition3),anyLong());
}