Skip to content

Commit

Permalink
Merge pull request #168 from rinrinne/boot-on-startup
Browse files Browse the repository at this point in the history
Add "No connection on bootup" option
  • Loading branch information
rsandell committed Aug 12, 2014
2 parents 7807966 + 63b3868 commit b49ce36
Show file tree
Hide file tree
Showing 10 changed files with 148 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -199,14 +199,10 @@ public JSONObject getServerStatuses() {

for (GerritServer server : PluginImpl.getInstance().getServers()) {
String status;
if (server.isPseudoMode()) {
status = "na";
if (server.isConnected()) {
status = "up";
} else {
if (server.isConnected()) {
status = "up";
} else {
status = "down";
}
status = "down";
}

obj = new JSONObject();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ private void addThisAsListener() {
GerritServer server = PluginImpl.getInstance().getServer(serverName);
if (server != null) {
server.addListener(this);
connected = server.isConnected() && (!server.isPseudoMode());
connected = server.isConnected();
} else {
logger.error("Could not find the server {}", serverName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ public class GerritServer implements Describable<GerritServer>, Action {
private static final int RESPONSE_INTERVAL_MS = 1000;
private static final int RESPONSE_TIMEOUT_S = 10;
private String name;
private boolean pseudoMode;
@Deprecated
private transient boolean pseudoMode;
private boolean noConnectionOnStartup;
private transient boolean started;
private transient boolean timeoutWakeup = false;
private transient String connectionResponse = "";
Expand Down Expand Up @@ -183,11 +185,12 @@ public GerritServer(String name) {
* Constructor.
*
* @param name the name of the server.
* @param pseudoMode if pseudo mode or not.
* @param noConnectionOnStartup if noConnectionOnStartup or not.
*/
public GerritServer(String name, boolean pseudoMode) {
public GerritServer(String name, boolean noConnectionOnStartup) {
this.name = name;
this.pseudoMode = pseudoMode;
this.pseudoMode = false;
this.noConnectionOnStartup = noConnectionOnStartup;
config = new Config();
}

Expand Down Expand Up @@ -223,6 +226,7 @@ public String getName() {
*
* @return true if so.
*/
@Deprecated
public boolean isPseudoMode() {
return pseudoMode;
}
Expand All @@ -232,10 +236,29 @@ public boolean isPseudoMode() {
*
* @param pseudoMode true if pseudoMode connection.
*/
@Deprecated
public void setPseudoMode(boolean pseudoMode) {
this.pseudoMode = pseudoMode;
}

/**
* If no connection on startup or not.
*
* @return true if so.
*/
public boolean isNoConnectionOnStartup() {
return noConnectionOnStartup;
}

/**
* Sets connect on startup.
*
* @param noConnectionOnStartup true if connect on startup.
*/
public void setNoConnectionOnStartup(boolean noConnectionOnStartup) {
this.noConnectionOnStartup = noConnectionOnStartup;
}

/**
* Gets wakeup is failed by timeout or not.
*
Expand Down Expand Up @@ -310,12 +333,6 @@ public void start() {
config.setCategories(categories);
gerritEventManager = PluginImpl.getInstance().getHandler();

if (pseudoMode) {
logger.info(name + " started (pseudo mode)");
started = true;
return;
}

initializeConnectionListener();

projectListUpdater = new GerritProjectListUpdater(name);
Expand Down Expand Up @@ -345,12 +362,6 @@ private void initializeConnectionListener() {
public void stop() {
logger.info("Stopping GerritServer " + name);

if (pseudoMode) {
logger.info(name + " stopped (pseudo mode)");
started = false;
return;
}

if (projectListUpdater != null) {
projectListUpdater.shutdown();
try {
Expand Down Expand Up @@ -425,11 +436,6 @@ public GerritConnectionListener getGerritConnectionListener() {
*
*/
public synchronized void startConnection() {
if (pseudoMode) {
gerritEventManager.setIgnoreEMail(name, config.getGerritEMail());
return;
}

if (!config.hasDefaultValues()) {
if (gerritConnection == null) {
logger.debug("Starting Gerrit connection...");
Expand All @@ -450,11 +456,6 @@ public synchronized void startConnection() {
*
*/
public synchronized void stopConnection() {
if (pseudoMode) {
gerritEventManager.setIgnoreEMail(name, null);
return;
}

if (gerritConnection != null) {
gerritConnection.shutdown(true);
gerritConnection.removeListener(gerritConnectionListener);
Expand All @@ -472,9 +473,6 @@ public synchronized void stopConnection() {
*/

public synchronized boolean isConnected() {
if (pseudoMode) {
return true;
}
if (gerritConnection != null) {
return gerritConnection.isConnected();
}
Expand All @@ -486,9 +484,6 @@ public synchronized boolean isConnected() {
*
*/
public void restartConnection() {
if (pseudoMode) {
return;
}
stopConnection();
startConnection();
}
Expand Down Expand Up @@ -570,7 +565,6 @@ public String getDisplayName() {

/**
* Tests if the provided parameters can connect to Gerrit.
* @param pseudoMode if pseudo mode or not.
* @param gerritHostName the hostname
* @param gerritSshPort the ssh-port
* @param gerritProxy the proxy url
Expand All @@ -581,16 +575,12 @@ public String getDisplayName() {
* {@link FormValidation#error(java.lang.String) } otherwise.
*/
public FormValidation doTestConnection(
@QueryParameter("pseudoMode") final boolean pseudoMode,
@QueryParameter("gerritHostName") final String gerritHostName,
@QueryParameter("gerritSshPort") final int gerritSshPort,
@QueryParameter("gerritProxy") final String gerritProxy,
@QueryParameter("gerritUserName") final String gerritUserName,
@QueryParameter("gerritAuthKeyFile") final String gerritAuthKeyFile,
@QueryParameter("gerritAuthKeyFilePassword") final String gerritAuthKeyFilePassword) {
if (pseudoMode) {
return FormValidation.ok(Messages.Success());
}
if (logger.isDebugEnabled()) {
logger.debug("gerritHostName = {}\n"
+ "gerritSshPort = {}\n"
Expand Down Expand Up @@ -775,12 +765,6 @@ public void doConfigSubmit(StaplerRequest req, StaplerResponse rsp) throws Servl
logger.debug("submit {}", req.toString());
}
JSONObject form = req.getSubmittedForm();
if (isConnected()) {
pseudoMode = false;
form.put("pseudoMode", false);
} else {
pseudoMode = form.getBoolean("pseudoMode");
}

String newName = form.getString("name");
boolean renamed = false;
Expand All @@ -793,6 +777,7 @@ public void doConfigSubmit(StaplerRequest req, StaplerResponse rsp) throws Servl
rename(newName);
renamed = true;
}
noConnectionOnStartup = form.getBoolean("noConnectionOnStartup");
config.setValues(form);

PluginImpl.getInstance().save();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ public void onDeleted(Item item) {
public void onLoaded() {
if (PluginImpl.getInstance() != null) {
for (GerritServer s : PluginImpl.getInstance().getServers()) {
s.startConnection();
if (!s.isNoConnectionOnStartup()) {
s.startConnection();
}
}
}
super.onLoaded();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
value="${it.name}"
checkUrl="'${rootURL}/${serverURL}/nameFreeCheck?value='+escape(this.value)"/>
</f:entry>
<f:entry title="${%Pseudo Mode}"
help="/plugin/gerrit-trigger/help-GerritPseudoMode.html">
<f:checkbox name="pseudoMode"
checked="${it.pseudoMode}"
<f:entry title="${%No Connection On Startup}"
help="/plugin/gerrit-trigger/help-GerritNoConnectionOnStartup.html">
<f:checkbox name="noConnectionOnStartup"
checked="${it.noConnectionOnStartup}"
default="false"/>
</f:entry>
<f:entry title="${%Hostname}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ Gerrit\ Server=\
Gerrit\u30b5\u30fc\u30d0\u30fc
Name=\
\u540d\u524d
Pseudo\ Mode=\
\u64ec\u4f3c\u30e2\u30fc\u30c9
No\ Connection\ On\ Startup=\
\u8d77\u52d5\u6642\u306b\u63a5\u7d9a\u3057\u306a\u3044
Hostname=\
\u30db\u30b9\u30c8\u540d
Frontend\ URL=\
Expand Down
1 change: 1 addition & 0 deletions src/main/webapp/help-GerritNoConnectionOnStartup.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Check if this server should not connect to Gerrit on startup.
7 changes: 0 additions & 7 deletions src/main/webapp/help-GerritPseudoMode.html

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -316,23 +316,14 @@ private void addNewServerByCopyingConfig(String newServerName, String fromServer
}

/**
* Test triggering job with events from a Gerrit server with Pseudo mode.
* Test not conect to Gerrit on startup.
* @throws Exception Error creating job.
*/
@Test
public void testTriggeringFromGerritServersWithPseudoMode() throws Exception {
public void testConnectOnStartup() throws Exception {
GerritServer gerritServerOne = new GerritServer(gerritServerOneName, true);
PluginImpl.getInstance().addServer(gerritServerOne);
gerritServerOne.start();
FreeStyleProject projectOne = DuplicatesUtil.createGerritTriggeredJob(this, projectOneName, gerritServerOneName);
PluginImpl.getInstance().getHandler().post(Setup.createPatchsetCreated(gerritServerOneName));
RunList<FreeStyleBuild> buildsOne = DuplicatesUtil.waitForBuilds(projectOne, 1, timeToBuild);

FreeStyleBuild buildOne = buildsOne.get(0);
assertSame(Result.SUCCESS, buildOne.getResult());
assertEquals(1, projectOne.getBuilds().size());
assertSame(gerritServerOneName, buildOne.getCause(GerritCause.class).getEvent().getProvider().getName());
assertEquals(true, gerritServerOne.isNoConnectionOnStartup());
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* The MIT License
*
* Copyright 2014 rinrinne a.k.a. rin_ne 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.hudsontrigger;

import static org.powermock.api.mockito.PowerMockito.mock;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.powermock.api.mockito.PowerMockito.when;
import static org.powermock.api.mockito.PowerMockito.spy;
import static org.powermock.api.mockito.PowerMockito.doNothing;

import java.util.Arrays;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.jvnet.hudson.test.JenkinsRule;
import org.mockito.Mockito;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import com.sonyericsson.hudson.plugins.gerrit.trigger.GerritServer;
import com.sonyericsson.hudson.plugins.gerrit.trigger.PluginImpl;

/**
* Tests for {@link GerritItemListener}.
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest(PluginImpl.class)
@PowerMockIgnore({"javax.crypto.*" })
public class GerritItemListenerTest {

/**
* Jenkins rule instance.
*/
// CS IGNORE VisibilityModifier FOR NEXT 3 LINES. REASON: Mocks tests.
@Rule
public JenkinsRule j = new JenkinsRule();

private final String gerritServerName = "testServer";
private GerritServer gerritServer;

/**
* Setup the mock'ed environment.
*/
@Before
public void setup() {
PluginImpl plugin = mock(PluginImpl.class);
mockStatic(PluginImpl.class);
when(PluginImpl.getInstance()).thenReturn(plugin);
gerritServer = spy(new GerritServer(gerritServerName));
doNothing().when(gerritServer).startConnection();
when(plugin.getServers()).thenReturn(Arrays.asList(gerritServer));
}

/**
* Tests {@link GerritItemListener#onLoad()} gets connection.
*
* @throws Exception if so.
*/
@Test
public void testOnLoadedWithConnection() throws Exception {
gerritServer.setNoConnectionOnStartup(false);
GerritItemListener listener = new GerritItemListener();
listener.onLoaded();
Mockito.verify(gerritServer, Mockito.times(1)).startConnection();
}

/**
* Tests {@link GerritItemListener#onLoad()} does not get connection.
*
* @throws Exception if so.
*/
@Test
public void testOnLoadedWithNoConnection() throws Exception {
gerritServer.setNoConnectionOnStartup(true);
GerritItemListener listener = new GerritItemListener();
listener.onLoaded();
Mockito.verify(gerritServer, Mockito.times(0)).startConnection();
}
}

0 comments on commit b49ce36

Please sign in to comment.