Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[JENKINS-42150] Some minor improvements
  • Loading branch information
stephenc committed Feb 17, 2017
1 parent b89eac3 commit a6d46f9
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 15 deletions.
47 changes: 41 additions & 6 deletions src/test/java/jenkins/scm/impl/mock/MockLatency.java
Expand Up @@ -34,6 +34,19 @@
* @since 2.0.5
*/
public abstract class MockLatency {

private static final MockLatency NONE = new MockLatency() {
@Override
public void apply() throws InterruptedException {
}
};
private static final MockLatency YIELD = new MockLatency() {
@Override
public void apply() throws InterruptedException {
Thread.yield();
}
};

public abstract void apply() throws InterruptedException;

/**
Expand All @@ -51,6 +64,25 @@ public void apply() throws InterruptedException {
};
}

/**
* A fixed latency for all threads except the current thread.
*
* @param time the latency.
* @param units the units.
* @return a fixed latency.
*/
public static MockLatency fixedForOtherThreads(final long time, final TimeUnit units) {
final Thread safe = Thread.currentThread();
return new MockLatency() {
@Override
public void apply() throws InterruptedException {
if (Thread.currentThread() != safe) {
units.sleep(time);
}
}
};
}

/**
* A random latency that has an expected average time.
*
Expand All @@ -75,11 +107,14 @@ public void apply() throws InterruptedException {
* @return a minimal latency that causes background threads to run.
*/
public static MockLatency yield() {
return new MockLatency() {
@Override
public void apply() throws InterruptedException {
Thread.yield();
}
};
return YIELD;
}
/**
* A latency that just forces the thread scheduler to yield.
*
* @return a minimal latency that causes background threads to run.
*/
public static MockLatency none() {
return NONE;
}
}
12 changes: 3 additions & 9 deletions src/test/java/jenkins/scm/impl/mock/MockSCMController.java
Expand Up @@ -59,7 +59,8 @@ public class MockSCMController implements Closeable {

private Map<String, Repository> repositories = new TreeMap<String, Repository>();
private List<MockFailure> faults = new ArrayList<MockFailure>();
private MockLatency latency = null;
@NonNull
private MockLatency latency = MockLatency.none();
private String displayName;
private String description;
private String url;
Expand Down Expand Up @@ -87,11 +88,6 @@ public MockSCMController withLatency(@NonNull MockLatency latency) {
return this;
}

public MockSCMController withoutLatency() {
this.latency = null;
return this;
}

/**
* (Re)creates a {@link MockSCMController} for use when you are running a data migration test.
* It will be the callers responsibility to restore the state of the {@link MockSCMController} accordingly.
Expand Down Expand Up @@ -191,9 +187,7 @@ public synchronized void clearFaults() {
public synchronized void checkFaults(@CheckForNull String repository, @CheckForNull String branch,
@CheckForNull String revision, boolean actions)
throws IOException, InterruptedException {
if (latency != null) {
latency.apply();
}
latency.apply();
for (MockFailure fault: faults) {
fault.check(repository, branch, revision, actions);
}
Expand Down

0 comments on commit a6d46f9

Please sign in to comment.