@Test
public void testMerge() throws WriteProcessException, QueryProcessException, IllegalPathException {

  mergeLock = new AtomicLong(0);
  for (int j = 21; j <= 30; j++) {
    TSRecord record = new TSRecord(j, deviceId);
    record.addTuple(DataPoint.getDataPoint(TSDataType.INT32, measurementId, String.valueOf(j)));
    insertToStorageGroupProcessor(record);
    processor.asyncCloseAllWorkingTsFileProcessors();
  }
  processor.syncCloseAllWorkingTsFileProcessors();
  for (int j = 10; j >= 1; j--) {
    TSRecord record = new TSRecord(j, deviceId);
    record.addTuple(DataPoint.getDataPoint(TSDataType.INT32, measurementId, String.valueOf(j)));
    insertToStorageGroupProcessor(record);
    processor.asyncCloseAllWorkingTsFileProcessors();
  }
  processor.syncCloseAllWorkingTsFileProcessors();
  processor.merge(true);
  while (mergeLock.get() == 0) {
    // wait
  }
  QueryDataSource queryDataSource = processor.query(new PartialPath(deviceId), measurementId, context,
      null, null);
  Assert.assertEquals(10, queryDataSource.getSeqResources().size());
  Assert.assertEquals(0, queryDataSource.getUnseqResources().size());
  for (TsFileResource resource : queryDataSource.getSeqResources()) {
    Assert.assertTrue(resource.isClosed());
  }
  for (TsFileResource resource : queryDataSource.getUnseqResources()) {
    Assert.assertTrue(resource.isClosed());
  }
}