Skip to content

Commit

Permalink
Merge pull request #41 from bboehmke/dynamicAxis-fix
Browse files Browse the repository at this point in the history
[JENKINS-34389] Fixed handling of dynamic axis
  • Loading branch information
olivergondza committed Mar 30, 2017
2 parents ca0aa46 + 645ed5a commit 4cc2761
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/main/java/hudson/matrix/MatrixBuild.java
Expand Up @@ -350,15 +350,15 @@ protected Result doRun(BuildListener listener) throws Exception {
MatrixProject p = getProject();
PrintStream logger = listener.getLogger();

// give axes a chance to rebuild themselves
activeConfigurations = p.rebuildConfigurations(this);

// list up aggregators
listUpAggregators(p.getPublishers().values());
listUpAggregators(p.getProperties().values());
listUpAggregators(p.getBuildWrappers().values());

axes = p.getAxes();
// rebuild project configuration
MatrixProject.RunConfiguration config = p.getRunConfiguration(this);
activeConfigurations = config.config;
axes = config.axisList;

try {
return p.getExecutionStrategy().run(this);
Expand Down
40 changes: 40 additions & 0 deletions src/main/java/hudson/matrix/MatrixProject.java
Expand Up @@ -82,6 +82,8 @@
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
Expand Down Expand Up @@ -199,6 +201,11 @@ public class MatrixProject extends AbstractProject<MatrixProject,MatrixBuild> im
*/
private String childCustomWorkspace;

/**
* Lock to prevent project changes on different build at the same time
*/
private transient Lock buildLock = new ReentrantLock();

public MatrixProject(String name) {
this(Jenkins.getInstance(), name);
}
Expand All @@ -207,6 +214,11 @@ public MatrixProject(ItemGroup parent, String name) {
super(parent, name);
}

protected Object readResolve() {
buildLock = new ReentrantLock();
return this;
}

@Override
public String getPronoun() {
return AlternativeUiTextProvider.get(PRONOUN, this, Messages.MatrixProject_Pronoun());
Expand Down Expand Up @@ -660,6 +672,34 @@ public Combination apply(@Nullable List<String> strings) {
return active;
}

/**
* Configuration for matrix build
*/
/*package*/ static class RunConfiguration {
public Set<MatrixConfiguration> config;
public AxisList axisList;
}

/**
* Rebuild project settings and return actual configuration and axis list
*/
/*package*/ RunConfiguration getRunConfiguration(MatrixBuildExecution context) throws IOException {
RunConfiguration runConfig = new RunConfiguration();
try {
buildLock.lock();

// give axes a chance to rebuild themselves
runConfig.config = rebuildConfigurations(context);

// deep copy the axes
runConfig.axisList = (AxisList) Jenkins.XSTREAM.fromXML(Jenkins.XSTREAM.toXML(axes));

} finally {
buildLock.unlock();
}
return runConfig;
}

private boolean isDynamicFilter(final String filter) {

if (!isParameterized() || filter == null) return false;
Expand Down
34 changes: 34 additions & 0 deletions src/test/java/hudson/matrix/MatrixTest.java
Expand Up @@ -28,11 +28,16 @@
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertArrayEquals;

import hudson.matrix.helper.DynamicTestAxis;

import hudson.model.Item;
import hudson.model.queue.QueueTaskFuture;
import hudson.security.AuthorizationMatrixProperty;
import hudson.security.ProjectMatrixAuthorizationStrategy;

import java.util.Arrays;
import java.util.Collections;

import org.acegisecurity.context.SecurityContextHolder;
Expand All @@ -44,6 +49,7 @@
import java.io.IOException;
import java.util.Set;

import org.apache.commons.lang.reflect.FieldUtils;
import org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException;
import org.jenkinsci.plugins.scriptsecurity.scripts.ScriptApproval;
import org.junit.Rule;
Expand Down Expand Up @@ -135,4 +141,32 @@ private static void expectRejection(MatrixProject project, String combinationFil
scriptApproval.approveSignature(signature);
assertEquals(Collections.emptySet(), scriptApproval.getPendingSignatures());
}


@Issue("JENKINS-34389")
@Test public void axisValuesChanged() throws Exception {
// create project with dynamic axis
MatrixProject project = j.createMatrixProject();
project.setAxes(new AxisList(
new DynamicTestAxis("axis")
));
project.setConcurrentBuild(true);

// build project
QueueTaskFuture<MatrixBuild> matrixBuildQueue = project.scheduleBuild2(0);
matrixBuildQueue.waitForStart();

QueueTaskFuture<MatrixBuild> matrixBuildQueue2 = project.scheduleBuild2(0);

MatrixBuild build = matrixBuildQueue.get();
MatrixBuild build2 = matrixBuildQueue2.get();

// get axes from build
AxisList axes = (AxisList) FieldUtils.readField(build, "axes", true);
AxisList axes2 = (AxisList) FieldUtils.readField(build2, "axes", true);

// check if axes are valid
assertArrayEquals(axes.get(0).getValues().toArray(), Arrays.asList("1", "10").toArray());
assertArrayEquals(axes2.get(0).getValues().toArray(), Arrays.asList("2", "20").toArray());
}
}
32 changes: 32 additions & 0 deletions src/test/java/hudson/matrix/helper/DynamicTestAxis.java
@@ -0,0 +1,32 @@
package hudson.matrix.helper;

import com.google.common.collect.Lists;
import hudson.matrix.Axis;
import hudson.matrix.MatrixBuild;

import java.util.Arrays;
import java.util.List;


public class DynamicTestAxis extends Axis {
private final List<String> axisValues = Lists.newArrayList();

public DynamicTestAxis(String name) {
super(name, "");
}

@Override
public synchronized List<String> getValues() {
return axisValues;
}

@Override
public synchronized List<String> rebuild(MatrixBuild.MatrixBuildExecution context) {
// each axis has 2 values: 1. = build number & 2. = build number * 10
axisValues.clear();
axisValues.addAll(Arrays.asList(
Integer.toString(context.getBuild().getNumber()),
Integer.toString(context.getBuild().getNumber()*10)));
return axisValues;
}
}

0 comments on commit 4cc2761

Please sign in to comment.