@Test
public void recover() throws IOException, MetadataException {

  Map<String, Map<Long, Set<File>>> allFileList = new HashMap<>();
  for (int i = 0; i < 3; i++) {
    IoTDB.metaManager.setStorageGroup(new PartialPath(getSgName(i)));
  }
  Random r = new Random(0);
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 5; j++) {
      allFileList.computeIfAbsent(getSgName(i), k -> new HashMap<>()).computeIfAbsent(0L, k -> new HashSet<>());
      String rand = r.nextInt(10000) + TSFILE_SUFFIX;
      String fileName = FilePathUtils.regularizePath(dataDir) + IoTDBConstant.SEQUENCE_FLODER_NAME
          + File.separator + getSgName(i) + File.separator + "0" + File.separator + rand;
      File file = new File(fileName);
      allFileList.get(getSgName(i)).get(0L).add(file);
      if (!file.getParentFile().exists()) {
        file.getParentFile().mkdirs();
      }
      if (!file.exists() && !file.createNewFile()) {
        LOGGER.error("Can not create new file {}", file.getPath());
      }
      if (!new File(file.getAbsolutePath() + TsFileResource.RESOURCE_SUFFIX).exists()
          && !new File(file.getAbsolutePath() + TsFileResource.RESOURCE_SUFFIX).createNewFile()) {
        LOGGER.error("Can not create new file {}", file.getPath());
      }
    }
  }
  manager.getValidFiles(dataDir);
  assertTrue(SyncUtils.isEmpty(manager.getLastLocalFilesMap()));
  senderLogger.startSyncTsFiles();
  for (Map<Long, Set<File>> map : allFileList.values()) {
    for (Set<File> newTsFiles : map.values()) {
      for (File file : newTsFiles) {
        senderLogger.finishSyncTsfile(file);
      }
    }
  }
  senderLogger.close();
  // recover log
  senderLogAnalyzer.recover();
  manager.getValidFiles(dataDir);
  assertFalse(SyncUtils.isEmpty(manager.getLastLocalFilesMap()));
  Map<String, Map<Long, Set<File>>> lastFilesMap = manager.getLastLocalFilesMap();
  assertFileMap(allFileList, lastFilesMap);
  // delete some files
  assertFalse(new File(config.getSenderFolderPath(), SyncConstant.SYNC_LOG_NAME).exists());
  senderLogger = new SyncSenderLogger(
      new File(config.getSenderFolderPath(), SyncConstant.SYNC_LOG_NAME));
  manager.getValidFiles(dataDir);
  assertFalse(SyncUtils.isEmpty(manager.getLastLocalFilesMap()));
  senderLogger.startSyncDeletedFilesName();
  for (Map<Long, Set<File>> map : allFileList.values()) {
    for (Set<File> newTsFiles : map.values()) {
      for (File file : newTsFiles) {
        senderLogger.finishSyncDeletedFileName(file);
      }
    }
  }
  senderLogger.close();
  // recover log
  senderLogAnalyzer.recover();
  manager.getValidFiles(dataDir);
  assertTrue(SyncUtils.isEmpty(manager.getLastLocalFilesMap()));
  assertTrue(SyncUtils.isEmpty(manager.getDeletedFilesMap()));
  Map<String, Map<Long, Set<File>>> toBeSyncedFilesMap = manager.getToBeSyncedFilesMap();
  assertFileMap(allFileList, toBeSyncedFilesMap);
}