Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
Signed-off-by: Nicolas Rol <[email protected]>
  • Loading branch information
rolnico committed Oct 1, 2024
1 parent 2c78a31 commit ecf115d
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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 <nicolas.rol at rte-france.com>}
*/
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 <yichen.tang at rte-france.com>}
Expand All @@ -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());
Expand All @@ -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());
}
}

0 comments on commit ecf115d

Please sign in to comment.