Repository: apache/flink
File: flink-runtime/src/test/java/org/apache/flink/runtime/util/profiler/ProfilingServiceTest.java (Java)

Code before:
        }
        // Due to the configuration of MAX_PROFILING_HISTORY_SIZE=2,
        // the profiling result directory shouldn't contain more than 2 files.
        verifyRollingDeletionWorks();
    }

    /**
     * Trigger a specific profiling instance for testing.
     *
     * @param mode target profiling mode
     * @param duration target profiling duration
     * @param waitUntilFinished flag of waiting for profiling finished or not
     */
    private void requestSingleProfiling(
            ProfilingInfo.ProfilingMode mode, Long duration, Boolean waitUntilFinished)
            throws InterruptedException, ExecutionException {
        ProfilingInfo profilingInfo =
                profilingService.requestProfiling(RESOURCE_ID, duration, mode).get();
        if (isNoPermissionOrAllocateSymbol(profilingInfo)) {
            log.warn(
                    "Ignoring failed profiling instance in {} mode, which caused by {}.",
                    profilingInfo.getProfilingMode(),
                    profilingInfo.getMessage());
            return;
        }
        Assertions.assertEquals(
                ProfilingInfo.ProfilingStatus.RUNNING,
                profilingInfo.getStatus(),
                String.format(
                        "Submitting profiling request should be started successfully or failed by no permission, but got errorMsg=%s",
                        profilingInfo.getMessage()));
        if (waitUntilFinished) {
            waitForProfilingFinished();
            Assertions.assertEquals(
                    ProfilingInfo.ProfilingStatus.FINISHED,
                    profilingInfo.getStatus(),
                    String.format(
                            "Profiling request should complete successful, but got errorMsg=%s",
                            profilingInfo.getMessage()));
        }
    }

    private void verifyRollingDeletionWorks() {
        ArrayDeque<ProfilingInfo> profilingList =
                profilingService.getProfilingListForTest(RESOURCE_ID);
        // Profiling History shouldn't exceed history size limit.
        Assertions.assertTrue(profilingList.size() <= profilingService.getHistorySizeLimit());
        // Profiling History files should be rolling deleted.
        Set<String> resultFileNames = new HashSet<>();
        File configuredDir = new File(profilingService.getProfilingResultDir());
        for (File f : Objects.requireNonNull(configuredDir.listFiles())) {

Review comment (suggestion):
just use test name as part of prefix to make it unique per test and to avoid tests impacting each other while concurrent execution

Code after:
        for (int i = 0; i < 3; i++) {
            requestSingleProfiling(
                    testInfo, ProfilingInfo.ProfilingMode.ITIMER, DEFAULT_PROFILING_DURATION, true);
        }
        // Due to the configuration of MAX_PROFILING_HISTORY_SIZE=2,
        // the profiling result directory shouldn't contain more than 2 files.
        verifyRollingDeletionWorks(testInfo);
    }

    /**
     * Trigger a specific profiling instance for testing.
     *
     * @param mode target profiling mode
     * @param duration target profiling duration
     * @param waitUntilFinished flag of waiting for profiling finished or not
     */
    private void requestSingleProfiling(
            TestInfo testInfo,
            ProfilingInfo.ProfilingMode mode,
            Long duration,
            Boolean waitUntilFinished)
            throws InterruptedException, ExecutionException {
        ProfilingInfo profilingInfo =
                profilingService
                        .requestProfiling(
                                RESOURCE_ID + "_" + testInfo.getTestMethod().get().getName(),
                                duration,
                                mode)
                        .get();
        if (isNoPermissionOrAllocateSymbol(profilingInfo)) {
            log.warn(
                    "Ignoring failed profiling instance in {} mode, which caused by {}.",
                    profilingInfo.getProfilingMode(),
                    profilingInfo.getMessage());
            return;
        }
        Assertions.assertEquals(
                ProfilingInfo.ProfilingStatus.RUNNING,
                profilingInfo.getStatus(),
                String.format(
                        "Submitting profiling request should be started successfully or failed by no permission, but got errorMsg=%s",
                        profilingInfo.getMessage()));
        if (waitUntilFinished) {
            waitForProfilingFinished();
            Assertions.assertEquals(
                    ProfilingInfo.ProfilingStatus.FINISHED,
                    profilingInfo.getStatus(),
                    String.format(
                            "Profiling request should complete successful, but got errorMsg=%s",
                            profilingInfo.getMessage()));
        }