Best Gridrouter code snippet using ru.qatools.gridrouter.RouteServlet.replyWithError
Source:RouteServlet.java
...88 terminated.set(true);89 } catch (InterruptedException e) {90 executor.shutdownNow();91 }92 replyWithError("Timed out when searching for valid host", response);93 }94 private Callable<Object> getRouteCallable(HttpServletRequest request, JsonMessage message, HttpServletResponse response,95 long requestId, int routeTimeout, AtomicBoolean terminated) {96 return () -> {97 route(request, message, response, requestId, routeTimeout, terminated);98 return null;99 };100 }101 private int getRouteTimeout(String user, JsonMessage message) {102 JsonCapabilities caps = message.getDesiredCapabilities();103 try {104 if (caps.any().containsKey(ROUTE_TIMEOUT_CAPABILITY)) {105 Integer desiredRouteTimeout = Integer.valueOf(String.valueOf(caps.any().get(ROUTE_TIMEOUT_CAPABILITY)));106 routeTimeout = (desiredRouteTimeout < MAX_ROUTE_TIMEOUT_SECONDS) ?107 desiredRouteTimeout :108 MAX_ROUTE_TIMEOUT_SECONDS;109 LOGGER.warn("[{}] [INVALID_ROUTE_TIMEOUT] [{}]", user, desiredRouteTimeout);110 }111 } catch (NumberFormatException ignored) {112 }113 return routeTimeout;114 }115 private void route(HttpServletRequest request, JsonMessage message,116 HttpServletResponse response,117 long requestId, int routeTimeout, AtomicBoolean terminated) throws IOException {118 long initialSeconds = Instant.now().getEpochSecond();119 JsonCapabilities caps = message.getDesiredCapabilities();120 String user = request.getRemoteUser();121 String remoteHost = getRemoteHost(request);122 String browser = caps.describe();123 Version actualVersion = config.findVersion(user, caps);124 if (actualVersion == null) {125 LOGGER.warn("[{}] [UNSUPPORTED_BROWSER] [{}] [{}] [{}]", requestId, user, remoteHost, browser);126 replyWithError(format("Cannot find %s capabilities on any available node",127 caps.describe()), response);128 return;129 }130 caps.setVersion(actualVersion.getNumber());131 capabilityProcessorFactory.getProcessor(caps).process(caps);132 List<Region> allRegions = actualVersion.getRegions()133 .stream().map(Region::copy).collect(toList());134 List<Region> unvisitedRegions = new ArrayList<>(allRegions);135 int attempt = 0;136 JsonMessage hubMessage = null;137 try (CloseableHttpClient client = newHttpClient(routeTimeout * 1000)) {138 if (actualVersion.getPermittedCount() != null) {139 avblBrowsersChecker.ensureFreeBrowsersAvailable(user, remoteHost, browser, actualVersion);140 }141 while (!allRegions.isEmpty() && !terminated.get()) {142 attempt++;143 Region currentRegion = hostSelectionStrategy.selectRegion(allRegions, unvisitedRegions);144 Host host = hostSelectionStrategy.selectHost(currentRegion.getHosts());145 String route = host.getRoute();146 try {147 LOGGER.info("[{}] [SESSION_ATTEMPTED] [{}] [{}] [{}] [{}] [{}]", requestId, user, remoteHost, browser, route, attempt);148 String target = route + request.getRequestURI();149 HttpResponse hubResponse = client.execute(post(target, message));150 hubMessage = JsonMessageFactory.from(hubResponse.getEntity().getContent());151 if (hubResponse.getStatusLine().getStatusCode() == SC_OK) {152 String sessionId = hubMessage.getSessionId();153 hubMessage.setSessionId(host.getRouteId() + sessionId);154 replyWithOk(hubMessage, response);155 long createdDurationSeconds = Instant.now().getEpochSecond() - initialSeconds;156 LOGGER.info("[{}] [{}] [SESSION_CREATED] [{}] [{}] [{}] [{}] [{}] [{}]",157 requestId, createdDurationSeconds, user, remoteHost, browser, route, sessionId, attempt);158 statsCounter.startSession(hubMessage.getSessionId(), user, caps.getBrowserName(), actualVersion.getNumber(), route);159 return;160 }161 LOGGER.warn("[{}] [SESSION_FAILED] [{}] [{}] [{}] [{}] - {}",162 requestId, user, remoteHost, browser, route, hubMessage.getErrorMessage());163 } catch (JsonProcessingException exception) {164 LOGGER.error("[{}] [BAD_HUB_JSON] [{}] [{}] [{}] [{}] - {}", "",165 requestId, user, remoteHost, browser, route, exception.getMessage());166 } catch (IOException exception) {167 LOGGER.error("[{}] [HUB_COMMUNICATION_FAILURE] [{}] [{}] [{}] - {}",168 requestId, user, remoteHost, browser, route, exception.getMessage());169 }170 currentRegion.getHosts().remove(host);171 if (currentRegion.getHosts().isEmpty()) {172 allRegions.remove(currentRegion);173 }174 unvisitedRegions.remove(currentRegion);175 if (unvisitedRegions.isEmpty()) {176 unvisitedRegions = new ArrayList<>(allRegions);177 }178 }179 } catch (AvailableBrowserCheckExeption e) {180 LOGGER.error("[{}] [AVAILABLE_BROWSER_CHECK_ERROR] [{}] [{}] [{}] - {}",181 requestId, user, remoteHost, browser, e.getMessage());182 }183 LOGGER.error("[{}] [SESSION_NOT_CREATED] [{}] [{}] [{}]", requestId, user, remoteHost, browser);184 if (hubMessage == null) {185 replyWithError("Cannot create session on any available node", response);186 } else {187 replyWithError(hubMessage, response);188 }189 }190 protected void replyWithOk(JsonMessage message, HttpServletResponse response) throws IOException {191 reply(SC_OK, message, response);192 }193 protected void replyWithError(String errorMessage, HttpServletResponse response) throws IOException {194 replyWithError(JsonMessageFactory.error(13, errorMessage), response);195 }196 protected void replyWithError(JsonMessage message, HttpServletResponse response) throws IOException {197 reply(SC_INTERNAL_SERVER_ERROR, message, response);198 }199 protected void reply(int code, JsonMessage message, HttpServletResponse response) throws IOException {200 response.setStatus(code);201 response.setContentType(APPLICATION_JSON.toString());202 String messageRaw = message.toJson();203 response.setContentLength(messageRaw.getBytes(UTF_8).length);204 try (OutputStream output = response.getOutputStream()) {205 IOUtils.write(messageRaw, output, UTF_8);206 }207 }208 protected HttpPost post(String target, JsonMessage message) throws IOException {209 HttpPost method = new HttpPost(target);210 StringEntity entity = new StringEntity(message.toJson(), APPLICATION_JSON);...
replyWithError
Using AI Code Generation
1package ru.qatools.gridrouter;2import org.junit.Test;3import org.junit.runner.RunWith;4import org.junit.runners.Parameterized;5import org.junit.runners.Parameterized.Parameters;6import java.util.Arrays;7import java.util.Collection;8import static org.hamcrest.CoreMatchers.is;9import static org.junit.Assert.assertThat;10@RunWith(Parameterized.class)11public class RouteServletTest {12 private String input;13 private String expected;14 public RouteServletTest(String input, String expected) {15 this.input = input;16 this.expected = expected;17 }18 public static Collection<Object[]> data() {19 return Arrays.asList(new Object[][]{20 {"", "Hello, World!"},21 {"Hello, World!", "Hello, World!"}22 });23 }24 public void testReplyWithError() throws Exception {25 RouteServlet routeServlet = new RouteServlet();26 assertThat(routeServlet.replyWithError(input), is(expected));27 }28}29package ru.qatools.gridrouter;30import org.junit.Test;31import org.junit.runner.RunWith;32import org.junit.runners.Parameterized;33import org.junit.runners.Parameterized.Parameters;34import java.util.Arrays;35import java.util.Collection;36import static org.hamcrest.CoreMatchers.is;37import static org.junit.Assert.assertThat;38@RunWith(Parameterized.class)39public class RouteServletTest {40 private String input;41 private String expected;42 public RouteServletTest(String input, String expected) {43 this.input = input;44 this.expected = expected;45 }46 public static Collection<Object[]> data() {47 return Arrays.asList(new Object[][]{48 {"", "Hello, World!"},49 {"Hello, World!", "Hello, World!"}50 });51 }52 public void testReplyWithError() throws Exception {53 RouteServlet routeServlet = new RouteServlet();54 assertThat(routeServlet.replyWithError(input), is(expected));55 }56}57package ru.qatools.gridrouter;58import org.junit.Test;59import org.junit.runner.RunWith;60import org.junit.runners.Parameterized;61import org
replyWithError
Using AI Code Generation
1import ru.qatools.gridrouter.RouteServlet2import ru.qatools.gridrouter.json.JsonCapabilities3import ru.qatools.gridrouter.json.JsonRequest4import ru.qatools.gridrouter.json.JsonResponse5import ru.qatools.gridrouter.json.JsonTestSlot6import ru.qatools.gridrouter.sessions.SessionStorage7import ru.yandex.qatools.camelot.api.EventProducer8import ru.yandex.qatools.camelot.api.annotations.Aggregator9import ru.yandex.qatools.camelot.api.annotations.Aggregator.InputType10import ru.yandex.qatools.camelot.api.annotations.Aggregator.InputType.*11import ru.yandex.qatools.camelot.api.annotations.Filter12import ru.yandex.qatools.camelot.api.annotations.OnTimer13import ru.yandex.qatools.camelot.api.annotations.OnTimer.TimerType14import ru.yandex.qatools.camelot.api.annotations.OnTimer.TimerType.*15import ru.yandex.qatools.camelot.api.annotations.Plugin16import ru.yandex.qatools.camelot.api.annotations.PluginContext17import ru.yandex.qatools.camelot.api.annotations.Producer18import ru.yandex.qatools.camelot.api.annotations.TimerId19import ru.yandex.qatools.camelot.common.builders.HeadersBuilder20import ru.yandex.qatools.camelot.common.builders.HeadersBuilder.headers21import ru.yandex.qatools.camelot.common.builders.HeadersBuilder.headersFrom22import ru.yandex.qatools.camelot.common.builders.HeadersBuilder.headersFromMap23import ru.yandex.qatools.camelot.common.builders.HeadersBuilder.headersFromObject24import ru.yandex.qatools.camelot.common.builders.HeadersBuilder.headersFromRoute25import ru.yandex.qatools.camelot.common.builders.HeadersBuilder.headersFromRouteMap26import ru.yandex.qatools.camelot.common.builders.HeadersBuilder.headersFromRouteObject27import ru.yandex.qatools.camelot.common.builders.HeadersBuilder.headersFromRouteString28import ru.yandex.qatools.camelot.common.builders.HeadersBuilder.headersFromString29import ru.yandex.qatools.camelot.common.builders.HeadersBuilder.headersFromUri30import ru.yandex
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!!