Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More test cleanup #69

Merged
merged 12 commits into from
Dec 18, 2019
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void shouldGetEmptyNodeLabeledPairsListUponInitialNull()
public void shouldGetNonEmptyNodeLabeledPairsListThatWasSet()
{
String expectedLabel = "aLabel";
Integer expectedMax = new Integer(1);
Integer expectedMax = 1;

ThrottleJobProperty.ThrottleCategory category =
new ThrottleJobProperty.ThrottleCategory(testCategoryName, 0, 0, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import static org.assertj.core.api.Assertions.assertThat;

import com.google.common.base.Optional;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.tngtech.jgiven.Stage;
Expand All @@ -27,6 +26,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.TreeMap;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
Expand Down Expand Up @@ -96,7 +96,7 @@ public static class GivenStage extends Stage<GivenStage> {
public JenkinsRule j;

@ScenarioState
private List<RunProject> projects = new ArrayList<RunProject>();
private List<RunProject> projects = new ArrayList<>();

private int numNodes = 2;
private int numExecutorsPerNode = 10;
Expand All @@ -107,12 +107,12 @@ public GivenStage a_category() {
return self();
}

public GivenStage maxConcurrentPerNode(int maxConcurrentPerNode) throws IOException {
public GivenStage maxConcurrentPerNode(int maxConcurrentPerNode) {
currentCategory.maxConcurrentPerNode(maxConcurrentPerNode);
return self();
}

public GivenStage maxConcurrentTotal(int maxConcurrentTotal) throws IOException {
public GivenStage maxConcurrentTotal(int maxConcurrentTotal) {
currentCategory.maxConcurrentTotal = maxConcurrentTotal;
return self();
}
Expand All @@ -125,7 +125,7 @@ public GivenStage maxConcurrentTotal(int maxConcurrentTotal) throws IOException
return self();
}

public GivenStage $_nodes(int i) throws Exception {
public GivenStage $_nodes(int i) {
numNodes = i;
return self();
}
Expand All @@ -145,7 +145,7 @@ public CategorySpec(JenkinsRule j) {
this.j = j;
}

public CategorySpec maxConcurrentPerNode(int maxConcurrentPerNode) throws IOException {
public CategorySpec maxConcurrentPerNode(int maxConcurrentPerNode) {
this.maxConcurrentPerNode = maxConcurrentPerNode;
return this;
}
Expand Down Expand Up @@ -179,7 +179,7 @@ public void onOnline(Computer C, TaskListener t) {
Jenkins jenkins = j.getInstance();
synchronized (jenkins) {
DumbSlave slave = new DumbSlave("slave" + jenkins.getNodes().size(), "dummy",
j.createTmpDir().getPath(), Integer.toString(numExecutorsPerNode), Node.Mode.NORMAL, "", j.createComputerLauncher(null), RetentionStrategy.NOOP, Collections.EMPTY_LIST);
j.createTmpDir().getPath(), Integer.toString(numExecutorsPerNode), Node.Mode.NORMAL, "", j.createComputerLauncher(null), RetentionStrategy.NOOP, Collections.emptyList());
jenkins.addNode(slave);
}
latch.await();
Expand All @@ -204,7 +204,7 @@ private void init() {
}

public WhenAction each_project_is_built_$_times(int i) throws InterruptedException {
List<RunProject> projectsToBeBuilt = new ArrayList<RunProject>();
List<RunProject> projectsToBeBuilt = new ArrayList<>();
for (RunProject project : projects) {
projectsToBeBuilt.addAll(Collections.nCopies(i, project));
}
Expand All @@ -222,23 +222,23 @@ private void teardown() {

@AfterStage
private void calculateConcurrentBuilds() throws ExecutionException, InterruptedException {
buildingChanges = new TreeMap<Long, Integer>();
buildsPerNode = new TreeMap<Long, Map<String, Integer>>();
buildingChanges = new TreeMap<>();
buildsPerNode = new TreeMap<>();
for (Future<AbstractBuild<?, ?>> buildFuture : builds) {
AbstractBuild<?, ?> build = buildFuture.get();
long startTimeInMillis = build.getStartTimeInMillis();
buildingChanges.put(startTimeInMillis, Optional.fromNullable(buildingChanges.get(startTimeInMillis)).or(0) + 1);
buildingChanges.put(startTimeInMillis, Optional.ofNullable(buildingChanges.get(startTimeInMillis)).orElse(0) + 1);
long endTimeInMillis = startTimeInMillis + build.getDuration();
buildingChanges.put(endTimeInMillis, Optional.fromNullable(buildingChanges.get(endTimeInMillis)).or(0) - 1);
buildingChanges.put(endTimeInMillis, Optional.ofNullable(buildingChanges.get(endTimeInMillis)).orElse(0) - 1);

String nodeName = build.getBuiltOnStr();
Map<String, Integer> nodeChanges = Optional.fromNullable(buildsPerNode.get(startTimeInMillis)).or(new HashMap<String, Integer>());
nodeChanges.put(nodeName, Optional.fromNullable(nodeChanges.get(nodeName)).or(0) + 1);
Map<String, Integer> nodeChanges = Optional.ofNullable(buildsPerNode.get(startTimeInMillis)).orElse(new HashMap<>());
nodeChanges.put(nodeName, Optional.ofNullable(nodeChanges.get(nodeName)).orElse(0) + 1);
buildsPerNode.put(startTimeInMillis,
nodeChanges);

nodeChanges = Optional.fromNullable(buildsPerNode.get(endTimeInMillis)).or(new HashMap<String, Integer>());
nodeChanges.put(nodeName, Optional.fromNullable(nodeChanges.get(nodeName)).or(0) - 1);
nodeChanges = Optional.ofNullable(buildsPerNode.get(endTimeInMillis)).orElse(new HashMap<>());
nodeChanges.put(nodeName, Optional.ofNullable(nodeChanges.get(nodeName)).orElse(0) - 1);
buildsPerNode.put(endTimeInMillis,
nodeChanges);

Expand Down Expand Up @@ -269,15 +269,15 @@ public static class ThenSomeOutcome extends Stage<ThenSomeOutcome> {
}

public ThenSomeOutcome at_most_$_concurrent_builds_per_node(int maxConcurrentPerNode) {
Map<String, Integer> numberOfConcurrentBuilds = new HashMap<String, Integer>();
Map<String, Integer> maxConcurrentBuilds = new HashMap<String, Integer>();
Map<String, Integer> numberOfConcurrentBuilds = new HashMap<>();
Map<String, Integer> maxConcurrentBuilds = new HashMap<>();
for (Map.Entry<Long, Map<String, Integer>> changePerNodePerTime : buildsPerNode.entrySet()) {
for (Map.Entry<String, Integer> changesPerNode : changePerNodePerTime.getValue().entrySet()) {
String nodeName = changesPerNode.getKey();
int newValue = Optional.fromNullable(numberOfConcurrentBuilds.get(nodeName)).or(0) +
int newValue = Optional.ofNullable(numberOfConcurrentBuilds.get(nodeName)).orElse(0) +
changesPerNode.getValue();
numberOfConcurrentBuilds.put(nodeName, newValue);
if (newValue > Optional.fromNullable(maxConcurrentBuilds.get(nodeName)).or(0)) {
if (newValue > Optional.ofNullable(maxConcurrentBuilds.get(nodeName)).orElse(0)) {
maxConcurrentBuilds.put(nodeName, newValue);
}
}
Expand Down Expand Up @@ -322,7 +322,7 @@ private static class SemaphoreBuilder extends Builder {
}

@Override
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException {
inBuild.release();
Thread.sleep(100);
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import hudson.slaves.NodeProperty;
import hudson.slaves.RetentionStrategy;
import hudson.slaves.SlaveComputer;
import java.util.Arrays;
import java.util.Collections;
import org.junit.Before;
import org.junit.Rule;
Expand Down Expand Up @@ -73,7 +72,7 @@ private DumbSlave createSlave(String nodeName, String labels, EnvVars env) throw
labels==null?"":labels,
r.createComputerLauncher(env),
RetentionStrategy.NOOP,
Collections.<NodeProperty<?>>emptyList()
Collections.emptyList()
);
r.jenkins.addNode(slave);
return slave;
Expand Down Expand Up @@ -128,12 +127,12 @@ public void testThrottlingWithCategoryPerNode() throws Exception {

ThrottleJobProperty.DescriptorImpl descriptor
= (ThrottleJobProperty.DescriptorImpl)r.jenkins.getDescriptor(ThrottleJobProperty.class);
descriptor.setCategories(Arrays.asList(
descriptor.setCategories(Collections.singletonList(
new ThrottleJobProperty.ThrottleCategory(
category,
1, // maxConcurrentPerNode
null, // maxConcurrentTotal
Collections.<NodeLabeledPair>emptyList()
Collections.emptyList()
)
));

Expand All @@ -142,7 +141,7 @@ public void testThrottlingWithCategoryPerNode() throws Exception {
p1.addProperty(new ThrottleJobProperty(
null, // maxConcurrentPerNode
null, // maxConcurrentTotal
Arrays.asList(category), // categories
Collections.singletonList(category), // categories
true, // throttleEnabled
"category", // throttleOption
false,
Expand All @@ -156,7 +155,7 @@ public void testThrottlingWithCategoryPerNode() throws Exception {
p2.addProperty(new ThrottleJobProperty(
null, // maxConcurrentPerNode
null, // maxConcurrentTotal
Arrays.asList(category), // categories
Collections.singletonList(category), // categories
true, // throttleEnabled
"category", // throttleOption
false,
Expand Down Expand Up @@ -233,12 +232,12 @@ public void testThrottlingWithCategoryInFolder() throws Exception {

ThrottleJobProperty.DescriptorImpl descriptor
= (ThrottleJobProperty.DescriptorImpl)r.jenkins.getDescriptor(ThrottleJobProperty.class);
descriptor.setCategories(Arrays.asList(
descriptor.setCategories(Collections.singletonList(
new ThrottleJobProperty.ThrottleCategory(
category,
1, // maxConcurrentPerNode
null, // maxConcurrentTotal
Collections.<NodeLabeledPair>emptyList()
Collections.emptyList()
)
));

Expand All @@ -248,7 +247,7 @@ public void testThrottlingWithCategoryInFolder() throws Exception {
p1.addProperty(new ThrottleJobProperty(
null, // maxConcurrentPerNode
null, // maxConcurrentTotal
Arrays.asList(category), // categories
Collections.singletonList(category), // categories
true, // throttleEnabled
"category", // throttleOption
false, // limitOneJobWithMatchingParams
Expand All @@ -263,7 +262,7 @@ public void testThrottlingWithCategoryInFolder() throws Exception {
p2.addProperty(new ThrottleJobProperty(
null, // maxConcurrentPerNode
null, // maxConcurrentTotal
Arrays.asList(category), // categories
Collections.singletonList(category), // categories
true, // throttleEnabled
"category", // throttleOption
false, // limitOneJobWithMatchingParams
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,12 +264,7 @@ public void testDescriptorImplShouldAConcurrencySafeListForCategories() {
new ThrottleJobProperty.ThrottleCategory(
anyString(), anyInt(), anyInt(), null);

ArrayList<ThrottleJobProperty.ThrottleCategory> unsafeList =
new ArrayList<ThrottleJobProperty.ThrottleCategory>() {
{
add(category);
}
};
List<ThrottleJobProperty.ThrottleCategory> unsafeList = Collections.singletonList(category);

descriptor.setCategories(unsafeList);
List<ThrottleJobProperty.ThrottleCategory> storedCategories =
Expand All @@ -288,7 +283,7 @@ public void testDescriptorImplShouldAConcurrencySafeListForCategories() {

@Issue("JENKINS-54578")
@Test
public void clearConfiguredCategories() throws Exception {
public void clearConfiguredCategories() {
story.then(
s -> {
ThrottleJobProperty.DescriptorImpl descriptor =
Expand Down Expand Up @@ -328,7 +323,7 @@ public void clearConfiguredCategories() throws Exception {
private void assertProjects(String category, AbstractProject<?,?>... projects) {
story.j.jenkins.setAuthorizationStrategy(new RejectAllAuthorizationStrategy());
try {
assertEquals(new HashSet<Queue.Task>(Arrays.asList(projects)), new HashSet<Queue.Task>
assertEquals(new HashSet<Queue.Task>(Arrays.asList(projects)), new HashSet<>
(ThrottleJobProperty.getCategoryTasks(category)));
} finally {
story.j.jenkins.setAuthorizationStrategy(AuthorizationStrategy.UNSECURED); // do not check during e.g. rebuildDependencyGraph from delete
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import hudson.model.FreeStyleProject;
import hudson.plugins.throttleconcurrents.testutils.HtmlUnitHelper;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.concurrent.ExecutionException;
Expand Down Expand Up @@ -194,8 +193,7 @@ private void assertBasedOnMaxLabelPairMatchingOrNot(
}

private void configureGlobalThrottling(String labelRoot, int numberOfPairs, int maxConcurrentPerNode)
throws InterruptedException, IOException, MalformedURLException
{
throws InterruptedException, IOException {
URL url = new URL(r.getURL()+configUrlSuffix);
HtmlPage page = r.createWebClient().getPage(url);
HtmlForm form = page.getFormByName(configFormName);
Expand Down Expand Up @@ -224,10 +222,10 @@ private void configureGlobalThrottling(String labelRoot, int numberOfPairs, int
buttonFound = true;
for(int i=0; i<numberOfPairs; i++)
{
List<HtmlInput> inputs = null;
List<HtmlInput> inputs;
int clickThenWaitForMaxTries = 3;
do {
page = (HtmlPage)deeperButton.click();
page = deeperButton.click();
TimeUnit.SECONDS.sleep(1);
form = page.getFormByName(configFormName);
inputs = form.getInputsByName("_.throttledNodeLabel");
Expand Down Expand Up @@ -255,8 +253,7 @@ private void configureGlobalThrottling(String labelRoot, int numberOfPairs, int
}

private void configureJobThrottling(FreeStyleProject project)
throws IOException, MalformedURLException
{
throws IOException {
URL url = new URL(r.getURL()+project.getUrl()+configUrlSuffix);
HtmlPage page = r.createWebClient().getPage(url);
HtmlForm form = page.getFormByName(configFormName);
Expand Down Expand Up @@ -288,8 +285,7 @@ private void configureJobThrottling(FreeStyleProject project)
}

private void configureNewNodeWithLabel(String label)
throws IOException, MalformedURLException
{
throws IOException {
URL url = new URL(r.getURL()+"computer/new");
HtmlPage page = r.createWebClient().getPage(url);
HtmlForm form = page.getFormByName("createItem");
Expand Down Expand Up @@ -339,8 +335,7 @@ private void configureNewNodeWithLabel(String label)
}

private String configureLogger()
throws IOException, MalformedURLException
{
throws IOException {
String logger = ThrottleQueueTaskDispatcher.class.getName();
r.jenkins.getLog().doNewLogRecorder(logger);
URL url = new URL(r.getURL()+logUrlPrefix+logger+"/"+configUrlSuffix);
Expand Down Expand Up @@ -397,7 +392,7 @@ private boolean buttonFoundThusFormSubmitted(HtmlForm form, List<HtmlButton> but

private String expectedTracesMessage(String traceKind, boolean assertingTrue)
{
StringBuffer messagePrefix = new StringBuffer("log shall");
StringBuilder messagePrefix = new StringBuilder("log shall");
if(!assertingTrue) {
messagePrefix.append(" not");
}
Expand All @@ -410,8 +405,7 @@ private void failWithMessageIfButtonNotFoundOnPage(boolean buttonFound, String b
}

private HtmlPage getLoggerPage(String logger)
throws IOException, MalformedURLException
{
throws IOException {
URL url = new URL(r.getURL()+logUrlPrefix+logger);
return r.createWebClient().getPage(url);
}
Expand Down
Loading