CgroupsLCEResourcesHandler implements LCEResourcesHandler { @VisibleForTesting boolean checkAndDeleteCgroup(File cgf) throws InterruptedException { boolean deleted = false; try (FileInputStream in = new FileInputStream(cgf + "/tasks")) { if (in.read() == -1) { Thread.sleep(deleteCgroupDelay); deleted = cgf.delete(); if (!deleted) { LOG.warn("Failed attempt to delete cgroup: " + cgf); } } else { logLineFromTasksFile(cgf); } } catch (IOException e) { LOG.warn("Failed to read cgroup tasks file. ", e); } return deleted; } CgroupsLCEResourcesHandler(); @Override void setConf(Configuration conf); @Override Configuration getConf(); void init(LinuxContainerExecutor lce); void preExecute(ContainerId containerId, Resource containerResource); void postExecute(ContainerId containerId); String getResourcesOption(ContainerId containerId);  }
@Test public void testcheckAndDeleteCgroup() throws Exception { CgroupsLCEResourcesHandler handler = new CgroupsLCEResourcesHandler(); handler.setConf(new YarnConfiguration()); handler.initConfig(); FileUtils.deleteQuietly(cgroupDir); Assert.assertFalse(handler.checkAndDeleteCgroup(cgroupDir)); File tfile = new File(cgroupDir.getAbsolutePath(), "tasks"); FileOutputStream fos = FileUtils.openOutputStream(tfile); File fspy = Mockito.spy(cgroupDir); Mockito.stub(fspy.delete()).toReturn(true); Assert.assertTrue(handler.checkAndDeleteCgroup(fspy)); fos.write("1234".getBytes()); fos.close(); Assert.assertFalse(handler.checkAndDeleteCgroup(fspy)); FileUtils.deleteQuietly(cgroupDir); }