From ecf115dd7803a476453bc3b58593fff1b3c19ab7 Mon Sep 17 00:00:00 2001 From: Nicolas Rol Date: Tue, 1 Oct 2024 16:04:42 +0200 Subject: [PATCH] add tests Signed-off-by: Nicolas Rol --- .../slurm/LocalCommandExecutorTest.java | 32 +++++++++++++++++++ .../computation/slurm/TaskStoreTest.java | 27 ++++++++++++++-- 2 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 computation-slurm/src/test/java/com/powsybl/computation/slurm/LocalCommandExecutorTest.java diff --git a/computation-slurm/src/test/java/com/powsybl/computation/slurm/LocalCommandExecutorTest.java b/computation-slurm/src/test/java/com/powsybl/computation/slurm/LocalCommandExecutorTest.java new file mode 100644 index 0000000..a9dab7e --- /dev/null +++ b/computation-slurm/src/test/java/com/powsybl/computation/slurm/LocalCommandExecutorTest.java @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2024, RTE (http://www.rte-france.com) + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * SPDX-License-Identifier: MPL-2.0 + */ + +package com.powsybl.computation.slurm; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.fail; + +/** + * @author Nicolas Rol {@literal } + */ +class LocalCommandExecutorTest { + + @Test + void test() { + try (CommandExecutor commandExecutor = new LocalCommandExecutor()) { + CommandResult commandResult = commandExecutor.execute("echo hello"); + assertEquals(0, commandResult.exitCode()); + assertEquals("hello" + System.lineSeparator(), commandResult.stdOut()); + assertEquals("", commandResult.stdErr()); + } catch (Exception e) { + fail("Unexpected exception: " + e); + } + } +} diff --git a/computation-slurm/src/test/java/com/powsybl/computation/slurm/TaskStoreTest.java b/computation-slurm/src/test/java/com/powsybl/computation/slurm/TaskStoreTest.java index 367444d..ce7eb93 100644 --- a/computation-slurm/src/test/java/com/powsybl/computation/slurm/TaskStoreTest.java +++ b/computation-slurm/src/test/java/com/powsybl/computation/slurm/TaskStoreTest.java @@ -11,10 +11,10 @@ import org.mockito.Mockito; import java.util.Collections; +import java.util.concurrent.atomic.AtomicInteger; import static org.junit.jupiter.api.Assertions.*; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; +import static org.mockito.Mockito.*; /** * @author Yichen Tang {@literal } @@ -29,11 +29,13 @@ void test() { TaskStore store = new TaskStore(); + assertTrue(store.isEmpty()); assertTrue(store.getTasks().isEmpty()); assertTrue(store.getPendingJobs().isEmpty()); store.add(task); + assertFalse(store.isEmpty()); assertEquals(1, store.getTasks().size()); assertSame(task, store.getTasks().get(0)); assertEquals(1, store.getPendingJobs().size()); @@ -47,4 +49,25 @@ void test() { assertTrue(store.getPendingJobs().isEmpty()); } + @Test + void testInterrupt() { + AtomicInteger jobsInterrupted = new AtomicInteger(0); + // Task and job + SlurmTaskImpl task = mock(SlurmTaskImpl.class); + MonitoredJob job = Mockito.mock(MonitoredJob.class); + when(task.getPendingJobs()).thenReturn(Collections.singletonList(job)); + doAnswer(invocation -> { + jobsInterrupted.incrementAndGet(); + return null; + }).when(task).interrupt(); + + // Store + TaskStore store = new TaskStore(); + store.add(task); + + // Test + assertFalse(store.isEmpty()); + store.interruptAll(); + assertEquals(1, jobsInterrupted.get()); + } }