OpenLiberty / liberty-tools-intellij

IntelliJ IDEA extension for Liberty
https://plugins.jetbrains.com/plugin/14856-open-liberty-tools
Eclipse Public License 2.0
12 stars 24 forks source link

Jakarta EE unit tests: Investigate if there are better solutions than using Thread.sleep() when creating the Maven module. #743

Open mrglavas opened 4 months ago

mrglavas commented 4 months ago

Changes to the Maven test framework have driven us to make several updates to the base Jakarta EE test class:

  1. to accommodate changes to the Maven test framework API
  2. to resolve timing issues that were introduced when moving to newer versions of the Maven test framework

We should revisit the decision to use Thread.sleep() when creating the Maven module and hopefully find a better solution.

Original code borrowed from Quarkus Tools:

  protected List<Module> createMavenModules(List<File> projectDirs) throws Exception {
    Project project = myTestFixture.getProject();
    List<VirtualFile> pomFiles = new ArrayList<>();
    for(File projectDir : projectDirs) {
      File moduleDir = new File(project.getBasePath(), projectDir.getName() + counter++);
      FileUtils.copyDirectory(projectDir, moduleDir);
      VirtualFile pomFile = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(moduleDir).findFileByRelativePath("pom.xml");
      pomFiles.add(pomFile);

    }
    importProjects(pomFiles.toArray(VirtualFile[]::new));
    Module[] modules = ModuleManager.getInstance(myTestFixture.getProject()).getModules();
    for(Module module : modules) {
      setupJdkForModule(module.getName());
    }
    return Arrays.asList(modules).stream().skip(1).collect(Collectors.toList());
  }

Updates that have been required in order to use Maven test framework v241.15989.150:

    protected List<Module> createMavenModules(List<File> projectDirs) throws Exception {
        Project project = getTestFixture().getProject();
        List<VirtualFile> pomFiles = new ArrayList<>();
        for (File projectDir : projectDirs) {
            File moduleDir = new File(project.getBasePath(), projectDir.getName() + counter.getAndIncrement());
            FileUtils.copyDirectory(projectDir, moduleDir);
            VirtualFile pomFile = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(moduleDir).findFileByRelativePath("pom.xml");
            pomFiles.add(pomFile);

        }
        // Make a blocking call to the Kotlin suspend function: importProjectsAsync().
        BuildersKt.runBlocking(
                EmptyCoroutineContext.INSTANCE,
                (scope, continuation) -> importProjectsAsync(pomFiles.toArray(VirtualFile[]::new), continuation)
        );
        Module[] modules = ModuleManager.getInstance(getTestFixture().getProject()).getModules();
        for (Module module : modules) {
            setupJdkForModule(module.getName());
        }
        // REVISIT: After calling setupJdkForModule() initialization appears to continue in the background
        // and a may cause a test to intermittently fail if it accesses the module too early. A 10-second wait
        // is hopefully long enough but would be preferable to synchronize on a completion event if one is
        // ever introduced in the future.
        Thread.sleep(10000L);
        return Arrays.asList(modules).stream().skip(1).collect(Collectors.toList());
    }