Search in sources :

Example 1 with Ints

use of com.google.common.primitives.Ints in project divolte-collector by divolte.

the class AsyncRequestBodyReceiver method receive.

public void receive(final BiConsumer<InputStream, Integer> callback, final HttpServerExchange exchange) {
    Objects.requireNonNull(callback);
    if (logger.isDebugEnabled()) {
        logger.debug("Pre-allocating buffer with {} chunks.", preallocateChunks.get());
    }
    if (exchange.isRequestComplete()) {
        logger.debug("Request already completed; zero length content body.");
        callback.accept(EMPTY_INPUT_STREAM, 0);
        return;
    }
    // Determine the number of buffer-slots to use, trusting a well-formed content-length
    // header if that's present.
    final Optional<Integer> contentLength = Optional.ofNullable(exchange.getRequestHeaders().getFirst(Headers.CONTENT_LENGTH)).map(Ints::tryParse);
    if (logger.isDebugEnabled()) {
        logger.debug("Content length claimed by request: {}", contentLength);
    }
    final int provisionChunks = contentLength.map(AsyncRequestBodyReceiver::calculateChunks).orElseGet(preallocateChunks::get);
    if (provisionChunks > maximumChunks) {
        logger.info("Rejecting request; anticipated content length ({}) will exceed exceed maximum allowed {})", contentLength, maximumChunks * ChunkyByteBuffer.CHUNK_SIZE);
        rejectLargeRequest(exchange);
        return;
    }
    // If we've reached this point we're always going to accept the body, so continuing is fine.
    if (HttpContinue.requiresContinueResponse(exchange)) {
        logger.debug("Request requires permission to proceed; allowing to continue.");
        HttpContinue.sendContinueResponse(exchange, new IoCallback() {

            @Override
            public void onComplete(final HttpServerExchange exchange, final Sender sender) {
                receive(callback, exchange, contentLength, provisionChunks);
            }

            @Override
            public void onException(final HttpServerExchange exchange, final Sender sender, final IOException exception) {
                logger.error("Error allowing request to continue.");
                exchange.endExchange();
            }
        });
    } else {
        receive(callback, exchange, contentLength, provisionChunks);
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HttpServerExchange(io.undertow.server.HttpServerExchange) Sender(io.undertow.io.Sender) Ints(com.google.common.primitives.Ints) IoCallback(io.undertow.io.IoCallback) IOException(java.io.IOException)

Example 2 with Ints

use of com.google.common.primitives.Ints in project divolte-collector by divolte.

the class SeleniumTestBase method setupSauceLabs.

private void setupSauceLabs() throws MalformedURLException {
    final String sauceUserName = Optional.ofNullable(System.getenv(SAUCE_USER_NAME_ENV_VAR)).orElseThrow(() -> new RuntimeException("When using 'sauce' as Selenium driver, please set the SauceLabs username " + "in the " + SAUCE_USER_NAME_ENV_VAR + " env var."));
    final String sauceApiKey = Optional.ofNullable(System.getenv(SAUCE_API_KEY_ENV_VAR)).orElseThrow(() -> new RuntimeException("When using 'sauce' as Selenium driver, please set the SauceLabs username " + "in the " + SAUCE_API_KEY_ENV_VAR + " env var."));
    final String sauceHost = Optional.ofNullable(System.getenv(SAUCE_HOST_ENV_VAR)).orElse("localhost");
    final int saucePort = Optional.ofNullable(System.getenv(SAUCE_PORT_ENV_VAR)).map(Ints::tryParse).orElse(4445);
    final DesiredCapabilities caps = capabilities.get();
    // Note: getMethodName() is misleading. It's really the formatted name from the @Parameters annotation.
    caps.setCapability("name", String.format("%s: %s", getClass().getSimpleName(), testName.getMethodName()));
    caps.setCapability("public", "team");
    caps.setCapability("videoUploadOnPass", false);
    final RemoteWebDriver remoteDriver = new RemoteWebDriver(new URL(String.format("http://%s:%s@%s:%d/wd/hub", sauceUserName, sauceApiKey, sauceHost, saucePort)), caps);
    final String sauceJobId = remoteDriver.getSessionId().toString();
    final SauceREST sauce = new SauceREST(sauceUserName, sauceApiKey);
    driver = remoteDriver;
    testResultHook = Optional.of(result -> {
        switch(result) {
            case Passed:
                sauce.jobPassed(sauceJobId);
                break;
            case Skipped:
                sauce.deleteJob(sauceJobId);
                break;
            case Failed:
                sauce.jobFailed(sauceJobId);
        }
    });
}
Also used : WebDriverWait(org.openqa.selenium.support.ui.WebDriverWait) Statement(org.junit.runners.model.Statement) ChromeDriver(org.openqa.selenium.chrome.ChromeDriver) java.util(java.util) URL(java.net.URL) WebDriver(org.openqa.selenium.WebDriver) WebDriverException(org.openqa.selenium.WebDriverException) RunWith(org.junit.runner.RunWith) Parameters(org.junit.runners.Parameterized.Parameters) ExpectedCondition(org.openqa.selenium.support.ui.ExpectedCondition) LoggerFactory(org.slf4j.LoggerFactory) Supplier(java.util.function.Supplier) ParametersAreNonnullByDefault(javax.annotation.ParametersAreNonnullByDefault) JavascriptExecutor(org.openqa.selenium.JavascriptExecutor) ImmutableList(com.google.common.collect.ImmutableList) Nullable(javax.annotation.Nullable) BrowserLists(io.divolte.server.BrowserLists) AssumptionViolatedException(org.junit.AssumptionViolatedException) PhantomJSDriver(org.openqa.selenium.phantomjs.PhantomJSDriver) SafariDriver(org.openqa.selenium.safari.SafariDriver) Logger(org.slf4j.Logger) MalformedURLException(java.net.MalformedURLException) Parameter(org.junit.runners.Parameterized.Parameter) Description(org.junit.runner.Description) DesiredCapabilities(org.openqa.selenium.remote.DesiredCapabilities) RemoteWebDriver(org.openqa.selenium.remote.RemoteWebDriver) Instant(java.time.Instant) Ints(com.google.common.primitives.Ints) SauceREST(com.saucelabs.saucerest.SauceREST) Collectors(java.util.stream.Collectors) TestServer(io.divolte.server.ServerTestUtils.TestServer) Consumer(java.util.function.Consumer) org.junit.rules(org.junit.rules) Rule(org.junit.Rule) Preconditions(com.google.common.base.Preconditions) Logs(org.openqa.selenium.logging.Logs) RemoteWebDriver(org.openqa.selenium.remote.RemoteWebDriver) DesiredCapabilities(org.openqa.selenium.remote.DesiredCapabilities) SauceREST(com.saucelabs.saucerest.SauceREST) URL(java.net.URL)

Aggregations

Ints (com.google.common.primitives.Ints)2 Preconditions (com.google.common.base.Preconditions)1 ImmutableList (com.google.common.collect.ImmutableList)1 SauceREST (com.saucelabs.saucerest.SauceREST)1 BrowserLists (io.divolte.server.BrowserLists)1 TestServer (io.divolte.server.ServerTestUtils.TestServer)1 IoCallback (io.undertow.io.IoCallback)1 Sender (io.undertow.io.Sender)1 HttpServerExchange (io.undertow.server.HttpServerExchange)1 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 Instant (java.time.Instant)1 java.util (java.util)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Consumer (java.util.function.Consumer)1 Supplier (java.util.function.Supplier)1 Collectors (java.util.stream.Collectors)1 Nullable (javax.annotation.Nullable)1 ParametersAreNonnullByDefault (javax.annotation.ParametersAreNonnullByDefault)1