Best Cerberus-source code snippet using org.cerberus.engine.queuemanagement.impl.ExecutionQueueWorkerThread.checkCreditLimit
Source:ExecutionQueueWorkerThread.java
...219 @Override220 public void run() {221 try {222 LOG.debug("Checking credit limit on : " + queueId + " with RobotHost : " + selectedRobotHost + " with RobotExtensionHost : " + selectedRobotExtHost);223 checkCreditLimit();224 LOG.debug("Start to execute : " + queueId + " with RobotHost : " + selectedRobotHost + " with RobotExtensionHost : " + selectedRobotExtHost);225 LOG.debug("Get queue exe to execute : " + queueId);226 // Getting the queue full object.227 setToExecute(queueService.convert(queueService.readByKey(queueId, false)));228 StringBuilder url = new StringBuilder();229 url.append(cerberusExecutionUrl);230 url.append(RunTestCaseV002.SERVLET_URL);231 url.append("?");232 url.append(makeParamRequest().mkString().replace(" ", "+"));233 LOG.debug("Make http call : " + queueId);234 // Make the http call and parse the output.235 runParseAnswer(runExecution(url), cerberusExecutionUrl + RunTestCaseV002.SERVLET_URL, url.toString());236 } catch (Exception e) {237 LOG.warn("Execution in queue " + queueId + " has finished with error");238 LOG.error(e, e);239 try {240 queueService.updateToError(queueId, e.getMessage());241 queueDepService.manageDependenciesEndOfQueueExecution(queueId);242 // If error, we check that campaign is finished.243 tagService.manageCampaignEndOfExecution(getToExecute().getTag());244 // Trigger Queue Job245 LOG.debug("trigger extra job.");246 triggerQueueJob(cerberusTriggerQueueJobUrl);247 } catch (CerberusException again) {248 LOG.error("Unable to mark execution in queue " + queueId + " as in error", again);249 }250 }251 }252 /**253 * Request the queue job to be executed calling the corresponding servlet.254 *255 * @return the execution answer from the {@link RunTestCase} servlet256 * @throws RunQueueProcessException if an error occurred during request257 * execution258 * @see #run()259 */260 private String triggerQueueJob(String url) {261 try {262 LOG.debug("Trigger Job Queue calling Service URL : " + url);263 LOG.debug("Trigger Execution with TimeOut : " + toExecuteTimeout);264 CloseableHttpClient httpclient = HttpClientBuilder.create().disableAutomaticRetries().build();265 RequestConfig requestConfig = RequestConfig.custom()266 .setConnectTimeout(toExecuteTimeout)267 .setConnectionRequestTimeout(toExecuteTimeout)268 .setSocketTimeout(toExecuteTimeout)269 .build();270 HttpGet httpGet = new HttpGet(url);271 httpGet.setConfig(requestConfig);272 CloseableHttpResponse response = httpclient.execute(httpGet);273 HttpEntity entity = response.getEntity();274 String responseContent = EntityUtils.toString(entity);275 return responseContent;276 } catch (Exception e) {277 final StringBuilder errorMessage = new StringBuilder("An unexpected error occurred trigering Queue Job: ");278 if (e instanceof HttpResponseException) {279 errorMessage.append(String.format("%d (%s)", ((HttpResponseException) e).getStatusCode(), e.getMessage()));280 } else {281 errorMessage.append(e.getMessage());282 errorMessage.append(". Check server logs");283 }284 LOG.error(errorMessage.toString(), e);285 throw new RunQueueProcessException(errorMessage.toString(), e);286 }287 }288 /**289 * Request execution of the inner {@link TestCaseExecutionQueue} to the290 * {@link RunTestCase} servlet291 *292 * @return the execution answer from the {@link RunTestCase} servlet293 * @throws RunQueueProcessException if an error occurred during request294 * execution295 * @see #run()296 */297 private String runExecution(StringBuilder url) {298 try {299 LOG.debug("Trigger Execution to URL : " + url.toString());300 LOG.debug("Trigger Execution with TimeOut : " + toExecuteTimeout);301 CloseableHttpClient httpclient = HttpClientBuilder.create().disableAutomaticRetries().build();302 RequestConfig requestConfig = RequestConfig.custom()303 .setConnectTimeout(toExecuteTimeout)304 .setConnectionRequestTimeout(toExecuteTimeout)305 .setSocketTimeout(toExecuteTimeout)306 .build();307 HttpGet httpGet = new HttpGet(url.toString());308 httpGet.setConfig(requestConfig);309 httpGet.setHeader("apikey", apiKeyService.getServiceAccountAPIKey());310 CloseableHttpResponse response = httpclient.execute(httpGet);311 HttpEntity entity = response.getEntity();312 String responseContent = EntityUtils.toString(entity);313 return responseContent;314 } catch (Exception e) {315 final StringBuilder errorMessage = new StringBuilder("An unexpected error occurred during test case execution: ");316 if (e instanceof HttpResponseException) {317 errorMessage.append(String.format("%d (%s)", ((HttpResponseException) e).getStatusCode(), e.getMessage()));318 } else {319 errorMessage.append(e.getMessage());320 errorMessage.append(". Check server logs");321 }322 LOG.error(errorMessage.toString(), e);323 throw new RunQueueProcessException(errorMessage.toString(), e);324 }325 }326 /**327 * Parse the answer given by the {@link RunTestCase}328 * <p>329 * @param answer the {@link RunTestCase}'s answer330 * @throws RunQueueProcessException if an error occurred if execution was on331 * failure or if answer cannot be parsed332 * @see #run()333 */334 private void runParseAnswer(String answer, String cerberusUrl, String cerberusFullUrl) {335 // Check answer format336 Matcher matcher = EXECUTION_ID_FROM_ANSWER_PATTERN.matcher(answer);337 if (!matcher.find()) {338 LOG.error("Bad answer format (could not find 'RunID = '). URL Called: " + cerberusFullUrl);339 LOG.error("Bad answer format (could not find 'RunID = '). Answer: " + answer);340 throw new RunQueueProcessException("Error occured when calling the service to run the testcase. Service answer did not have the expected format (missing 'RunID = '). Probably due to bad cerberus_url value. URL Called: '" + cerberusUrl + "'.");341 }342 // Extract the return code343 Long executionID;344 try {345 executionID = Long.parseLong(matcher.group(1));346 } catch (NumberFormatException e) {347 LOG.error("Bad answer format (executionId is not numeric). Answer: " + answer);348 throw new RunQueueProcessException("Bad return code format: " + matcher.group(1));349 }350 // Check if return code is in error351 if (executionID == 0) {352 Matcher descriptionMatcher = RETURN_CODE_DESCRIPTION_FROM_ANSWER_PATTERN.matcher(answer);353 if (!descriptionMatcher.find()) {354 LOG.error("Bad answer format (could not find 'ReturnCodeDescription = '). URL Called: " + cerberusFullUrl);355 LOG.error("Bad answer format (could not find 'ReturnCodeDescription = '). Answer: " + answer);356 throw new RunQueueProcessException("Error occured when calling the service to run the testcase. Service answer did not have the expected format (missing 'ReturnCodeDescription = '). Probably due to bad cerberus_url value. URL Called: '" + cerberusUrl + "'.");357 }358 throw new RunQueueProcessException(descriptionMatcher.group(1));359 }360 }361 /**362 * Control is execution can be executed following consumption constrains.363 * <p>364 * @throws RunQueueProcessException if either daily duration of nb of365 * execution limit has been reached.366 * @see #run()367 */368 private void checkCreditLimit() {369 int maxNbExe = parameterService.getParameterIntegerByKey(Parameter.VALUE_cerberus_creditlimit_nbexeperday, "", 0);370 int maxSecondExe = parameterService.getParameterIntegerByKey(Parameter.VALUE_cerberus_creditlimit_secondexeperday, "", 0);371 if ((maxNbExe != 0) && (sessionCounter.getCreditLimitNbExe() >= maxNbExe)) {372 throw new RunQueueProcessException("Your Number of Execution Credit Limit (" + maxNbExe + ") was been reached !!!");373 }374 if ((maxSecondExe != 0) && (sessionCounter.getCreditLimitSecondExe() >= maxSecondExe)) {375 throw new RunQueueProcessException("Your Execution Time Credit Limit (" + maxSecondExe + " min) was been reached !!!");376 }377 }378 @Override379 public String toString() {380 return this.cerberusExecutionUrl;381 }382}...
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!!