From 2c8a1a5d59904d23085b1f04b8309ca033251740 Mon Sep 17 00:00:00 2001 From: Krishnan Mahadevan Date: Tue, 21 Nov 2017 09:02:46 +0530 Subject: [PATCH] Revamp DefaultRemoteProxyTest Signed-off-by: Alexei Barantsev --- .../proxy/DefaultRemoteProxyTest.java | 76 ++++++++++++++++--- 1 file changed, 66 insertions(+), 10 deletions(-) diff --git a/java/server/test/org/openqa/grid/selenium/proxy/DefaultRemoteProxyTest.java b/java/server/test/org/openqa/grid/selenium/proxy/DefaultRemoteProxyTest.java index a02f94bbc0b23..22a77dca238b2 100644 --- a/java/server/test/org/openqa/grid/selenium/proxy/DefaultRemoteProxyTest.java +++ b/java/server/test/org/openqa/grid/selenium/proxy/DefaultRemoteProxyTest.java @@ -17,38 +17,94 @@ package org.openqa.grid.selenium.proxy; -import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertFalse; import com.beust.jcommander.JCommander; +import org.junit.After; import org.junit.Test; import org.openqa.grid.common.RegistrationRequest; import org.openqa.grid.internal.BaseRemoteProxy; import org.openqa.grid.internal.DefaultGridRegistry; import org.openqa.grid.internal.GridRegistry; -import org.openqa.grid.internal.TestSession; +import org.openqa.grid.internal.listeners.SelfHealingProxy; +import org.openqa.grid.internal.mock.GridHelper; +import org.openqa.grid.internal.mock.MockedRequestHandler; +import org.openqa.grid.internal.utils.configuration.GridHubConfiguration; import org.openqa.grid.internal.utils.configuration.GridNodeConfiguration; +import org.openqa.grid.web.Hub; +import org.openqa.selenium.remote.server.jmx.JMXHelper; import java.util.HashMap; +import java.util.Random; +import java.util.concurrent.TimeoutException; + +import javax.management.MalformedObjectNameException; +import javax.management.ObjectName; public class DefaultRemoteProxyTest { @Test - public void proxyTimeout() throws InterruptedException { - GridRegistry registry = DefaultGridRegistry.newInstance(); - registry.getConfiguration().timeout = 1; + public void ensureCleanupHappensWhenProxyTimesoutAndCleanupIsEnabled() + throws TimeoutException, InterruptedException { + BaseRemoteProxy p = createProxyAndSimulateTimeout(1000); + assertTrue("Ensure there are NO active sessions", + p.getRegistry().getActiveSessions().isEmpty()); + assertEquals("Ensure that there are NO used slots", 0, p.getTotalUsed()); + } + + @Test + public void ensureNoCleanupHappensWhenProxyTimesoutAndCleanupIsDisabled() + throws TimeoutException, InterruptedException { + BaseRemoteProxy p = createProxyAndSimulateTimeout(0); + assertFalse("Ensure there are active sessions", p.getRegistry().getActiveSessions().isEmpty()); + assertEquals("Ensure that 1 slot is still marked as in use", 1, p.getTotalUsed()); + } + + @After + public void unregisterHubFromJMX() throws MalformedObjectNameException { + ObjectName obj = new ObjectName("org.seleniumhq.grid:type=Hub"); + new JMXHelper().unregister(obj); + } + + private static BaseRemoteProxy createProxyAndSimulateTimeout(int cleanupCycle) + throws TimeoutException, InterruptedException { + GridRegistry registry = DefaultGridRegistry.newInstance(new Hub(new GridHubConfiguration())); + registry.getHub().getConfiguration().timeout = 1; + registry.getHub().getConfiguration().cleanUpCycle = cleanupCycle; GridNodeConfiguration nodeConfiguration = new GridNodeConfiguration(); + nodeConfiguration.port = new Random().nextInt(100); + nodeConfiguration.timeout = 1; new JCommander(nodeConfiguration, "-role", "webdriver"); RegistrationRequest req = RegistrationRequest.build(nodeConfiguration); req.getConfiguration().proxy = DefaultRemoteProxy.class.getName(); + BaseRemoteProxy p = createMockProxyWithPollingDisabled(req, registry); + MockedRequestHandler reqHandler = GridHelper.createNewSessionHandler(registry, new HashMap<>()); + registry.addNewSessionRequest(reqHandler); + reqHandler.waitForSessionBound(); + assertFalse("Ensure a new session was created", registry.getActiveSessions().isEmpty()); + simulateTimeout(cleanupCycle); + return p; + } + private static void simulateTimeout(int cleanupCycle) { + int sleepMoreThanCleanupCycle = cleanupCycle + 1000; + try { + Thread.sleep(sleepMoreThanCleanupCycle); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + + private static BaseRemoteProxy createMockProxyWithPollingDisabled(RegistrationRequest req, + GridRegistry registry) { BaseRemoteProxy p = BaseRemoteProxy.getNewInstance(req, registry); - TestSession newSession = p.getNewSession(new HashMap()); - assertNotNull(newSession ); - Thread.sleep(2); - p.forceSlotCleanerRun(); - assertTrue(p.getRegistry().getActiveSessions().isEmpty()); + registry.add(p); + ((SelfHealingProxy) p).stopPolling(); + return p; } + }