Skip to content
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
<hpi.bundledArtifacts>jboss-marshalling,jboss-marshalling-river</hpi.bundledArtifacts>
<hpi.strictBundledArtifacts>true</hpi.strictBundledArtifacts>
<hpi.compatibleSinceVersion>3.0</hpi.compatibleSinceVersion>
<ban-junit4-imports.skip>false</ban-junit4-imports.skip>
<hpi.strictBundledArtifacts>true</hpi.strictBundledArtifacts>
<hpi.bundledArtifacts>jboss-marshalling,jboss-marshalling-river</hpi.bundledArtifacts>
<gitHubRepo>jenkinsci/${project.artifactId}-plugin</gitHubRepo>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,30 @@
import org.jenkinsci.plugins.workflow.steps.StepContext;
import org.jenkinsci.plugins.workflow.steps.StepDescriptor;
import org.jenkinsci.plugins.workflow.steps.StepExecution;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.Rule;
import org.jvnet.hudson.test.BuildWatcher;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.TestExtension;
import org.jvnet.hudson.test.junit.jupiter.BuildWatcherExtension;
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
import org.kohsuke.stapler.DataBoundConstructor;

public class LogActionImplTest {
@WithJenkins
class LogActionImplTest {

@ClassRule public static BuildWatcher buildWatcher = new BuildWatcher();
@Rule public JenkinsRule r = new JenkinsRule();
@SuppressWarnings("unused")
@RegisterExtension
private static final BuildWatcherExtension BUILD_WATCHER = new BuildWatcherExtension();
private JenkinsRule r;

@Test public void logsAndBlocks() throws Exception {
@BeforeEach
void beforeEach(JenkinsRule rule) {
r = rule;
}

@Test
void logsAndBlocks() throws Exception {
WorkflowJob p = r.createProject(WorkflowJob.class);
p.setDefinition(new CpsFlowDefinition("parallel a: {chatty('LBBL') {echo(/atom step in A with $remaining commands to go/)}}, b: {chatty('BL') {echo(/atom step in B with $remaining commands to go/)}}", true));
WorkflowRun b = r.buildAndAssertSuccess(p);
Expand All @@ -61,27 +71,48 @@ public class LogActionImplTest {
r.assertLogContains("atom step in B with 1 commands to go", b);
r.assertLogContains("logging from BL with 0 commands to go", b);
}

@SuppressWarnings("unused")
public static class ChattyStep extends Step {

public final String pattern;
@DataBoundConstructor public ChattyStep(String pattern) {this.pattern = pattern;}
@Override public StepExecution start(StepContext context) throws Exception {

@DataBoundConstructor
public ChattyStep(String pattern) {this.pattern = pattern;}

@Override
public StepExecution start(StepContext context) throws Exception {
return new Execution(context, pattern);
}
@TestExtension("logsAndBlocks") public static class DescriptorImpl extends StepDescriptor {
@Override public String getFunctionName() {return "chatty";}
@Override public boolean takesImplicitBlockArgument() {return true;}
@Override public Set<? extends Class<?>> getRequiredContext() {

@SuppressWarnings("unused")
@TestExtension("logsAndBlocks")
public static class DescriptorImpl extends StepDescriptor {

@Override
public String getFunctionName() {return "chatty";}

@Override
public boolean takesImplicitBlockArgument() {return true;}

@Override
public Set<? extends Class<?>> getRequiredContext() {
return Collections.singleton(TaskListener.class);
}
}

private static class Execution extends StepExecution {

private final String pattern;
private LinkedList<Boolean> commands; // L ~ false to log, B ~ true to run block

Execution(StepContext context, String pattern) {
super(context);
this.pattern = pattern;
}
final String pattern;
LinkedList<Boolean> commands; // L ~ false to log, B ~ true to run block
@Override public boolean start() throws Exception {

@Override
public boolean start() throws Exception {
commands = new LinkedList<>();
for (char c : pattern.toCharArray()) {
if (c == 'L') {
Expand All @@ -94,6 +125,7 @@ private static class Execution extends StepExecution {
run();
return false;
}

private void run() throws Exception {
StepContext context = getContext();
if (commands.isEmpty()) {
Expand All @@ -105,19 +137,23 @@ private void run() throws Exception {
run();
}
}

private final class Callback extends BodyExecutionCallback { // not using TailCall since run() sometimes calls onSuccess itself
@Override public void onSuccess(StepContext context, Object result) {

@Override
public void onSuccess(StepContext context, Object result) {
try {
run();
} catch (Exception x) {
context.onFailure(x);
}
}
@Override public void onFailure(StepContext context, Throwable t) {

@Override
public void onFailure(StepContext context, Throwable t) {
context.onFailure(t);
}
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,19 @@
import org.jenkinsci.plugins.workflow.flow.FlowExecution;
import org.jenkinsci.plugins.workflow.graph.FlowNode;
import org.jenkinsci.plugins.workflow.support.actions.PauseAction;
import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

import org.mockito.Mockito;

/**
* @author <a href="mailto:tom.fennelly@gmail.com">tom.fennelly@gmail.com</a>
*/
public class PauseActionTest {
class PauseActionTest {

@Test
public void test() throws Exception {
void test() throws Exception {
FlowExecution flowExecution = Mockito.mock(FlowExecution.class);
FlowNode flowNode = new FlowNode(flowExecution, "1") {
@Override
Expand All @@ -45,35 +47,35 @@ protected String getTypeDisplayName() {
}
};

Assert.assertFalse(PauseAction.isPaused(flowNode));
Assert.assertEquals(0L, PauseAction.getPauseDuration(flowNode));
assertFalse(PauseAction.isPaused(flowNode));
assertEquals(0L, PauseAction.getPauseDuration(flowNode));

flowNode.addAction(new PauseAction("P1"));
PauseAction firstPause = PauseAction.getCurrentPause(flowNode);
Assert.assertEquals("P1", firstPause.getCause());
Assert.assertTrue(PauseAction.isPaused(flowNode));
assertEquals("P1", firstPause.getCause());
assertTrue(PauseAction.isPaused(flowNode));
Thread.sleep(200);
Assert.assertTrue(PauseAction.getPauseDuration(flowNode) > 100L);
assertTrue(PauseAction.getPauseDuration(flowNode) > 100L);

PauseAction.endCurrentPause(flowNode);
Assert.assertFalse(PauseAction.isPaused(flowNode));
assertFalse(PauseAction.isPaused(flowNode));
long firstPauseDuration = firstPause.getPauseDuration();

Thread.sleep(200);
Assert.assertEquals(firstPauseDuration, PauseAction.getPauseDuration(flowNode));
assertEquals(firstPauseDuration, PauseAction.getPauseDuration(flowNode));

flowNode.addAction(new PauseAction("P2"));
PauseAction secondPause = PauseAction.getCurrentPause(flowNode);
Assert.assertEquals("P2", secondPause.getCause());
Assert.assertTrue(PauseAction.isPaused(flowNode));
assertEquals("P2", secondPause.getCause());
assertTrue(PauseAction.isPaused(flowNode));
Thread.sleep(200);
Assert.assertTrue(PauseAction.getPauseDuration(flowNode) > firstPauseDuration);
assertTrue(PauseAction.getPauseDuration(flowNode) > firstPauseDuration);

PauseAction.endCurrentPause(flowNode);
Assert.assertFalse(PauseAction.isPaused(flowNode));
assertFalse(PauseAction.isPaused(flowNode));
long secondPauseDuration = secondPause.getPauseDuration();

Thread.sleep(200);
Assert.assertEquals((firstPauseDuration + secondPauseDuration), PauseAction.getPauseDuration(flowNode));
assertEquals((firstPauseDuration + secondPauseDuration), PauseAction.getPauseDuration(flowNode));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,26 @@
import java.util.logging.Level;
import java.util.stream.IntStream;
import jenkins.util.Timer;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.Rule;
import org.jvnet.hudson.test.LoggerRule;
import static org.junit.jupiter.api.Assertions.fail;

public class TimeoutTest {
import org.junit.jupiter.api.Test;
import org.jvnet.hudson.test.LogRecorder;

@Rule public LoggerRule logging = new LoggerRule().record(Timeout.class, Level.FINER);

@Test public void passed() throws Exception {
class TimeoutTest {

@SuppressWarnings("unused")
private final LogRecorder logging = new LogRecorder().record(Timeout.class, Level.FINER);

@Test
void passed() throws Exception {
try (Timeout timeout = Timeout.limit(5, TimeUnit.SECONDS)) {
Thread.sleep(1_000);
}
Thread.sleep(5_000);
}

@Test public void failed() throws Exception {
@Test
void failed() throws Exception {
try (Timeout timeout = Timeout.limit(5, TimeUnit.SECONDS)) {
Thread.sleep(10_000);
fail("should have timed out");
Expand All @@ -57,7 +60,8 @@ public class TimeoutTest {
Thread.sleep(1_000);
}

@Test public void hung() throws Exception {
@Test
void hung() throws Exception {
/* see disabled code in Timeout:
final AtomicBoolean stop = new AtomicBoolean();
Thread t = Thread.currentThread();
Expand Down Expand Up @@ -88,7 +92,8 @@ public class TimeoutTest {
*/
}

@Test public void starvation() throws Exception {
@Test
void starvation() throws Exception {
Map<Integer, Future<?>> hangers = new TreeMap<>();
IntStream.range(0, 15).forEachOrdered(i -> hangers.put(i, Timer.get().submit(() -> {
try (Timeout timeout = Timeout.limit(5, TimeUnit.SECONDS)) {
Expand All @@ -105,5 +110,4 @@ public class TimeoutTest {
System.err.println("joined #" + hanger.getKey());
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,28 @@
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.jenkinsci.plugins.workflow.test.steps.SemaphoreStep;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.Rule;
import org.jvnet.hudson.test.BuildWatcher;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.LoggerRule;
import org.jvnet.hudson.test.JenkinsSessionRule;
import org.jvnet.hudson.test.LogRecorder;
import org.jvnet.hudson.test.junit.jupiter.BuildWatcherExtension;
import org.jvnet.hudson.test.junit.jupiter.JenkinsSessionExtension;

public class ThrowablePickleTest {
class ThrowablePickleTest {

@ClassRule public static BuildWatcher buildWatcher = new BuildWatcher();
@Rule public JenkinsSessionRule sessions = new JenkinsSessionRule();
@Rule public LoggerRule logging = new LoggerRule().record(ThrowablePickle.class, Level.FINE);
@SuppressWarnings("unused")
@RegisterExtension
private static final BuildWatcherExtension BUILD_WATCHER = new BuildWatcherExtension();

@RegisterExtension
private final JenkinsSessionExtension sessions = new JenkinsSessionExtension();

@SuppressWarnings("unused")
private final LogRecorder logging = new LogRecorder().record(ThrowablePickle.class, Level.FINE);

@Issue("JENKINS-51390")
@Test public void smokes() throws Throwable {
@Test
void smokes() throws Throwable {
String beName = BadException.class.getName();
sessions.then(r -> {
WorkflowJob p = r.createProject(WorkflowJob.class, "p");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,29 @@
import org.jenkinsci.plugins.workflow.pickles.Pickle;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.SettableFuture;
import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.NoSuchElementException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

import static java.util.Arrays.*;
import static org.junit.jupiter.api.Assertions.*;

/**
* @author Kohsuke Kawaguchi
*/
public class EphemeralPickleResolverTest extends Assert {
class EphemeralPickleResolverTest {

@Test
public void resolveNothing() throws Exception {
void resolveNothing() throws Exception {
ListenableFuture<?> f = new PickleResolver(new ArrayList<>()).rehydrate();
assertSuccessfulCompletion(f);
}

@Test
public void resolveSomething() throws Exception {
void resolveSomething() throws Exception {
TestPickle v1 = new TestPickle();
TestPickle v2 = new TestPickle();
ListenableFuture<?> f = new PickleResolver(asList(v1, v2)).rehydrate();
Expand All @@ -64,7 +65,7 @@ public void resolveSomething() throws Exception {
* If a resolution of a value fails, the whole thing should fail.
*/
@Test
public void resolutionFails() throws Exception {
void resolutionFails() throws Exception {
TestPickle v1 = new TestPickle();
TestPickle v2 = new TestPickle();
ListenableFuture<?> f = new PickleResolver(asList(v1, v2)).rehydrate();
Expand All @@ -79,7 +80,7 @@ public void resolutionFails() throws Exception {
f.get();
fail("Expected a failure");
} catch (ExecutionException e) {
assertTrue(e.getCause() instanceof NoSuchElementException);
assertInstanceOf(NoSuchElementException.class, e.getCause());
}
}

Expand All @@ -93,8 +94,9 @@ private void assertSuccessfulCompletion(Future<?> f) throws Exception {
f.get();
}

class TestPickle extends Pickle {
SettableFuture<?> f = SettableFuture.create();
static class TestPickle extends Pickle {
private SettableFuture<?> f = SettableFuture.create();

@Override
public ListenableFuture<?> rehydrate() {
return f;
Expand Down
Loading
Loading