Program: 6954
Project: 6954
Buggy source:
@Test public void testInvalidFlowRunRecord() throws Exception {
  HttpResponse response=deploy(WordCountApp.class,Constants.Gateway.API_VERSION_3_TOKEN,TEST_NAMESPACE1);
  Assert.assertEquals(200,response.getStatusLine().getStatusCode());
  Id.Program wordcountFlow1=Id.Program.from(TEST_NAMESPACE1,"String_Node_Str",ProgramType.FLOW,"String_Node_Str");
  Assert.assertEquals("String_Node_Str",getProgramStatus(wordcountFlow1));
  startProgram(wordcountFlow1);
  waitState(wordcountFlow1,ProgramRunStatus.RUNNING.toString());
  List<RunRecord> runRecords=getProgramRuns(wordcountFlow1,ProgramRunStatus.RUNNING.toString());
  Assert.assertEquals(1,runRecords.size());
  RunRecord rr=runRecords.get(0);
  Assert.assertEquals(ProgramRunStatus.RUNNING,rr.getStatus());
  RuntimeInfo runtimeInfo=runtimeService.lookup(wordcountFlow1,RunIds.fromString(rr.getPid()));
  ProgramController programController=runtimeInfo.getController();
  programController.stop();
  Thread.sleep(2000);
  rr=store.getRun(wordcountFlow1,rr.getPid());
  Assert.assertEquals(ProgramRunStatus.KILLED,rr.getStatus());
  long now=System.currentTimeMillis();
  long nowSecs=TimeUnit.MILLISECONDS.toSeconds(now);
  store.setStart(wordcountFlow1,rr.getPid(),nowSecs);
  rr=store.getRun(wordcountFlow1,rr.getPid());
  Assert.assertEquals(ProgramRunStatus.RUNNING,rr.getStatus());
  runRecords=getProgramRuns(wordcountFlow1,ProgramRunStatus.FAILED.toString());
  Assert.assertEquals(0,runRecords.size());
  programLifecycleService.validateAndCorrectRunningRunRecords(ProgramType.FLOW);
  runRecords=getProgramRuns(wordcountFlow1,ProgramRunStatus.FAILED.toString());
  Assert.assertEquals(1,runRecords.size());
  rr=runRecords.get(0);
  Assert.assertEquals(ProgramRunStatus.FAILED,rr.getStatus());
}

Fixed version (ground truth):
@Test public void testInvalidFlowRunRecord() throws Exception {
  HttpResponse response=deploy(WordCountApp.class,Constants.Gateway.API_VERSION_3_TOKEN,TEST_NAMESPACE1);
  Assert.assertEquals(200,response.getStatusLine().getStatusCode());
  Id.Program wordcountFlow1=Id.Program.from(TEST_NAMESPACE1,"String_Node_Str",ProgramType.FLOW,"String_Node_Str");
  Assert.assertEquals("String_Node_Str",getProgramStatus(wordcountFlow1));
  startProgram(wordcountFlow1);
  waitState(wordcountFlow1,ProgramRunStatus.RUNNING.toString());
  List<RunRecord> runRecords=getProgramRuns(wordcountFlow1,ProgramRunStatus.RUNNING.toString());
  Assert.assertEquals(1,runRecords.size());
  RunRecord rr=runRecords.get(0);
  Assert.assertEquals(ProgramRunStatus.RUNNING,rr.getStatus());
  RuntimeInfo runtimeInfo=runtimeService.lookup(wordcountFlow1,RunIds.fromString(rr.getPid()));
  ProgramController programController=runtimeInfo.getController();
  programController.stop();
  Thread.sleep(2000);
  rr=store.getRun(wordcountFlow1,rr.getPid());
  Assert.assertEquals(ProgramRunStatus.KILLED,rr.getStatus());
  long now=System.currentTimeMillis();
  long nowSecs=TimeUnit.MILLISECONDS.toSeconds(now);
  store.setStart(wordcountFlow1,rr.getPid(),nowSecs);
  rr=store.getRun(wordcountFlow1,rr.getPid());
  Assert.assertEquals(ProgramRunStatus.RUNNING,rr.getStatus());
  runRecords=getProgramRuns(wordcountFlow1,ProgramRunStatus.FAILED.toString());
  Assert.assertEquals(0,runRecords.size());
  Set<String> processedInvalidRunRecordIds=Sets.newHashSet();
  programLifecycleService.validateAndCorrectRunningRunRecords(ProgramType.FLOW,processedInvalidRunRecordIds);
  runRecords=getProgramRuns(wordcountFlow1,ProgramRunStatus.FAILED.toString());
  Assert.assertEquals(1,runRecords.size());
  rr=runRecords.get(0);
  Assert.assertEquals(ProgramRunStatus.FAILED,rr.getStatus());
}