From 95772760dd4e98697e9dbf6e16f0be49804ce72c Mon Sep 17 00:00:00 2001 From: zaro0508 Date: Thu, 28 Mar 2013 14:16:23 -0700 Subject: [PATCH] tests for GearmanProxy Added new tests for GearmanProxy. Also changed method names init_workers and stop_all to initWorkers and stopAll. more in line with the java-ish camelcase convention. Change-Id: I081236d334afd1c57aab8213b80e080dc5dccb53 --- .../plugins/gearman/GearmanPluginConfig.java | 4 +- .../hudson/plugins/gearman/GearmanProxy.java | 5 +- .../plugins/gearman/GearmanProxyTest.java | 102 ++++++++++++++++++ 3 files changed, 107 insertions(+), 4 deletions(-) create mode 100644 src/test/java/hudson/plugins/gearman/GearmanProxyTest.java diff --git a/src/main/java/hudson/plugins/gearman/GearmanPluginConfig.java b/src/main/java/hudson/plugins/gearman/GearmanPluginConfig.java index b3e1358..a0e5feb 100644 --- a/src/main/java/hudson/plugins/gearman/GearmanPluginConfig.java +++ b/src/main/java/hudson/plugins/gearman/GearmanPluginConfig.java @@ -107,10 +107,10 @@ public class GearmanPluginConfig extends GlobalConfiguration { "host"); } - GearmanProxy.getInstance().init_worker(); + GearmanProxy.getInstance().initWorkers(); } else { - GearmanProxy.getInstance().stop_all(); + GearmanProxy.getInstance().stopAll(); } req.bindJSON(this, json); diff --git a/src/main/java/hudson/plugins/gearman/GearmanProxy.java b/src/main/java/hudson/plugins/gearman/GearmanProxy.java index 150f77c..258358e 100644 --- a/src/main/java/hudson/plugins/gearman/GearmanProxy.java +++ b/src/main/java/hudson/plugins/gearman/GearmanProxy.java @@ -47,6 +47,7 @@ public class GearmanProxy { private final List gewtHandles; private final List gmwtHandles; + // Singleton instance public static synchronized GearmanProxy getInstance() { if (gearmanProxy == null) { gearmanProxy = new GearmanProxy(); @@ -64,7 +65,7 @@ public class GearmanProxy { /* * This method initializes the gearman workers. */ - public void init_worker() { + public void initWorkers() { /* * Purpose here is to create a 1:1 mapping of 'gearman worker':'jenkins @@ -161,7 +162,7 @@ public class GearmanProxy { /* * This method stops all gearman workers */ - public void stop_all() { + public void stopAll() { // stop gearman executors synchronized(gewtHandles) { for (AbstractWorkerThread gewtHandle : gewtHandles) { // stop executors diff --git a/src/test/java/hudson/plugins/gearman/GearmanProxyTest.java b/src/test/java/hudson/plugins/gearman/GearmanProxyTest.java new file mode 100644 index 0000000..ae3c654 --- /dev/null +++ b/src/test/java/hudson/plugins/gearman/GearmanProxyTest.java @@ -0,0 +1,102 @@ +/* + * + * Copyright 2013 Hewlett-Packard Development Company, L.P. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package hudson.plugins.gearman; + +import hudson.slaves.DumbSlave; + +import org.junit.Test; +import org.jvnet.hudson.test.HudsonTestCase; + +/** + * Test for the {@link ExecutorWorkerThread} class. + * + * @author Khai Do + */ +public class GearmanProxyTest extends HudsonTestCase { + + GearmanProxy gp = GearmanProxy.getInstance(); + + @Test + public void testGetNumExecutors() throws Exception { + + DumbSlave slave = createSlave(); + + assertEquals(0, gp.getNumExecutors()); + + gp.getGewtHandles().add(new ExecutorWorkerThread("localhost", 4730, "test-exec-0", slave)); + gp.getGewtHandles().add(new ExecutorWorkerThread("localhost", 4730, "test-exec-1", slave)); + gp.getGewtHandles().add(new ExecutorWorkerThread("localhost", 4730, "test-exec-2", slave)); + + assertEquals(3, gp.getNumExecutors()); + + gp.getGewtHandles().add(new ManagementWorkerThread("localhost", 4730, "manage-exec-2")); + + assertEquals(4, gp.getNumExecutors()); + + gp.getGewtHandles().clear(); + } + + @Test + public void testCreateManagementWorker() { + + assertEquals(0, gp.getGmwtHandles().size()); + + gp.createManagementWorker(); + + assertEquals(1, gp.getGmwtHandles().size()); + assertTrue(gp.getGmwtHandles().get(0).isAlive()); + gp.getGmwtHandles().clear(); + } + + @Test + public void testCreateExecutorWorkersOnNode() throws Exception { + + DumbSlave slave = createSlave(); + + assertEquals(0, gp.getGewtHandles().size()); + + gp.createExecutorWorkersOnNode(slave.toComputer()); + assertEquals(1, gp.getGewtHandles().size()); + + gp.getGewtHandles().clear(); + } + + @Test + public void testInitWorkers() { + + gp.initWorkers(); + + assertEquals(2, gp.getGewtHandles().size()); + + gp.getGmwtHandles().clear(); + gp.getGewtHandles().clear(); + } + + @Test + public void testInitWorkers2() throws Exception { + + DumbSlave slave = createSlave(); + gp.initWorkers(); + + assertEquals(3, gp.getGewtHandles().size()); + + gp.getGmwtHandles().clear(); + gp.getGewtHandles().clear(); + } +}