WaitAvailableBrowsersChecker implements AvailableBrowsersChecker { @Override public void ensureFreeBrowsersAvailable(String user, String remoteHost, String browser, Version version) { int waitAttempt = 0; final String requestId = randomUUID().toString(); final Temporal waitingStarted = now(); final Duration maxWait = Duration.ofSeconds(queueTimeout); while (maxWait.compareTo(Duration.between(waitingStarted, now())) > 0 && (countSessions(user, browser, version)) >= version.getPermittedCount()) { try { onWait(user, browser, version, requestId, waitAttempt); Thread.sleep(SECONDS.toMillis(queueWaitInterval)); } catch (InterruptedException e) { LOGGER.error("Failed to sleep thread", e); } if (maxWait.compareTo(Duration.between(waitingStarted, now())) < 0) { onWaitTimeout(user, browser, version, requestId, waitAttempt); } } onWaitFinished(user, browser, version, requestId, waitAttempt); } WaitAvailableBrowsersChecker();  WaitAvailableBrowsersChecker(int queueTimeout, int queueWaitInterval, StatsCounter statsCounter); @Override void ensureFreeBrowsersAvailable(String user, String remoteHost, String browser, Version version);  }
@Test public void testNoWaitAvailableBrowser() throws Exception { when(counter.getSessionsCountForUserAndBrowser(eq("user"), eq("firefox"), eq("33"))).thenReturn(5); Temporal started = now(); checker.ensureFreeBrowsersAvailable("user", "host", "firefox", version); verify(counter, times(1)).getSessionsCountForUserAndBrowser(eq("user"), eq("firefox"), eq("33")); assertThat(Duration.between(started, now()).toMillis(), lessThan(1000L)); verifyNoMoreInteractions(counter); }
@Test(expected = WaitAvailableBrowserTimeoutException.class) public void testWaitAvailableBrowsersTimeout() throws Exception { checker.ensureFreeBrowsersAvailable("user", "host", "firefox", version); }