@Test
  public void testLocalDanglingFilesCleaning() throws Exception {
    HiveConf conf = new HiveConfForTest(getClass());
    conf.set("fs.default.name", "file:///");
    String tmpDir = System.getProperty("test.tmp.dir");
    conf.set("hive.exec.scratchdir", tmpDir + "/hive-27317-hdfsscratchdir");
    conf.set("hive.exec.local.scratchdir", tmpDir + "/hive-27317-localscratchdir");
    FileSystem fs = FileSystem.get(conf);

    // Constants
    String appId = "appId_" + System.currentTimeMillis();
    String userName = System.getProperty("user.name");
    String hdfs = "hdfs";
    String inuse = "inuse.lck";
    String l = File.separator;

    // Simulating hdfs dangling dir and its inuse.lck file
    // Note: Give scratch dirs all the write permissions
    FsPermission allPermissions = new FsPermission((short)00777);
    customScratchDir = new Path(HiveConf.getVar(conf, HiveConf.ConfVars.SCRATCH_DIR));
    Utilities.createDirsWithPermission(conf, customScratchDir, allPermissions, true);
    Path hdfsRootDir = new Path(customScratchDir + l + userName + l + hdfs);
    Path hdfsSessionDir = new Path(hdfsRootDir + l + userName + l + appId);
    Path hdfsSessionLock = new Path(hdfsSessionDir + l + inuse);
    fs.create(hdfsSessionLock);

    // Simulating local dangling files
    customLocalTmpDir = new Path (HiveConf.getVar(conf, HiveConf.ConfVars.LOCAL_SCRATCH_DIR));
    Path localSessionDir = new Path(customLocalTmpDir + l + appId);
    Path localPipeOutFileRemove = new Path(customLocalTmpDir + l
            + appId + "-started-with-session-name.pipeout");
    Path localPipeOutFileNotRemove = new Path(customLocalTmpDir + l
            + "not-started-with-session-name-" + appId + ".pipeout");
    Path localPipeOutFileFailRemove = new Path(customLocalTmpDir + l
            + appId + "-started-with-session-name-but-fail-delete.pipeout");

    // Create dirs/files
    Utilities.createDirsWithPermission(conf, localSessionDir, allPermissions, true);
    fs.create(localPipeOutFileRemove);
    fs.create(localPipeOutFileNotRemove);
    fs.create(localPipeOutFileFailRemove);

    // Set permission for localPipeOutFileFailRemove file as not writable
    // This will avoid file to be deleted as we check whether it is writable or not first
    fs.setPermission(localPipeOutFileFailRemove, FsPermission.valueOf("-r--r--r--"));

    // The main service will be identifying which session files/dirs are dangling
    ClearDanglingScratchDir clearDanglingScratchDirMain = new ClearDanglingScratchDir(false,
            false, true, hdfsRootDir.toString(), conf);
    clearDanglingScratchDirMain.run();

    // localSessionDir and localPipeOutFileRemove should be removed
    // localPipeOutFileNotRemove and localPipeOutFileFailRemove should not be removed
    Assert.assertFalse("Local session dir '" + localSessionDir
            + "' still exists, should have been removed!", fs.exists(localSessionDir));
    Assert.assertFalse("Local .pipeout file '" + localPipeOutFileRemove
            + "' still exists, should have been removed!", fs.exists(localPipeOutFileRemove));
    Assert.assertTrue("Local .pipeout file '" + localPipeOutFileNotRemove
            + "' does not exist, should have not been removed!", fs.exists(localPipeOutFileNotRemove));
    Assert.assertTrue("Local .pipeout file '" + localPipeOutFileFailRemove
            + "' does not exist, should have not been removed!", fs.exists(localPipeOutFileFailRemove));
  }
Testing behaviour of ClearDanglingScratchDir service over local tmp files/dirs
@throws Exception
itests/hive-unit/src/test/java/org/apache/hadoop/hive/ql/session/TestClearDanglingScratchDir.java