Skip to content

Commit

Permalink
[JENKINS-42150] Fix locks when applying latency
Browse files Browse the repository at this point in the history
- Also fix locks when applying faults
- Also fix includes check in mock impl
  • Loading branch information
stephenc committed Feb 20, 2017
1 parent 48fc047 commit ec8b944
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
15 changes: 13 additions & 2 deletions src/test/java/jenkins/scm/impl/mock/MockSCMController.java
Expand Up @@ -184,10 +184,21 @@ public synchronized void clearFaults() {
this.faults.clear();
}

public synchronized void checkFaults(@CheckForNull String repository, @CheckForNull String branch,
public void applyLatency() throws InterruptedException {
MockLatency latency;
synchronized (this) {
latency = this.latency;
}
latency.apply();
}

public void checkFaults(@CheckForNull String repository, @CheckForNull String branch,
@CheckForNull String revision, boolean actions)
throws IOException, InterruptedException {
latency.apply();
List<MockFailure> faults;
synchronized (this) {
faults = new ArrayList<MockFailure>(this.faults);
}
for (MockFailure fault: faults) {
fault.check(repository, branch, revision, actions);
}
Expand Down
9 changes: 9 additions & 0 deletions src/test/java/jenkins/scm/impl/mock/MockSCMNavigator.java
Expand Up @@ -34,7 +34,9 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import javax.annotation.Nonnull;
import jenkins.scm.api.SCMHead;
import jenkins.scm.api.SCMNavigator;
import jenkins.scm.api.SCMNavigatorDescriptor;
import jenkins.scm.api.SCMNavigatorEvent;
Expand Down Expand Up @@ -99,12 +101,18 @@ protected String id() {

@Override
public void visitSources(@NonNull SCMSourceObserver observer) throws IOException, InterruptedException {
controller().applyLatency();
controller().checkFaults(null, null, null, false);
Set<String> includes = observer.getIncludes();
for (String name : controller().listRepositories()) {
if (!observer.isObserving()) {
return;
}
checkInterrupt();
if (includes != null && !includes.contains(name)) {
continue;
}
controller().applyLatency();
controller().checkFaults(name, null, null, false);
SCMSourceObserver.ProjectObserver po = observer.observe(name);
po.addSource(new MockSCMSource(getId() + "::" + name,
Expand All @@ -119,6 +127,7 @@ public List<Action> retrieveActions(@NonNull SCMNavigatorOwner owner,
@CheckForNull SCMNavigatorEvent event,
@NonNull TaskListener listener)
throws IOException, InterruptedException {
controller().applyLatency();
controller().checkFaults(null, null, null, true);
List<Action> result = new ArrayList<Action>();
result.add(new MockSCMLink("organization"));
Expand Down
16 changes: 13 additions & 3 deletions src/test/java/jenkins/scm/impl/mock/MockSCMSource.java
Expand Up @@ -118,18 +118,21 @@ public boolean isIncludeChangeRequests() {
protected void retrieve(@CheckForNull SCMSourceCriteria criteria, @NonNull SCMHeadObserver observer,
@CheckForNull SCMHeadEvent<?> event, @NonNull TaskListener listener)
throws IOException, InterruptedException {
controller().applyLatency();
controller().checkFaults(repository, null, null, false);
Set<SCMHead> includes = observer.getIncludes();
if (includeBranches) {
for (final String branch : controller().listBranches(repository)) {
checkInterrupt();
String revision = controller().getRevision(repository, branch);
MockSCMHead head = new MockSCMHead(branch);
controller().checkFaults(repository, head.getName(), null, false);
if (includes != null && !includes.contains(head)) {
continue;
}
controller().applyLatency();
controller().checkFaults(repository, head.getName(), null, false);
if (criteria == null || criteria.isHead(new MockSCMProbe(head, revision), listener)) {
controller().applyLatency();
controller().checkFaults(repository, head.getName(), revision, false);
observer.observe(head, new MockSCMRevision(head, revision));
}
Expand All @@ -140,11 +143,13 @@ protected void retrieve(@CheckForNull SCMSourceCriteria criteria, @NonNull SCMHe
checkInterrupt();
String revision = controller().getRevision(repository, tag);
MockSCMHead head = new MockTagSCMHead(tag, controller().getTagTimestamp(repository, tag));
controller().checkFaults(repository, head.getName(), null, false);
if (includes != null && !includes.contains(head)) {
continue;
}
controller().applyLatency();
controller().checkFaults(repository, head.getName(), null, false);
if (criteria == null || criteria.isHead(new MockSCMProbe(head, revision), listener)) {
controller().applyLatency();
controller().checkFaults(repository, head.getName(), revision, false);
observer.observe(head, new MockSCMRevision(head, revision));
}
Expand All @@ -156,11 +161,13 @@ protected void retrieve(@CheckForNull SCMSourceCriteria criteria, @NonNull SCMHe
String revision = controller().getRevision(repository, "change-request/" + number);
String target = controller().getTarget(repository, number);
MockChangeRequestSCMHead head = new MockChangeRequestSCMHead(number, target);
controller().checkFaults(repository, head.getName(), null, false);
if (includes != null && !includes.contains(head)) {
continue;
}
controller().applyLatency();
controller().checkFaults(repository, head.getName(), null, false);
if (criteria == null || criteria.isHead(new MockSCMProbe(head, revision), listener)) {
controller().applyLatency();
controller().checkFaults(repository, head.getName(), revision, false);
observer.observe(head, new MockSCMRevision(head, revision));
}
Expand All @@ -179,6 +186,7 @@ public SCM build(@NonNull SCMHead head, @CheckForNull SCMRevision revision) {
@Override
protected List<Action> retrieveActions(@CheckForNull SCMSourceEvent event, @NonNull TaskListener listener)
throws IOException, InterruptedException {
controller().applyLatency();
controller().checkFaults(repository, null, null, true);
List<Action> result = new ArrayList<Action>();
result.add(new MockSCMLink("source"));
Expand All @@ -201,6 +209,7 @@ protected List<Action> retrieveActions(@NonNull SCMRevision revision,
@CheckForNull SCMHeadEvent event,
@NonNull TaskListener listener)
throws IOException, InterruptedException {
controller().applyLatency();
controller().checkFaults(repository, revision.getHead().getName(), ((MockSCMRevision)revision).getHash(), true);
return Collections.<Action>singletonList(new MockSCMLink("revision"));
}
Expand All @@ -211,6 +220,7 @@ protected List<Action> retrieveActions(@NonNull SCMHead head,
@CheckForNull SCMHeadEvent event,
@NonNull TaskListener listener)
throws IOException, InterruptedException {
controller().applyLatency();
controller().checkFaults(repository, head.getName(), null, true);
List<Action> result = new ArrayList<Action>();
if (head instanceof MockChangeRequestSCMHead) {
Expand Down

0 comments on commit ec8b944

Please sign in to comment.