Best SeLion code snippet using com.paypal.selion.proxy.SeLionRemoteProxy.getNodeRecycleThread
Source:SeLionRemoteProxy.java
...317 proxyLogger.fine("Was this a scheduled shutdown? " + (scheduledShutdown) + " on node " + getId());318 return null;319 }320 private void startNodeRecycleThread() {321 if (!getNodeRecycleThread().isAlive()) {322 getNodeRecycleThread().start();323 }324 }325 private void stopNodeRecycleThread() {326 if (getNodeRecycleThread().isAlive()) {327 try {328 getNodeRecycleThread().shutdown();329 getNodeRecycleThread().join(2000); // Wait no longer than 2x the recycle thread's loop330 } catch (InterruptedException e) { // NOSONAR331 // ignore332 }333 }334 }335 private void updateBrowserCache(RegistrationRequest request) throws MalformedURLException {336 // Update the browser information cache. Used by GridStatics servlet337 for (MutableCapabilities desiredCapabilities : request.getConfiguration().capabilities) {338 Map<String, ?> capabilitiesMap = desiredCapabilities.asMap();339 String browserName = capabilitiesMap.get(CapabilityType.BROWSER_NAME).toString();340 String maxInstancesAsString = capabilitiesMap.get("maxInstances").toString();341 if (StringUtils.isNotBlank(browserName) && StringUtils.isNotBlank(maxInstancesAsString)) {342 int maxInstances = Integer.valueOf(maxInstancesAsString);343 BrowserInformationCache cache = BrowserInformationCache.getInstance();344 cache.updateBrowserInfo(getRemoteHost(), browserName, maxInstances);345 }346 }347 }348 @Override349 public void afterSession(TestSession session) {350 LOGGER.entering();351 totalSessionsCompleted++;352 proxyLogger.fine("Completed session #" + totalSessionsCompleted + " (" + session.toString() + ")");353 proxyLogger.fine("Total number of slots used: " + getTotalUsed() + " on node: " + getId());354 LOGGER.exiting();355 }356 /**357 * Gracefully shuts the node down by;<br>358 * <br>359 * 1. Stops accepting new sessions<br>360 * 2. Waits for sessions to complete<br>361 * 3. Calls {@link #forceNodeShutdown}<br>362 */363 public void requestNodeShutdown() {364 LOGGER.entering();365 scheduledShutdown = true;366 startNodeRecycleThread();367 LOGGER.exiting();368 }369 /**370 * Forcefully shuts the node down by calling {@link NodeForceRestartServlet}371 */372 public synchronized void forceNodeShutdown() {373 LOGGER.entering();374 // stop the node recycle thread375 stopNodeRecycleThread();376 // verify the servlet is supported on the node377 if (!canForceShutdown) {378 // allow this proxy to keep going379 disableMaxSessions();380 scheduledShutdown = false;381 LOGGER.exiting();382 return;383 }384 // clean up the test slots385 for (TestSlot slot : getTestSlots()) {386 if (slot.getSession() != null) {387 totalSessionsCompleted++;388 proxyLogger.info("Timing out session #" + totalSessionsCompleted + " (" + slot.getSession().toString()389 + ")");390 getRegistry().forceRelease(slot, SessionTerminationReason.TIMEOUT);391 }392 }393 // call the node servlet394 List<NameValuePair> nvps = new ArrayList<>();395 nvps.add(new BasicNameValuePair(NodeForceRestartServlet.TOKEN_PARAMETER,396 NodeForceRestartServlet.CONFIGURED_TOKEN_VALUE));397 HttpResponse response = sendToNodeServlet(NodeForceRestartServlet.class, nvps);398 if (response == null) {399 proxyLogger.warning("Node " + getId() + " failed to shutdown and returned a null response.");400 // stop the polling thread, mark this proxy as down, force this proxy to re-register401 teardown("it failed to shutdown and must re-register");402 LOGGER.exiting();403 return;404 }405 final int responseStatusCode = response.getStatusLine().getStatusCode();406 if (responseStatusCode != HttpStatus.SC_OK) {407 proxyLogger.warning("Node " + getId() + " did not shutdown and returned HTTP " + responseStatusCode);408 // stop the polling thread, mark this proxy as down, force this proxy to re-register409 teardown("it failed to shutdown and must re-register");410 LOGGER.exiting();411 return;412 }413 // stop the polling thread, mark this proxy as down414 teardown("it is shutdown");415 proxyLogger.info("Node " + getId() + " shutdown successfully.");416 LOGGER.exiting();417 }418 private void teardown(String reason) {419 addNewEvent(new RemoteUnregisterException(String.format("Unregistering node %s because %s.",420 getId(), reason)));421 teardown();422 }423 /**424 * Thread will recycle the node when all active sessions are completed425 */426 class NodeRecycleThread extends Thread {427 private static final int DEFAULT_TIMEOUT = 0; // Waits forever428 private volatile boolean running;429 private final String nodeId;430 NodeRecycleThread(String nodeId) {431 super();432 running = false;433 this.nodeId = nodeId;434 }435 @Override436 public void run() {437 LOGGER.entering();438 running = true;439 int timeout = getThreadWaitTimeout();440 int expired = 0;441 proxyLogger.fine("Started NodeRecycleThread with " + ((timeout == 0) ? "no" : "a " + timeout + " second")442 + " timeout for node " + nodeId);443 while (keepLooping(expired, timeout)) {444 try {445 sleep(1000);446 expired += 1;447 } catch (InterruptedException e) {448 if (running) {449 // SEVERE, only if shutdown() was not called450 LOGGER.log(Level.SEVERE, e.getMessage(), e);451 }452 running = false;453 proxyLogger.warning("NodeRecycleThread was interrupted.");454 LOGGER.exiting();455 return;456 }457 }458 if (wasExpired(expired, timeout)) {459 proxyLogger.info("Timeout occurred while waiting for sessions to complete. Shutting down the node.");460 } else {461 proxyLogger.info("All sessions are complete. Shutting down the node.");462 }463 forceNodeShutdown();464 LOGGER.exiting();465 }466 int getThreadWaitTimeout() {467 final String key = "nodeRecycleThreadWaitTimeout";468 return config.custom.containsKey(key) ? Integer.parseInt(config.custom.get(key)) : DEFAULT_TIMEOUT;469 }470 private boolean keepLooping(int expired, int timeout) {471 return (getTotalUsed() > 0) && running && ((expired < timeout) || (timeout == 0));472 }473 private boolean wasExpired(int expired, int timeout) {474 return (expired >= timeout) && (timeout != 0);475 }476 public void shutdown() {477 LOGGER.entering();478 running = false;479 proxyLogger.fine("Shutting down NodeRecycleThread for node " + nodeId);480 interrupt();481 LOGGER.exiting();482 }483 }484 /**485 * @return the {@link NodeRecycleThread} associated with this proxy486 */487 private NodeRecycleThread getNodeRecycleThread() {488 return nodeRecycleThread;489 }490 /**491 * disables the max session count for this node.492 */493 private void disableMaxSessions() {494 proxyLogger.warning("Disabling max unique sessions for Node " + getId());495 config.custom.put("uniqueSessionCount", "-1");496 }497 /**498 * @return an integer value which represents the number of unique sessions this proxy allows for before499 * automatically spinning up a {@link NodeRecycleThread}500 */501 private int getMaxSessionsAllowed() {502 final String key = "uniqueSessionCount";503 return config.custom.containsKey(key) ? Integer.parseInt(config.custom.get(key)) : DEFAULT_MAX_SESSIONS_ALLOWED;504 }505 /**506 * @return total uptime since proxy came online in minutes507 */508 public long getUptimeInMinutes() {509 return TimeUnit.MILLISECONDS.toMinutes(System.currentTimeMillis() - proxyStartMillis);510 }511 /**512 * @return total number of sessions completed since proxy came online513 */514 public int getTotalSessionsComplete() {515 return totalSessionsCompleted;516 }517 /**518 * @return total number of sessions started since proxy came online519 */520 public int getTotalSessionsStarted() {521 return totalSessionsStarted;522 }523 /**524 * @return <code>true</code> or <code>false</code>, whether the proxy is scheduled for recycle525 */526 public boolean isScheduledForRecycle() {527 return getNodeRecycleThread().isAlive();528 }529 /**530 * @return whether the proxy supports/has running {@link NodeForceRestartServlet}531 */532 public boolean supportsForceShutdown() {533 return canForceShutdown;534 }535 /**536 * @return whether the proxy supports/has running {@link NodeAutoUpgradeServlet}537 */538 public boolean supportsAutoUpgrade() {539 return canAutoUpgrade;540 }541 /**...
getNodeRecycleThread
Using AI Code Generation
1package com.paypal.selion.examples;2import java.net.MalformedURLException;3import java.net.URL;4import java.util.ArrayList;5import java.util.List;6import java.util.concurrent.TimeUnit;7import org.openqa.selenium.By;8import org.openqa.selenium.WebDriver;9import org.openqa.selenium.WebElement;10import org.openqa.selenium.remote.DesiredCapabilities;11import org.openqa.selenium.remote.RemoteWebDriver;12import org.testng.Assert;13import org.testng.annotations.Test;14import com.paypal.selion.proxy.SeLionRemoteProxy;15public class SelionRemoteProxyExample {16 public void testSelionRemoteProxy() throws MalformedURLException {17 DesiredCapabilities capabilities = DesiredCapabilities.firefox();18 WebDriver driver = new RemoteWebDriver(new URL(hubUrl + "/wd/hub"),19 capabilities);20 driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);21 WebElement element = driver.findElement(By.name("q"));22 element.sendKeys("Cheese!");23 element.submit();24 System.out.println("Page title is: " + driver.getTitle());25 System.out.println("Page title is: " + driver.getTitle());26 driver.quit();27 SeLionRemoteProxy proxy = new SeLionRemoteProxy(new URL(hubUrl), capabilities);28 List<WebDriver> drivers = new ArrayList<WebDriver>();29 drivers.add(driver);30 proxy.getNodeRecycleThread().setDrivers(drivers);31 proxy.getNodeRecycleThread().run();32 Assert.assertTrue(proxy.getNodeRecycle
getNodeRecycleThread
Using AI Code Generation
1import com.paypal.selion.proxy.SeLionRemoteProxy;2import org.openqa.grid.common.RegistrationRequest;3import org.openqa.grid.internal.Registry;4import org.openqa.grid.internal.RemoteProxy;5import org.openqa.grid.internal.utils.configuration.GridHubConfiguration;6import org.openqa.grid.internal.utils.configuration.GridNodeConfiguration;7import org.openqa.grid.selenium.GridLauncherV3;8import org.openqa.grid.web.Hub;9import org.openqa.selenium.remote.server.SeleniumServer;10public class Test {11 public static void main(String[] args) throws Exception {12 GridHubConfiguration hubConfig = new GridHubConfiguration();13 hubConfig.setPort(4444);14 Hub hub = new Hub(hubConfig);15 hub.start();16 GridNodeConfiguration nodeConfig = new GridNodeConfiguration();17 nodeConfig.setPort(5555);18 nodeConfig.setHub(hubConfig.getHubHost() + ":" + hubConfig.getPort());19 nodeConfig.setCapabilities(RegistrationRequest.build("-role", "node").getCapabilities());20 SeleniumServer node = new SeleniumServer(nodeConfig);21 node.boot();22 Registry registry = hub.getRegistry();23 RemoteProxy proxy = registry.getAllProxies().iterator().next();24 Thread nodeRecycleThread = SeLionRemoteProxy.getNodeRecycleThread(proxy);25 System.out.println(nodeRecycleThread);26 node.stop();27 GridLauncherV3.killProcess(nodeConfig.getRemoteHost(), nodeConfig.getPort());28 nodeRecycleThread.join();29 System.out.println("Node's recycle thread completed");30 }31}
getNodeRecycleThread
Using AI Code Generation
1SeLionRemoteProxy proxy = (SeLionRemoteProxy) Registry.getRegistry().getAllProxies().iterator().next();2Thread nodeRecycleThread = proxy.getNodeRecycleThread();3System.out.println(nodeRecycleThread.isAlive());4SeLionRemoteProxy proxy = (SeLionRemoteProxy) Registry.getRegistry().getAllProxies().iterator().next();5Thread nodeRecycleThread = new Thread();6proxy.setNodeRecycleThread(nodeRecycleThread);7SeLionRemoteProxy proxy = (SeLionRemoteProxy) Registry.getRegistry().getAllProxies().iterator().next();8long waitTime = proxy.getRecycleThreadWaitTime();9System.out.println(waitTime);10SeLionRemoteProxy proxy = (SeLionRemoteProxy) Registry.getRegistry().getAllProxies().iterator().next();11long waitTime = 1000;12proxy.setRecycleThreadWaitTime(waitTime);13SeLionRemoteProxy proxy = (SeLionRemoteProxy) Registry.getRegistry().getAllProxies().iterator().next();14Thread nodeRecycleThread = proxy.getNodeRecycleThread();15System.out.println(nodeRecycleThread.isAlive());16SeLionRemoteProxy proxy = (SeLionRemoteProxy) Registry.getRegistry().getAllProxies().iterator().next();17Thread nodeRecycleThread = new Thread();18proxy.setNodeRecycleThread(nodeRecycleThread);
getNodeRecycleThread
Using AI Code Generation
1import com.paypal.selion.proxy.SeLionRemoteProxy;2SeLionRemoteProxy proxy = new SeLionRemoteProxy();3Thread nodeRecycleThread = proxy.getNodeRecycleThread();4nodeRecycleThread.start();5SeLionRemoteProxy proxy = new SeLionRemoteProxy();6Thread nodeRecycleThread = proxy.getNodeRecycleThread();7nodeRecycleThread.start();8proxy.recycleNode();9import com.paypal.selion.proxy.SeLionRemoteProxy;10SeLionRemoteProxy proxy = new SeLionRemoteProxy();11Thread nodeRecycleThread = proxy.getNodeRecycleThread();12nodeRecycleThread.start();13SeLionRemoteProxy proxy = new SeLionRemoteProxy();14Thread nodeRecycleThread = proxy.getNodeRecycleThread();15nodeRecycleThread.start();16proxy.recycleNode();17import com.paypal.selion.proxy.SeLionRemoteProxy;18SeLionRemoteProxy proxy = new SeLionRemoteProxy();19Thread nodeRecycleThread = proxy.getNodeRecycleThread();20nodeRecycleThread.start();21SeLionRemoteProxy proxy = new SeLionRemoteProxy();22Thread nodeRecycleThread = proxy.getNodeRecycleThread();23nodeRecycleThread.start();24proxy.recycleNode();25import com.paypal.selion.proxy.SeLionRemoteProxy;26SeLionRemoteProxy proxy = new SeLionRemoteProxy();27Thread nodeRecycleThread = proxy.getNodeRecycleThread();28nodeRecycleThread.start();29SeLionRemoteProxy proxy = new SeLionRemoteProxy();30Thread nodeRecycleThread = proxy.getNodeRecycleThread();31nodeRecycleThread.start();32proxy.recycleNode();
getNodeRecycleThread
Using AI Code Generation
1import com.paypal.selion.proxy.SeLionRemoteProxy;2import com.paypal.selion.proxy.SeLionRemoteProxyPool;3import org.openqa.grid.internal.RemoteProxy;4import org.openqa.grid.internal.TestSlot;5import org.openqa.grid.web.servlet.handler.RequestHandler;6public class CustomRequestHandler extends RequestHandler {7 public CustomRequestHandler(Request request, SeleniumBasedRequest seleniumBasedRequest, Registry registry) {8 super(request, seleniumBasedRequest, registry);9 }10 public void forwardNewSessionRequestAndUpdateRegistry() {11 RemoteProxy proxy = getRegistry().getProxyById(getTestId());12 Thread recycleThread = SeLionRemoteProxyPool.getNodeRecycleThread((SeLionRemoteProxy) proxy);13 long threadId = recycleThread.getId();14 super.forwardNewSessionRequestAndUpdateRegistry();15 }16}17import com.paypal.selion.proxy.SeLionRemoteProxy;18import com.paypal.selion.proxy.SeLionRemoteProxyPool;19import org.openqa.grid.internal.RemoteProxy;20import org.openqa.grid.internal.TestSlot;21import org.openqa.grid.web.servlet.handler.RequestHandler;22public class CustomRequestHandler extends RequestHandler {23 public CustomRequestHandler(Request request, SeleniumBasedRequest seleniumBasedRequest, Registry registry) {24 super(request, seleniumBasedRequest, registry);25 }26 public void forwardNewSessionRequestAndUpdateRegistry() {27 RemoteProxy proxy = getRegistry().getProxyById(getTestId());28 Thread recycleThread = SeLionRemoteProxyPool.getNodeRecycleThread((SeLionRemoteProxy) proxy);29 long threadId = recycleThread.getId();
getNodeRecycleThread
Using AI Code Generation
1System.out.println("Thread Name: " + threadName);2System.out.println("Thread Name: " + threadName);3System.out.println("Thread Name: " + threadName);4System.out.println("Thread Name: " + threadName);5System.out.println("Thread Name: " + threadName);6System.out.println("Thread Name: " + threadName);7System.out.println("Thread Name: " + threadName);
getNodeRecycleThread
Using AI Code Generation
1import com.paypal.selion.configuration.Config;2import com.paypal.selion.configuration.Config.ConfigProperty;3import com.paypal.selion.proxy.SeLionRemoteProxy;4public class Test {5 public static void main(String[] args) {6 Config config = Config.getConfig();7 String hubHost = config.getStringConfig(ConfigProperty.SELENIUM_HOST);8 int hubPort = Integer.parseInt(config.getStringConfig(ConfigProperty.SELENIUM_PORT));9 SeLionRemoteProxy proxy = new SeLionRemoteProxy(hubHost, hubPort, null);10 proxy.getNodeRecycleThread().stopThread();11 }12}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!