gradle 之分支进程执行分析

  1. @Override
  2. public void execute(final JvmTestExecutionSpec testExecutionSpec, TestResultProcessor testResultProcessor) {
  3. // 获取测试框架类型,junit或testng或junit5
  4. final TestFramework testFramework = testExecutionSpec.getTestFramework();
  5. // 返回一个工厂,用于在每个工作进程中创建一个 TestClassProcessor。 这个工厂被序列化到工作进程,然后它的 WorkerTestClassProcessorFactory.create(org.gradle.internal.service.ServiceRegistry) 方法被调用来创建测试处理器。
  6. final WorkerTestClassProcessorFactory testInstanceFactory = testFramework.getProcessorFactory();
  7. final WorkerLeaseRegistry.WorkerLease currentWorkerLease = workerLeaseRegistry.getCurrentWorkerLease();
  8. final Set<File> classpath = ImmutableSet.copyOf(testExecutionSpec.getClasspath());
  9. final Set<File> modulePath = ImmutableSet.copyOf(testExecutionSpec.getModulePath());
  10. final List<String> testWorkerImplementationModules = testFramework.getTestWorkerImplementationModules();
  11. final Factory<TestClassProcessor> forkingProcessorFactory = new Factory<TestClassProcessor>() {
  12. @Override
  13. public TestClassProcessor create() {
  14. return new ForkingTestClassProcessor(currentWorkerLease, workerFactory, testInstanceFactory, testExecutionSpec.getJavaForkOptions(),
  15. classpath, modulePath, testWorkerImplementationModules, testFramework.getWorkerConfigurationAction(), moduleRegistry, documentationRegistry);
  16. }
  17. };
  18. final Factory<TestClassProcessor> reforkingProcessorFactory = new Factory<TestClassProcessor>() {
  19. @Override
  20. public TestClassProcessor create() {
  21. return new RestartEveryNTestClassProcessor(forkingProcessorFactory, testExecutionSpec.getForkEvery());
  22. }
  23. };
  24. processor =
  25. new PatternMatchTestClassProcessor(testFilter,
  26. new RunPreviousFailedFirstTestClassProcessor(testExecutionSpec.getPreviousFailedTestClasses(),
  27. new MaxNParallelTestClassProcessor(getMaxParallelForks(testExecutionSpec), reforkingProcessorFactory, actorFactory)));
  28. final FileTree testClassFiles = testExecutionSpec.getCandidateClassFiles();
  29. Runnable detector;
  30. if (testExecutionSpec.isScanForTestClasses() && testFramework.getDetector() != null) {
  31. TestFrameworkDetector testFrameworkDetector = testFramework.getDetector();
  32. testFrameworkDetector.setTestClasses(testExecutionSpec.getTestClassesDirs().getFiles());
  33. testFrameworkDetector.setTestClasspath(classpath);
  34. detector = new DefaultTestClassScanner(testClassFiles, testFrameworkDetector, processor);
  35. } else {
  36. detector = new DefaultTestClassScanner(testClassFiles, null, processor);
  37. }
  38. new TestMainAction(detector, processor, testResultProcessor, clock, testExecutionSpec.getPath(), "Gradle Test Run " + testExecutionSpec.getIdentityPath()).run();
  39. }