jenkins執行單元測試,會產生大量臨時文件,要及時刪除,不然會把inode耗盡


  jenkins的build命令:clean test -U findbugs:findbugs pmd:pmd sonar:sonar -Djava.io.tmpdir=/tmp/ -Dsonar.projectKey=xxxxx -Dsonar.projectName=xxxxxx -Dsonar.branch=xxxxx,這條命令執行單測的時候,會產生大量的臨時文件到linux的/tmp/目錄,日積月累,會最終消耗殆盡inode,從而不能使用硬盤再創建文件和文件夾

 

  原因:在做java單元測試的時候,創建了一些臨時文件,沒有及時刪除:https://garygregory.wordpress.com/2010/01/20/junit-tip-use-rules-to-manage-temporary-files-and-folders/

  You can remove the burden of deleting temporary files and folders from your test code by using a JUnit TemporaryFolder Rule. Rules themselves are new in JUnit 4.7 and are used to change the behavior of tests. For example, the following test creates and deletes a temporary file and folder:

  In order to enable this feature, you must use the annotation @Rule on the declaration of the TemporaryFolder instance variable. The TemporaryFolder creates a folder in the default temporary file directory specified by the system property java.io.tmpdir. The method newFile creates a new file in the temporary directory and newFolder creates a new folder.

  When the test method finishes, JUnit automatically deletes all files and directories in and including the TemporaryFolder. JUnit guarantees to delete the resources, whether the test passes or fails.

package test;
 
import java.io.File;
import java.io.IOException;
 
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
 
public class TestTemporaryFolderRule {
    @Rule
    public TemporaryFolder testFolder = new TemporaryFolder();
 
    @Test
    public void testInTempFolder() throws IOException {
        File tempFile = testFolder.newFile("file.txt");
        File tempFolder = testFolder.newFolder("folder");
        System.out.println("Test folder: " + testFolder.getRoot());
        // test...
    }
}

 

  

What is the correct way to write to temp file during unit tests with Maven?

Tests fail when java.io.tmpdir does not exist:https://ops4j1.jira.com/browse/PAXEXAM-294

  

Java.io.tmpdir介紹: https://www.cnblogs.com/nbjin/p/7392541.html

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM