Skip to content

Commit

Permalink
Fix JENKINS-22155
Browse files Browse the repository at this point in the history
  • Loading branch information
rsandell committed Mar 21, 2014
1 parent 6e15374 commit 6370c37
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 10 deletions.
Expand Up @@ -104,9 +104,11 @@ private static Pattern buildLinePattern() {
}

/**
* Read and parse the dynamic trigger configuration
* Read and parse the dynamic trigger configuration.
*
* @param reader stream from which to read the config
* @param serverName the name of the Gerrit server.
* @param serverName the name of the Gerrit server configured in the project, could be null.
*
* @return List of Gerrit projects
* @throws ParseException when the fetched content couldn't be parsed
* @throws IOException for all other kinds of fetch errors
Expand Down
Expand Up @@ -420,7 +420,7 @@ private Provider initializeProvider(GerritTriggeredEvent tEvent) {
*
* @return true if so.
*/
private boolean isAnyServer() {
boolean isAnyServer() {
return serverName == null || serverName.isEmpty() || ANY_SERVER.equals(serverName);
}

Expand Down
Expand Up @@ -23,12 +23,14 @@
*/
package com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger;

import com.sonyericsson.hudson.plugins.gerrit.gerritevents.GerritDefaultValues;
import com.sonyericsson.hudson.plugins.gerrit.trigger.GerritServer;
import com.sonyericsson.hudson.plugins.gerrit.trigger.PluginImpl;
import hudson.util.TimeUnit2;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Timer;
import java.util.TimerTask;

/**
* Manages the timer that is used for each GerritTrigger TimerTask that
Expand Down Expand Up @@ -77,15 +79,63 @@ public static GerritTriggerTimer getInstance() {
return instance;
}

//CS IGNORE LineLength FOR NEXT 5 LINES. REASON: JavaDoc

/**
* Figures out what
* {@link com.sonyericsson.hudson.plugins.gerrit.trigger.config.IGerritHudsonTriggerConfig#getDynamicConfigRefreshInterval()}
* to use.
*
* @param timerTask the timerTask that should be scheduled.
*
* @return the refresh interval in ms.
* @see #calculateAverageDynamicConfigRefreshInterval()
*/
private long calculateDynamicConfigRefreshInterval(GerritTriggerTimerTask timerTask) {
if (timerTask.getGerritTrigger().isAnyServer()) {
if (PluginImpl.getInstance().getServers() == null || PluginImpl.getInstance().getServers().isEmpty()) {
return GerritDefaultValues.DEFAULT_DYNAMIC_CONFIG_REFRESH_INTERVAL;
} else {
//Do an average just for giggles
return calculateAverageDynamicConfigRefreshInterval();
}
} else {
//get the actual if it exists.
GerritServer server = PluginImpl.getInstance().getServer(timerTask.getGerritTrigger().getServerName());
if (server != null) {
return server.getConfig().getDynamicConfigRefreshInterval();
} else {
//Do an average just for giggles
return calculateAverageDynamicConfigRefreshInterval();
}
}
}

//CS IGNORE LineLength FOR NEXT 5 LINES. REASON: JavaDoc

/**
* Calculates the average
* {@link com.sonyericsson.hudson.plugins.gerrit.trigger.config.IGerritHudsonTriggerConfig#getDynamicConfigRefreshInterval()}
* among the configured GerritServers.
*
* @return the average value.
*/
private long calculateAverageDynamicConfigRefreshInterval() {
long total = 0;
for (GerritServer server : PluginImpl.getInstance().getServers()) {
total += server.getConfig().getDynamicConfigRefreshInterval();
}
long average = total / PluginImpl.getInstance().getServers().size();
return Math.max(GerritDefaultValues.MINIMUM_DYNAMIC_CONFIG_REFRESH_INTERVAL, average);
}

/**
* Schedule a TimerTask according to the two constants above.
*
* @param timerTask the subclass of TimerTask to be scheduled
* @param serverName the name of the Gerrit server.
* @param timerTask the TimerTask to be scheduled
*/
public void schedule(TimerTask timerTask, String serverName) {
long timerPeriod = MILLISECONDS_PER_SECOND
* PluginImpl.getInstance().getServer(serverName).getConfig().getDynamicConfigRefreshInterval();
public void schedule(GerritTriggerTimerTask timerTask) {
long timerPeriod = TimeUnit2.SECONDS.toMillis(calculateDynamicConfigRefreshInterval(timerTask));
try {
timer.schedule(timerTask, DELAY_MILLISECONDS, timerPeriod);
} catch (IllegalArgumentException iae) {
Expand Down
Expand Up @@ -41,7 +41,7 @@ public class GerritTriggerTimerTask extends TimerTask {
*/
GerritTriggerTimerTask(GerritTrigger gerritTrigger) {
this.gerritTrigger = gerritTrigger;
GerritTriggerTimer.getInstance().schedule(this, gerritTrigger.getServerName());
GerritTriggerTimer.getInstance().schedule(this);
}

/**
Expand All @@ -51,4 +51,13 @@ public class GerritTriggerTimerTask extends TimerTask {
public void run() {
gerritTrigger.updateTriggerConfigURL();
}

/**
* The {@link GerritTrigger} that created this timerTask.
*
* @return the trigger.
*/
public GerritTrigger getGerritTrigger() {
return gerritTrigger;
}
}
@@ -0,0 +1,51 @@
/*
* The MIT License
*
* Copyright 2012, 2014 Sony Mobile Communications AB. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.sonyericsson.hudson.plugins.gerrit.trigger.spec;

import com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.GerritTrigger;
import hudson.model.FreeStyleProject;
import org.jvnet.hudson.test.HudsonTestCase;
import org.jvnet.hudson.test.recipes.LocalData;

/**
* Tests to see if jobs from Gerrit-Trigger v. 2.10.1 can be loaded.
*
* @author Robert Sandell <robert.sandell@sonyericsson.com>
*/
public class BackCompat2101HudsonTest extends HudsonTestCase {

/**
* Tests that a dynamic trigger config in 2.10.1 carries over correctly.
* @see https://issues.jenkins-ci.org/browse/JENKINS-22155
*/
@LocalData
public void testDynamicTriggerLoading() {
FreeStyleProject project = (FreeStyleProject)jenkins.getItem("DynamicTrigger");
assertNotNull(project);
GerritTrigger trigger = GerritTrigger.getTrigger(project);
assertNotNull(trigger);
assertTrue(trigger.isDynamicTriggerConfiguration());
assertEquals("file://tmp/some.txt", trigger.getTriggerConfigURL());
}
}
Binary file not shown.

0 comments on commit 6370c37

Please sign in to comment.