Skip to content

Commit

Permalink
Merge pull request #1472 from jbaranov/JENKINS-25514
Browse files Browse the repository at this point in the history
[JENKINS-25514] Fixed lock case in FutureImpl
  • Loading branch information
olivergondza committed Jan 6, 2015
2 parents 4470b9e + 3edae66 commit 5bfaca4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
8 changes: 8 additions & 0 deletions core/src/main/java/hudson/model/queue/FutureImpl.java
Expand Up @@ -84,6 +84,14 @@ public boolean cancel(boolean mayInterruptIfRunning) {
}
}

@Override
public synchronized void setAsCancelled() {
super.setAsCancelled();
if (!start.isDone()) {
start.setAsCancelled();
}
}

synchronized void addExecutor(@Nonnull Executor executor) {
this.executors.add(executor);
}
Expand Down
30 changes: 30 additions & 0 deletions test/src/test/java/hudson/model/QueueTest.java
Expand Up @@ -62,6 +62,7 @@
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicInteger;
Expand Down Expand Up @@ -567,4 +568,33 @@ public String getDisplayName() {
} catch (CancellationException e) {
}
}

public void testWaitForStartAndCancelBeforeStart() throws Exception {
final OneShotEvent ev = new OneShotEvent();
FreeStyleProject p = createFreeStyleProject();

QueueTaskFuture<FreeStyleBuild> f = p.scheduleBuild2(10);
final Queue.Item item = Queue.getInstance().getItem(p);
assertNotNull(item);

final ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);
executor.schedule(new Runnable() {
@Override
public void run() {
try {
Queue.getInstance().doCancelItem(item.id);
} catch (IOException e) {
e.printStackTrace();
} catch (ServletException e) {
e.printStackTrace();
}
}
}, 2, TimeUnit.SECONDS);

try {
f.waitForStart();
fail("Expected an CancellationException to be thrown");
} catch (CancellationException e) {}
}

}

0 comments on commit 5bfaca4

Please sign in to comment.