SecurityAnalysisExecutionHandlers { public static ExecutionHandler<SecurityAnalysisResult> distributed(SecurityAnalysisExecutionInput input, int subtaskCount) { Preconditions.checkArgument(subtaskCount >= 1, TASK_COUNT_ERROR_MESSAGE, subtaskCount); return new SecurityAnalysisExecutionHandler<>(workingDir -> readResults(workingDir, subtaskCount), (workingDir, options) -> distributedOptions(workingDir, options, subtaskCount), (workingDir, cause) -> generateExceptionWithLogs(workingDir, cause, subtaskCount), subtaskCount, input); } private  SecurityAnalysisExecutionHandlers(); static ExecutionHandler<SecurityAnalysisResult> forwarded(SecurityAnalysisExecutionInput input); static ExecutionHandler<SecurityAnalysisResult> forwarded(SecurityAnalysisExecutionInput input, Integer forwardedTaskCount); static ExecutionHandler<SecurityAnalysisResultWithLog> forwardedWithLogs(SecurityAnalysisExecutionInput input); static ExecutionHandler<SecurityAnalysisResultWithLog> forwardedWithLogs(SecurityAnalysisExecutionInput input, Integer forwardedTaskCount); static ExecutionHandler<SecurityAnalysisResult> distributed(SecurityAnalysisExecutionInput input, int subtaskCount); static ExecutionHandler<SecurityAnalysisResultWithLog> distributedWithLog(SecurityAnalysisExecutionInput input, int subtaskCount); static SecurityAnalysisResult readSingleResult(Path workingDir); static SecurityAnalysisResultWithLog readSingleResultWithLogs(Path workingDir); static void forwardedOptions(Path workingDir, SecurityAnalysisCommandOptions options, Integer taskCount); static void forwardedWithLogsOptions(Path workingDir, SecurityAnalysisCommandOptions options, Integer taskCount); static void distributedOptions(Path workingDir, SecurityAnalysisCommandOptions options, int taskCount); static void distributedWithLogsOptions(Path workingDir, SecurityAnalysisCommandOptions options, int taskCount); static Path getOutputPathForTask(Path workingDir, int taskIndex); static SecurityAnalysisResult readResults(Path workingDir, int subtaskCount); static SecurityAnalysisResultWithLog readResultsWithLogs(Path workingDir, int subtaskCount); static Path getLogPathForTask(Path workingDir, int taskNumber); static Path getLogPath(Path workingDir);  }
@Test public void distributedBefore() throws IOException { SecurityAnalysisExecutionInput input = new SecurityAnalysisExecutionInput() .setParameters(new SecurityAnalysisParameters()) .setNetworkVariant(EurostagTutorialExample1Factory.create(), VariantManagerConstants.INITIAL_VARIANT_ID) .setContingenciesSource(ByteSource.wrap("contingencies definition".getBytes(StandardCharsets.UTF_8))) .addResultExtensions(ImmutableList.of("ext1", "ext2")) .addViolationTypes(ImmutableList.of(LimitViolationType.CURRENT)); ExecutionHandler<SecurityAnalysisResult> handler = SecurityAnalysisExecutionHandlers.distributed(input, 3); List<CommandExecution> commandExecutions = handler.before(workingDir); SimpleCommand command = (SimpleCommand) commandExecutions.get(0).getCommand(); List<String> args = command.getArgs(0); assertThat(command.getArgs(0).subList(1, args.size())) .containsExactlyInAnyOrder("--case-file=/work/network.xiidm", "--parameters-file=/work/parameters.json", "--output-file=/work/task_0_result.json", "--output-format=JSON", "--contingencies-file=/work/contingencies.groovy", "--with-extensions=ext1,ext2", "--limit-types=CURRENT", "--task=1/3"); assertThat(command.getArgs(1).subList(1, args.size())) .containsExactlyInAnyOrder("--case-file=/work/network.xiidm", "--parameters-file=/work/parameters.json", "--output-file=/work/task_1_result.json", "--output-format=JSON", "--contingencies-file=/work/contingencies.groovy", "--with-extensions=ext1,ext2", "--limit-types=CURRENT", "--task=2/3"); }
@Test public void distributedAfter() throws IOException { JsonSecurityAnalysisResultExporter exporter = new JsonSecurityAnalysisResultExporter(); try (Writer writer = Files.newBufferedWriter(workingDir.resolve("task_0_result.json"))) { exporter.export(resultForContingency("c1"), writer); } SecurityAnalysisExecutionInput input = new SecurityAnalysisExecutionInput(); ExecutionHandler<SecurityAnalysisResult> handler3 = SecurityAnalysisExecutionHandlers.distributed(input, 2); assertThatExceptionOfType(ComputationException.class).isThrownBy(() -> { Command cmd = Mockito.mock(Command.class); handler3.after(workingDir, new DefaultExecutionReport(Collections.singletonList(new ExecutionError(cmd, 0, 42)))); }) .withMessageContaining("An error occurred during security analysis command execution") .withStackTraceContaining("Error during the execution in directory /work exit codes: Task 0 : 42"); try (Writer writer = Files.newBufferedWriter(workingDir.resolve("task_1_result.json"))) { exporter.export(resultForContingency("c2"), writer); } ExecutionHandler<SecurityAnalysisResult> handler = SecurityAnalysisExecutionHandlers.distributed(input, 2); SecurityAnalysisResult result = handler.after(workingDir, new DefaultExecutionReport()); assertNotNull(result); assertTrue(result.getPreContingencyResult().isComputationOk()); assertTrue(result.getPreContingencyResult().getLimitViolations().isEmpty()); assertEquals(2, result.getPostContingencyResults().size()); assertEquals("c1", result.getPostContingencyResults().get(0).getContingency().getId()); assertEquals("c2", result.getPostContingencyResults().get(1).getContingency().getId()); }