Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[JENKINS-27836] Add test case
  • Loading branch information
stephenc committed Apr 8, 2015
1 parent 9fd4c1a commit 0000211
Showing 1 changed file with 131 additions and 0 deletions.
131 changes: 131 additions & 0 deletions test/src/test/java/hudson/tasks/LogRotatorTest.java
Expand Up @@ -23,27 +23,38 @@
*/
package hudson.tasks;

import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

import hudson.Launcher;
import hudson.model.AbstractBuild;
import hudson.model.BuildListener;
import hudson.model.Descriptor;
import hudson.model.FreeStyleBuild;
import hudson.model.FreeStyleProject;
import hudson.model.Result;
import hudson.model.Run;
import hudson.model.queue.QueueTaskFuture;
import hudson.tasks.ArtifactArchiverTest.CreateArtifact;

import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.FailureBuilder;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.TestBuilder;

/**
* Verifies that the last successful and stable builds of a job will be kept if requested.
Expand Down Expand Up @@ -136,6 +147,59 @@ public void artifactDelete() throws Exception {
assertTrue(project.getBuildByNumber(7).getHasArtifacts());
assertTrue(project.getBuildByNumber(8).getHasArtifacts());
}

@Test
@Issue("JENKINS-27836")
public void artifactsRetainedWhileBuilding() throws Exception {
j.getInstance().setNumExecutors(3);
FreeStyleProject p = j.createFreeStyleProject();
p.setBuildDiscarder(new LogRotator(-1, 3, -1, 1));
StallBuilder sync = new StallBuilder();
p.getBuildersList().replaceBy(Arrays.asList(new CreateArtifact(), sync));
p.setConcurrentBuild(true);
QueueTaskFuture<FreeStyleBuild> futureRun1 = p.scheduleBuild2(0);
FreeStyleBuild run1 = futureRun1.waitForStart();
sync.waitFor(run1.getNumber(), 1, TimeUnit.SECONDS);
QueueTaskFuture<FreeStyleBuild> futureRun2 = p.scheduleBuild2(0);
FreeStyleBuild run2 = futureRun2.waitForStart();
sync.waitFor(run2.getNumber(), 1, TimeUnit.SECONDS);
QueueTaskFuture<FreeStyleBuild> futureRun3 = p.scheduleBuild2(0);
FreeStyleBuild run3 = futureRun3.waitForStart();
sync.waitFor(run3.getNumber(), 1, TimeUnit.SECONDS);
assertThat("we haven't released run1's guard", run1.isBuilding(), is(true));
assertThat("we haven't released run2's guard", run2.isBuilding(), is(true));
assertThat("we haven't released run3's guard", run3.isBuilding(), is(true));
assertThat("we have artifacts in run1", run1.getHasArtifacts(), is(true));
assertThat("we have artifacts in run2", run2.getHasArtifacts(), is(true));
assertThat("we have artifacts in run3", run3.getHasArtifacts(), is(true));
sync.release(run1.getNumber());
futureRun1.get();
assertThat("we have released run1's guard", run1.isBuilding(), is(false));
assertThat("we haven't released run2's guard", run2.isBuilding(), is(true));
assertThat("we haven't released run3's guard", run3.isBuilding(), is(true));
assertThat("run1 is last stable build", p.getLastStableBuild(), is(run1));
assertThat("run1 is last successful build", p.getLastSuccessfulBuild(), is(run1));
assertThat("we have artifacts in run1", run1.getHasArtifacts(), is(true));
assertThat("CRITICAL ASSERTION: we have artifacts in run2", run2.getHasArtifacts(), is(true));
assertThat("we have artifacts in run3", run3.getHasArtifacts(), is(true));
sync.release(run2.getNumber());
futureRun2.get();
assertThat("we have released run2's guard", run2.isBuilding(), is(false));
assertThat("we haven't released run3's guard", run3.isBuilding(), is(true));
assertThat("we have no artifacts in run1", run1.getHasArtifacts(), is(false));
assertThat("run2 is last stable build", p.getLastStableBuild(), is(run2));
assertThat("run2 is last successful build", p.getLastSuccessfulBuild(), is(run2));
assertThat("we have artifacts in run2", run2.getHasArtifacts(), is(true));
assertThat("we have artifacts in run3", run3.getHasArtifacts(), is(true));
sync.release(run3.getNumber());
futureRun3.get();
assertThat("we have released run3's guard", run3.isBuilding(), is(false));
assertThat("we have no artifacts in run1", run1.getHasArtifacts(), is(false));
assertThat("we have no artifacts in run2", run2.getHasArtifacts(), is(false));
assertThat("run3 is last stable build", p.getLastStableBuild(), is(run3));
assertThat("run3 is last successful build", p.getLastSuccessfulBuild(), is(run3));
assertThat("we have artifacts in run3", run3.getHasArtifacts(), is(true));
}

static Result build(FreeStyleProject project) throws Exception {
return project.scheduleBuild2(0).get().getResult();
Expand Down Expand Up @@ -163,4 +227,71 @@ public String getDisplayName() {
};
}
}

static class StallBuilder extends TestBuilder {

private int syncBuildNumber;

private final Object syncLock = new Object();

private int waitBuildNumber;

private final Object waitLock = new Object();

private final ArtifactArchiver archiver = new ArtifactArchiver("f");

public @Override boolean perform(AbstractBuild<?,?> build, Launcher launcher, BuildListener listener)
throws IOException, InterruptedException {
archiver.perform(build, launcher, listener);
Logger.getAnonymousLogger().log(Level.INFO, "Building #{0}", build.getNumber());
synchronized (waitLock) {
if (waitBuildNumber < build.getNumber()) {
waitBuildNumber = build.getNumber();
waitLock.notifyAll();
}
}
Logger.getAnonymousLogger().log(Level.INFO, "Waiting #{0}", build.getNumber());
synchronized (syncLock) {
while (build.getNumber() > syncBuildNumber) {
try {
syncLock.wait(10);
} catch (InterruptedException e) {
e.printStackTrace(listener.fatalError("Interrupted: %s", e.getMessage()));
return false;
}
}
}
Logger.getAnonymousLogger().log(Level.INFO, "Done #{0}", build.getNumber());
return true;
}

public void release(int upToBuildNumber) {
synchronized (syncLock) {
if (syncBuildNumber < upToBuildNumber) {
Logger.getAnonymousLogger().log(Level.INFO, "Signal #{0}", upToBuildNumber);
syncBuildNumber = upToBuildNumber;
syncLock.notifyAll();
}
}
}

public void waitFor(int buildNumber, long timeout, TimeUnit units) throws TimeoutException,
InterruptedException {
long giveUp = System.nanoTime() + units.toNanos(timeout);
synchronized (waitLock) {
while (waitBuildNumber < buildNumber) {
long remaining = giveUp - System.nanoTime();
if (remaining < 0) {
throw new TimeoutException();
}
waitLock.wait(remaining/1000000L, (int)(remaining%1000000L));
}
}
}

public BuildStepMonitor getRequiredMonitorService() {
return BuildStepMonitor.NONE;
}

}
}

0 comments on commit 0000211

Please sign in to comment.