Skip to content

Commit

Permalink
Revamp DefaultRemoteProxyTest
Browse files Browse the repository at this point in the history
Signed-off-by: Alexei Barantsev <[email protected]>
  • Loading branch information
krmahadevan authored and barancev committed Nov 23, 2017
1 parent 0af6b3b commit 2c8a1a5
Showing 1 changed file with 66 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Object>());
assertNotNull(newSession );
Thread.sleep(2);
p.forceSlotCleanerRun();
assertTrue(p.getRegistry().getActiveSessions().isEmpty());
registry.add(p);
((SelfHealingProxy) p).stopPolling();
return p;
}

}

0 comments on commit 2c8a1a5

Please sign in to comment.