Skip to content
Permalink
Browse files

[JENKINS-42150] Some minor improvements

  • Loading branch information...
stephenc committed Feb 17, 2017
1 parent b89eac3 commit a6d46f93d1deafc6ecee5af6092c7800aa5b33a9
@@ -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;

/**
@@ -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.
*
@@ -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;
}
}
@@ -59,7 +59,8 @@

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;
@@ -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.
@@ -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);
}

0 comments on commit a6d46f9

Please sign in to comment.
You can’t perform that action at this time.