Program: 2136
Project: 2136
Buggy source:
@Test(groups="String_Node_Str") public void testJsonClientIntegration() throws Exception {
  final RESTEventService service=createService(new JsonEventSerializer());
  Assert.assertEquals(processor.getEventsReceived(),0);
  Assert.assertEquals(timelineEventHandler.getEventsDiscarded(),0);
  final UUID hostUUID=UUID.randomUUID();
  final String hostName=hostUUID.toString();
  final int eventTypeId=timelineDAO.getOrAddEventCategory(EVENT_TYPE);
  final Integer hostId=timelineDAO.getOrAddHost(hostName);
  Assert.assertNotNull(hostId);
  final Integer minHeapUserKindId=timelineDAO.getOrAddSampleKind(hostId,eventTypeId,MIN_HEAPUSED_KIND);
  Assert.assertNotNull(minHeapUserKindId);
  final Integer maxHeapUserKindId=timelineDAO.getOrAddSampleKind(hostId,eventTypeId,MAX_HEAPUSED_KIND);
  Assert.assertNotNull(maxHeapUserKindId);
  final DateTime startTime=new DateTime(DateTimeZone.UTC);
  DateTime endTime=startTime;
  final int sampleCount=10;
  for (int i=0; i < sampleCount; i++) {
    endTime=startTime.plusMinutes(i);
    final MapEvent event=createEvent(hostUUID,endTime.getMillis());
    service.sendREST(event);
    Assert.assertEquals(processor.getEventsReceived(),1 + i);
    Assert.assertEquals(timelineEventHandler.getEventsDiscarded(),0);
    Assert.assertEquals(timelineEventHandler.getAccumulators().size(),1);
    final BiMap<Integer,String> hosts=timelineDAO.getHosts();
    Assert.assertEquals(hosts.values().size(),1);
    Assert.assertEquals(hosts.values().toArray()[0],hostName);
    final BiMap<Integer,CategoryIdAndSampleKind> categoryIdsAndSampleKinds=timelineDAO.getSampleKinds();
    Assert.assertEquals(categoryIdsAndSampleKinds.values().size(),event.getKeys().size());
    final List<String> sampleKinds=CategoryIdAndSampleKind.extractSampleKinds(categoryIdsAndSampleKinds.values());
    Assert.assertTrue(sampleKinds.contains(MIN_HEAPUSED_KIND));
    Assert.assertTrue(sampleKinds.contains(MAX_HEAPUSED_KIND));
  }
  timelineEventHandler.forceCommit(false);
  backgroundWriter.finishBackgroundWriting();
  final AccumulatorConsumer consumer=new AccumulatorConsumer();
  timelineDAO.getSamplesByHostIdsAndSampleKindIds(ImmutableList.<Integer>of(hostId),ImmutableList.<Integer>of(minHeapUserKindId,maxHeapUserKindId),startTime,endTime,consumer);
  final List<TimelineChunk> chunks=consumer.getAccumulator();
  Assert.assertEquals(chunks.size(),2);
  Assert.assertEquals((Integer)chunks.get(0).getHostId(),hostId);
  Assert.assertEquals((Integer)chunks.get(1).getHostId(),hostId);
  Assert.assertTrue((chunks.get(0).getSampleKindId() == minHeapUserKindId) || chunks.get(0).getSampleKindId() == maxHeapUserKindId);
  Assert.assertTrue((chunks.get(1).getSampleKindId() == minHeapUserKindId) || chunks.get(1).getSampleKindId() == maxHeapUserKindId);
  Assert.assertEquals(chunks.get(0).getHostId(),(int)hostId);
  Assert.assertEquals(chunks.get(1).getHostId(),(int)hostId);
  Assert.assertTrue(chunks.get(0).getSampleKindId() == minHeapUserKindId || chunks.get(0).getSampleKindId() == maxHeapUserKindId);
  Assert.assertTrue(chunks.get(1).getSampleKindId() == minHeapUserKindId || chunks.get(1).getSampleKindId() == maxHeapUserKindId);
  Assert.assertEquals(chunks.get(0).getSampleCount(),sampleCount);
  Assert.assertEquals(chunks.get(1).getSampleCount(),sampleCount);
  Assert.assertEquals(chunks.get(0).getHostId(),(int)hostId);
  Assert.assertEquals(chunks.get(1).getHostId(),(int)hostId);
  Assert.assertEquals(chunks.get(0).getStartTime().getMillis() / 1000,startTime.getMillis() / 1000);
  Assert.assertEquals(chunks.get(1).getStartTime().getMillis() / 1000,startTime.getMillis() / 1000);
  Assert.assertEquals(chunks.get(0).getEndTime().getMillis() / 1000,endTime.getMillis() / 1000);
  Assert.assertEquals(chunks.get(1).getEndTime().getMillis() / 1000,endTime.getMillis() / 1000);
  Assert.assertEquals(chunks.get(0).getSampleCount(),sampleCount);
  Assert.assertEquals(chunks.get(1).getSampleCount(),sampleCount);
  final TimeCursor timeCursor0=new TimeCursor(chunks.get(0).getTimes(),chunks.get(0).getSampleCount());
  final TimeCursor timeCursor1=new TimeCursor(chunks.get(1).getTimes(),chunks.get(1).getSampleCount());
  for (int i=0; i < sampleCount; i++) {
    Assert.assertEquals(timeCursor0.getNextTime(),(int)(startTime.plusMinutes(i).getMillis() / 1000));
    Assert.assertEquals(timeCursor1.getNextTime(),startTime.plusMinutes(i).getMillis() / 1000);
  }
}

Fixed version (ground truth):
@Test(groups="String_Node_Str") public void testJsonClientIntegration() throws Exception {
  final RESTEventService service=createService(new JsonEventSerializer());
  Assert.assertEquals(processor.getEventsReceived(),0);
  Assert.assertEquals(timelineEventHandler.getEventsDiscarded(),0);
  final UUID hostUUID=UUID.randomUUID();
  final String hostName=hostUUID.toString();
  final int eventTypeId=timelineDAO.getOrAddEventCategory(EVENT_TYPE);
  final Integer hostId=timelineDAO.getOrAddHost(hostName);
  Assert.assertNotNull(hostId);
  final Integer minHeapUserKindId=timelineDAO.getOrAddSampleKind(hostId,eventTypeId,MIN_HEAPUSED_KIND);
  Assert.assertNotNull(minHeapUserKindId);
  final Integer maxHeapUserKindId=timelineDAO.getOrAddSampleKind(hostId,eventTypeId,MAX_HEAPUSED_KIND);
  Assert.assertNotNull(maxHeapUserKindId);
  final DateTime startTime=new DateTime(DateTimeZone.UTC);
  DateTime endTime=startTime;
  final int sampleCount=10;
  for (int i=0; i < sampleCount; i++) {
    endTime=startTime.plusMinutes(i);
    final MapEvent event=createEvent(hostUUID,endTime.getMillis());
    service.sendREST(event);
    Assert.assertEquals(processor.getEventsReceived(),1 + i);
    Assert.assertEquals(timelineEventHandler.getEventsDiscarded(),0);
    Assert.assertEquals(timelineEventHandler.getAccumulators().size(),1);
    final BiMap<Integer,String> hosts=timelineDAO.getHosts();
    Assert.assertEquals(hosts.values().size(),1);
    Assert.assertEquals(hosts.values().toArray()[0],hostName);
    final BiMap<Integer,CategoryIdAndSampleKind> categoryIdsAndSampleKinds=timelineDAO.getSampleKinds();
    Assert.assertEquals(categoryIdsAndSampleKinds.values().size(),event.getKeys().size());
    final List<String> sampleKinds=CategoryIdAndSampleKind.extractSampleKinds(categoryIdsAndSampleKinds.values());
    Assert.assertTrue(sampleKinds.contains(MIN_HEAPUSED_KIND));
    Assert.assertTrue(sampleKinds.contains(MAX_HEAPUSED_KIND));
  }
  timelineEventHandler.forceCommit(false);
  backgroundWriter.initiateShutdown();
  final AccumulatorConsumer consumer=new AccumulatorConsumer();
  timelineDAO.getSamplesByHostIdsAndSampleKindIds(ImmutableList.<Integer>of(hostId),ImmutableList.<Integer>of(minHeapUserKindId,maxHeapUserKindId),startTime,endTime,consumer);
  final List<TimelineChunk> chunks=consumer.getAccumulator();
  Assert.assertEquals(chunks.size(),2);
  Assert.assertEquals((Integer)chunks.get(0).getHostId(),hostId);
  Assert.assertEquals((Integer)chunks.get(1).getHostId(),hostId);
  Assert.assertTrue((chunks.get(0).getSampleKindId() == minHeapUserKindId) || chunks.get(0).getSampleKindId() == maxHeapUserKindId);
  Assert.assertTrue((chunks.get(1).getSampleKindId() == minHeapUserKindId) || chunks.get(1).getSampleKindId() == maxHeapUserKindId);
  Assert.assertEquals(chunks.get(0).getHostId(),(int)hostId);
  Assert.assertEquals(chunks.get(1).getHostId(),(int)hostId);
  Assert.assertTrue(chunks.get(0).getSampleKindId() == minHeapUserKindId || chunks.get(0).getSampleKindId() == maxHeapUserKindId);
  Assert.assertTrue(chunks.get(1).getSampleKindId() == minHeapUserKindId || chunks.get(1).getSampleKindId() == maxHeapUserKindId);
  Assert.assertEquals(chunks.get(0).getSampleCount(),sampleCount);
  Assert.assertEquals(chunks.get(1).getSampleCount(),sampleCount);
  Assert.assertEquals(chunks.get(0).getHostId(),(int)hostId);
  Assert.assertEquals(chunks.get(1).getHostId(),(int)hostId);
  Assert.assertEquals(chunks.get(0).getStartTime().getMillis() / 1000,startTime.getMillis() / 1000);
  Assert.assertEquals(chunks.get(1).getStartTime().getMillis() / 1000,startTime.getMillis() / 1000);
  Assert.assertEquals(chunks.get(0).getEndTime().getMillis() / 1000,endTime.getMillis() / 1000);
  Assert.assertEquals(chunks.get(1).getEndTime().getMillis() / 1000,endTime.getMillis() / 1000);
  Assert.assertEquals(chunks.get(0).getSampleCount(),sampleCount);
  Assert.assertEquals(chunks.get(1).getSampleCount(),sampleCount);
  final TimeCursor timeCursor0=new TimeCursor(chunks.get(0).getTimes(),chunks.get(0).getSampleCount());
  final TimeCursor timeCursor1=new TimeCursor(chunks.get(1).getTimes(),chunks.get(1).getSampleCount());
  for (int i=0; i < sampleCount; i++) {
    Assert.assertEquals(timeCursor0.getNextTime(),(int)(startTime.plusMinutes(i).getMillis() / 1000));
    Assert.assertEquals(timeCursor1.getNextTime(),startTime.plusMinutes(i).getMillis() / 1000);
  }
}