/** Deletes the specified FrameGroupCommandQueueSet */
 public static void clearQueueSet(String sessionId) {
   LOGGER.debug("clearing queue set");
   FrameGroupCommandQueueSet queue = FrameGroupCommandQueueSet.queueSets.get(sessionId);
   if (null != queue) {
     queue.endOfLife();
     FrameGroupCommandQueueSet.queueSets.remove(sessionId);
   }
 }
 @Test
 public void testGetGlobalQueueSpeed() {
   assertEquals(defaultSpeed, CommandQueue.getSpeed());
   FrameGroupCommandQueueSet session1 =
       FrameGroupCommandQueueSet.makeQueueSet(
           firstSessionId,
           RemoteControlConfiguration.DEFAULT_PORT,
           new RemoteControlConfiguration());
   int sessionSpeedOnInit = session1.getSpeed();
   assertEquals(defaultSpeed, sessionSpeedOnInit);
   FrameGroupCommandQueueSet.clearQueueSet(firstSessionId);
 }
 public void testGetPresetSpeedValidSession() {
   assertEquals(defaultSpeed, CommandQueue.getSpeed());
   FrameGroupCommandQueueSet session1 =
       FrameGroupCommandQueueSet.makeQueueSet(
           firstSessionId,
           RemoteControlConfiguration.DEFAULT_PORT,
           new RemoteControlConfiguration());
   assertNotNull(session1);
   SeleniumDriverResourceHandler.setSpeedForSession(firstSessionId, newSpeed);
   String speed = SeleniumDriverResourceHandler.getSpeedForSession(firstSessionId);
   assertEquals(newSpeedString, speed);
   FrameGroupCommandQueueSet.clearQueueSet(firstSessionId);
 }
  @Test
  public void testCommandQueueInitSpeedMatchesSessionSpeed() {
    assertEquals(defaultSpeed, CommandQueue.getSpeed());
    FrameGroupCommandQueueSet session1 =
        FrameGroupCommandQueueSet.makeQueueSet(
            firstSessionId,
            RemoteControlConfiguration.DEFAULT_PORT,
            new RemoteControlConfiguration());
    session1.setSpeed(newSpeed);

    CommandQueue queue1 = session1.getCommandQueue(firstQueueId);
    assertEquals(newSpeed, queue1.getQueueDelay());
    assertEquals(defaultSpeed, CommandQueue.getSpeed());

    FrameGroupCommandQueueSet.clearQueueSet(firstSessionId);
  }
 /**
  * Shuts down this browser session's launcher and clears out its session data (if session is not
  * null).
  *
  * @param sessionInfo the browser session to end.
  */
 protected void shutdownBrowserAndClearSessionData(BrowserSessionInfo sessionInfo) {
   try {
     sessionInfo.launcher.close(); // can throw RuntimeException
   } finally {
     if (null != sessionInfo.session) {
       FrameGroupCommandQueueSet.clearQueueSet(sessionInfo.sessionId);
     }
   }
 }
  /**
   * Creates and tries to open a new session.
   *
   * @param browserString
   * @param startURL
   * @param extensionJs
   * @param configuration Remote Control configuration. Cannot be null.
   * @param ensureClean if a clean session is required
   * @return the BrowserSessionInfo of the new session.
   * @throws RemoteCommandException if the browser failed to launch and request work in the required
   *     amount of time.
   */
  protected BrowserSessionInfo createNewRemoteSession(
      String browserString,
      String startURL,
      String extensionJs,
      Capabilities browserConfiguration,
      boolean ensureClean,
      RemoteControlConfiguration configuration)
      throws RemoteCommandException {

    final FrameGroupCommandQueueSet queueSet;
    final BrowserSessionInfo sessionInfo;
    final BrowserLauncher launcher;
    String sessionId;

    sessionId = UUID.randomUUID().toString().replace("-", "");
    if ("*webdriver".equals(browserString) && browserConfiguration != null) {
      Object id = browserConfiguration.getCapability("webdriver.remote.sessionid");
      if (id != null && id instanceof String) {
        sessionId = (String) id;
      }
    }

    queueSet = makeQueueSet(sessionId, configuration.getPortDriversShouldContact(), configuration);
    queueSet.setExtensionJs(extensionJs);

    try {
      launcher =
          browserLauncherFactory.getBrowserLauncher(
              browserString, sessionId, configuration, browserConfiguration);
    } catch (InvalidBrowserExecutableException e) {
      throw new RemoteCommandException(e.getMessage(), "");
    }

    sessionInfo = new BrowserSessionInfo(sessionId, browserString, startURL, launcher, queueSet);
    SessionIdTracker.setLastSessionId(sessionId);
    log.info("Allocated session " + sessionId + " for " + startURL + ", launching...");

    final PerSessionLogHandler perSessionLogHandler = LoggingManager.perSessionLogHandler();
    perSessionLogHandler.attachToCurrentThread(new SessionId(sessionId));
    try {
      launcher.launchRemoteSession(startURL);
      queueSet.waitForLoad(configuration.getTimeoutInSeconds() * 1000l);

      // TODO DGF log4j only
      // NDC.push("sessionId="+sessionId);
      FrameGroupCommandQueueSet queue = getQueueSet(sessionId);
      queue.doCommand("setContext", sessionId, "");

      activeSessions.add(sessionInfo);
      return sessionInfo;
    } catch (Exception e) {
      /*
       * At this point the session might not have been added to neither available nor active
       * sessions. This session is unlikely to be of any practical use so we need to make sure we
       * close the browser and clear all session data.
       */
      log.log(
          Level.SEVERE,
          "Failed to start new browser session, shutdown browser and clear all session data",
          e);
      shutdownBrowserAndClearSessionData(sessionInfo);
      throw new RemoteCommandException("Error while launching browser", "", e);
    } finally {
      perSessionLogHandler.detachFromCurrentThread();
    }
  }
 /**
  * Isolated dependency
  *
  * @param sessionId
  * @return an existing FrameGroupCommandQueueSet instance
  */
 protected FrameGroupCommandQueueSet getQueueSet(String sessionId) {
   return FrameGroupCommandQueueSet.getQueueSet(sessionId);
 }
 /**
  * Isolated dependency
  *
  * @param sessionId
  * @param port
  * @param configuration
  * @return a new FrameGroupCommandQueueSet instance
  */
 protected FrameGroupCommandQueueSet makeQueueSet(
     String sessionId, int port, RemoteControlConfiguration configuration) {
   return FrameGroupCommandQueueSet.makeQueueSet(
       sessionId, configuration.getPortDriversShouldContact(), configuration);
 }