Skip to content

Commit

Permalink
improve testParallelLimitConcurrency to do a basic test of the limit
Browse files Browse the repository at this point in the history
The test testParallelLimitConcurrency now does a very basic test of the
concurrency limit by using a job which pauses for ten seconds and thus the
expected runtime of the flow can be tested based on the configured limit.
  • Loading branch information
jcarrothers-sap committed Jun 30, 2015
1 parent 83a5b7c commit 44a9940
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 3 deletions.
20 changes: 18 additions & 2 deletions src/test/groovy/com/cloudbees/plugins/flow/DSLTestCase.groovy
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/*
* The MIT License
*
* Copyright (c) 2013, CloudBees, Inc., Nicolas De Loof.
* Cisco Systems, Inc., a California corporation
* Copyright (c) 2013-2015, CloudBees, Inc., Nicolas De Loof.
* Cisco Systems, Inc., a California corporation
* SAP SE
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -49,6 +50,7 @@ import hudson.model.BuildListener
import hudson.tasks.Builder
import com.cloudbees.plugin.flow.ConfigurableFailureBuilder
import com.cloudbees.plugin.flow.BlockingBuilder
import com.cloudbees.plugin.flow.SleepingBuilder
import hudson.model.Job

import static hudson.model.Result.UNSTABLE
Expand All @@ -67,6 +69,14 @@ abstract class DSLTestCase extends HudsonTestCase {
return jobs
}

def createSleepingJobs = { names ->
def jobs = []
names.each {
jobs.add(createSleepingJob(it))
}
return jobs
}

def createFailJob = {String name, int failures = Integer.MAX_VALUE ->
def job = createJob(name)
job.getBuildersList().add(new ConfigurableFailureBuilder(failures));
Expand All @@ -85,6 +95,12 @@ abstract class DSLTestCase extends HudsonTestCase {
return job
}

def createSleepingJob = {String name, int seconds = 10 ->
def job = createJob(name)
job.getBuildersList().add(new SleepingBuilder(seconds));
return job
}

def run = { script ->
BuildFlow flow = new BuildFlow(Jenkins.instance, getName())
flow.dsl = script
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class ParallelTest extends DSLTestCase {
}

public void testParallelLimitConcurrency() {
def jobs = createJobs(["job1", "job2", "job3", "job4"])
def jobs = createSleepingJobs(["job1", "job2", "job3", "job4"])
def flow = run("""
parallel( 2,
{ build("job1") },
Expand All @@ -57,6 +57,8 @@ class ParallelTest extends DSLTestCase {
)
""")
assertAllSuccess(jobs)
assert flow.duration/1000 >= (5+10)*2
assert flow.duration/1000 < (5+10)*3
assert SUCCESS == flow.result
println flow.jobsGraph.edgeSet()
}
Expand Down
73 changes: 73 additions & 0 deletions src/test/java/com/cloudbees/plugin/flow/SleepingBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* The MIT License
*
* Copyright (c) 2013-2015, Cisco Systems, Inc., a California corporation
* SAP SE
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.cloudbees.plugin.flow;

import hudson.Extension;
import hudson.Launcher;
import hudson.model.AbstractBuild;
import hudson.model.BuildListener;
import hudson.model.Descriptor;
import hudson.tasks.Builder;
import net.sf.json.JSONObject;
import org.kohsuke.stapler.StaplerRequest;

import static hudson.model.Result.*;

/**
* A Builder that will sleep for the specified number of seconds.
*
* @author: James Nord
*/
public class SleepingBuilder extends Builder {

public final int sleepSeconds;

SleepingBuilder(int sleepSeconds) {
this.sleepSeconds = sleepSeconds;
}

public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) {
System.out.println("Sleeping Builder in build " + build.getFullDisplayName() + "starting");
try {
System.out.println("Sleeping Builder in build " + build.getFullDisplayName() + "waiting");
Thread.sleep(sleepSeconds * 1000);
} catch (InterruptedException ex) {
build.setResult(ABORTED);
}
System.out.println("Sleeping Builder in build " + build.getFullDisplayName() + " completing " + build.getResult());
return true;
}

@Extension
public static final class DescriptorImpl extends Descriptor<Builder> {
public String getDisplayName() {
return "Sleeping builder";
}

public SleepingBuilder newInstance(StaplerRequest req, JSONObject data) {
return new SleepingBuilder(10);
}
}
}

0 comments on commit 44a9940

Please sign in to comment.