Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use test name in GitCommandsExecutorTest test data #1246

Merged
merged 1 commit into from
Jan 17, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@
import java.util.concurrent.Callable;
import java.util.concurrent.atomic.AtomicBoolean;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
Expand All @@ -36,6 +39,9 @@ public class GitCommandsExecutorTest {
private final TaskListener listener;
private final ByteArrayOutputStream logStream;

@Rule
public TestName testName = new TestName();

public GitCommandsExecutorTest(int threads) {
this.threads = threads;
this.logStream = new ByteArrayOutputStream();
Expand All @@ -47,6 +53,15 @@ public void verifyCorrectExecutorServiceShutdown() {
loggedNoOutput();
}

private String value = null;
private String error = null;

@Before
public void defineValues() {
value = "some value " + testName.getMethodName();
error = "some error " + testName.getMethodName();
}

@Parameters(name = "threads={0}")
public static Iterable<Integer> threadsParameter() {
return asList(1, 2, 100);
Expand All @@ -56,7 +71,7 @@ public static Iterable<Integer> threadsParameter() {
public void allCommandsSucceed() throws Exception {
List<Callable<String>> commands = new ArrayList<>();
for (int i = 0; i < threads; i++) {
commands.add(successfulCommand("some value"));
commands.add(new GoodCommand(value));
}

new GitCommandsExecutor(threads, listener).invokeAll(commands);
Expand All @@ -70,29 +85,29 @@ public void allCommandsSucceed() throws Exception {
public void allCommandsFail() throws Exception {
List<Callable<String>> commands = new ArrayList<>();
for (int i = 0; i < threads; i++) {
commands.add(erroneousCommand(new RuntimeException("some error")));
commands.add(new BadCommand(new RuntimeException(error)));
}

Exception e = assertThrows(GitException.class, () -> {
new GitCommandsExecutor(threads, listener).invokeAll(commands);
});
assertThat(e.getMessage(), is("java.lang.RuntimeException: some error"));
assertThat(e.getMessage(), is("java.lang.RuntimeException: " + error));
assertThat(e.getCause(), instanceOf(RuntimeException.class));
}

@Test
public void firstCommandFails() throws Exception {
long commandExecutionTime = 60_000;
List<Callable<String>> commands = asList(
erroneousCommand(new RuntimeException("some error")),
successfulCommand("some value", commandExecutionTime),
successfulCommand("some value", commandExecutionTime));
new BadCommand(new RuntimeException(error)),
new GoodCommand(value, commandExecutionTime),
new GoodCommand(value, commandExecutionTime));

long executionStartMillis = System.currentTimeMillis();
Exception e = assertThrows(GitException.class, () -> {
new GitCommandsExecutor(threads, listener).invokeAll(commands);
});
assertThat(e.getMessage(), is("java.lang.RuntimeException: some error"));
assertThat(e.getMessage(), is("java.lang.RuntimeException: " + error));
assertThat(e.getCause(), instanceOf(RuntimeException.class));
long executionStopMillis = System.currentTimeMillis();

Expand All @@ -106,15 +121,13 @@ public void firstCommandFails() throws Exception {

@Test
public void lastCommandFails() throws Exception {
List<Callable<String>> commands = asList(
successfulCommand("some value"),
successfulCommand("some value"),
erroneousCommand(new RuntimeException("some error")));
List<Callable<String>> commands =
asList(new GoodCommand(value), new GoodCommand(value), new BadCommand(new RuntimeException(error)));

Exception e = assertThrows(GitException.class, () -> {
new GitCommandsExecutor(threads, listener).invokeAll(commands);
});
assertThat(e.getMessage(), is("java.lang.RuntimeException: some error"));
assertThat(e.getMessage(), is("java.lang.RuntimeException: " + error));
assertThat(e.getCause(), instanceOf(RuntimeException.class));

for (Callable<String> command : commands) {
Expand All @@ -126,7 +139,7 @@ public void lastCommandFails() throws Exception {
public void moreCommandsThanThreads() throws Exception {
List<Callable<String>> commands = new ArrayList<>();
for (int i = 0; i < threads + 1; i++) {
commands.add(successfulCommand("some value"));
commands.add(new GoodCommand(value));
}

new GitCommandsExecutor(threads, listener).invokeAll(commands);
Expand All @@ -140,7 +153,7 @@ public void moreCommandsThanThreads() throws Exception {
public void lessCommandsThanThreads() throws Exception {
List<Callable<String>> commands = new ArrayList<>();
for (int i = 0; i < threads - 1; i++) {
commands.add(successfulCommand("some value"));
commands.add(new GoodCommand(value));
}

new GitCommandsExecutor(threads, listener).invokeAll(commands);
Expand All @@ -155,7 +168,7 @@ public void callerThreadWasInterrupted() throws Exception {
long commandExecutionTime = 60_000;
List<Callable<String>> commands = new ArrayList<>();
for (int i = 0; i < threads + 1; i++) {
commands.add(successfulCommand("some value", commandExecutionTime));
commands.add(new GoodCommand(value, commandExecutionTime));
}

AtomicBoolean isCallerInterrupted = new AtomicBoolean(false);
Expand Down Expand Up @@ -190,7 +203,7 @@ public void callerThreadWasInterrupted() throws Exception {
@Test
public void commandWasInterrupted() throws Exception {
Exception commandException = new InterruptedException("some interrupt");
List<Callable<String>> commands = Collections.singletonList(erroneousCommand(commandException));
List<Callable<String>> commands = Collections.singletonList(new BadCommand(commandException));
Exception e = assertThrows(
InterruptedException.class, () -> new GitCommandsExecutor(threads, listener).invokeAll(commands));
assertThat(e.getMessage(), is(nullValue()));
Expand All @@ -203,62 +216,55 @@ public void commandWasInterrupted() throws Exception {

@Test
public void commandHadGitProblem() throws Exception {
Exception commandException = new GitException("some error");
List<Callable<String>> commands = Collections.singletonList(erroneousCommand(commandException));
Exception commandException = new GitException(error);
List<Callable<String>> commands = Collections.singletonList(new BadCommand(commandException));

Exception e = assertThrows(GitException.class, () -> {
new GitCommandsExecutor(threads, listener).invokeAll(commands);
});
assertThat(e.getMessage(), is("hudson.plugins.git.GitException: some error"));
assertThat(e.getMessage(), is("hudson.plugins.git.GitException: " + error));
assertThat(e.getCause(), is(theInstance(commandException)));
}

@Test
public void commandHadUnknownProblem() throws Exception {
Exception commandException = new RuntimeException("some error");
List<Callable<String>> commands = Collections.singletonList(erroneousCommand(commandException));
Exception commandException = new RuntimeException(error);
List<Callable<String>> commands = Collections.singletonList(new BadCommand(commandException));

Exception e = assertThrows(GitException.class, () -> {
new GitCommandsExecutor(threads, listener).invokeAll(commands);
});
assertThat(e.getMessage(), is("java.lang.RuntimeException: some error"));
assertThat(e.getMessage(), is("java.lang.RuntimeException: " + error));
assertThat(e.getCause(), is(theInstance(commandException)));
}

private Callable<String> successfulCommand(String value) throws Exception {
return new SuccessfulCommand(value);
}

private Callable<String> successfulCommand(String value, long commandExecutionTime) throws Exception {
return new SuccessfulCommand(value, commandExecutionTime);
}

private void loggedNoOutput() {
String loggedOutput = logStream.toString();
assertThat(loggedOutput, is(emptyString()));
}

private void wasCalled(Callable<String> command) {
if (command instanceof SuccessfulCommand succeeds) {
assertTrue(succeeds.wasCalled());
} else if (command instanceof ErroneousCommand erroneous) {
assertTrue(erroneous.wasCalled());
if (command instanceof GoodCommand good) {
assertTrue(good.wasCalled());
} else if (command instanceof BadCommand bad) {
assertTrue(bad.wasCalled());
} else {
fail("Unexpected command type " + command);
}
}

private class SuccessfulCommand implements Callable {
/* A callable that always returns a value, sometimes after a delay */
private class GoodCommand implements Callable {

private final String value;
private final long commandExecutionTime;
private boolean called = false;

SuccessfulCommand(String value) {
GoodCommand(String value) {
this(value, -1);
}

SuccessfulCommand(String value, long commandExecutionTime) {
GoodCommand(String value, long commandExecutionTime) {
this.value = value;
this.commandExecutionTime = commandExecutionTime;
}
Expand All @@ -277,16 +283,13 @@ public Object call() throws Exception {
}
}

private Callable<String> erroneousCommand(Exception exception) throws Exception {
return new ErroneousCommand(exception);
}

private class ErroneousCommand implements Callable {
/* A callable that always throws an exception */
private class BadCommand implements Callable {

private final Exception exception;
private boolean called = false;

ErroneousCommand(Exception exceptionToThrow) {
BadCommand(Exception exceptionToThrow) {
exception = exceptionToThrow;
}

Expand Down
Loading