<commit_before>/*
 * JBoss, Home of Professional Open Source
 *
 * Copyright 2016 Red Hat, Inc. and/or its affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.wildfly.elytron.web.undertow.server.util;

import javax.net.ssl.SSLContext;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.function.Supplier;

import io.undertow.UndertowOptions;
import io.undertow.protocols.ssl.UndertowXnioSsl;
import io.undertow.server.HttpHandler;
import io.undertow.server.HttpServerExchange;
import io.undertow.server.OpenListener;
import io.undertow.server.protocol.http.HttpOpenListener;
import org.junit.rules.ExternalResource;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
import org.xnio.BufferAllocator;
import org.xnio.ByteBufferSlicePool;
import org.xnio.ChannelListener;
import org.xnio.ChannelListeners;
import org.xnio.OptionMap;
import org.xnio.Options;
import org.xnio.StreamConnection;
import org.xnio.Xnio;
import org.xnio.XnioWorker;
import org.xnio.channels.AcceptingChannel;

/**
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 */
public class UndertowServer extends ExternalResource {

    private Xnio xnio;
    private XnioWorker worker;
    private OptionMap serverOptions;
    private OpenListener openListener;
    private ChannelListener acceptListener;
    private AcceptingChannel<? extends StreamConnection> server;
    private HttpHandler rootHttpHandler = null;
    private final Supplier<SSLContext> serverSslContext;
    private final int port;

    public UndertowServer(HttpHandler root) {
        this(root, (Supplier<SSLContext>) null);
    }

    public UndertowServer(HttpHandler root, int port) {
        this(root, port, null);
    }

    public UndertowServer(HttpHandler root, Supplier<SSLContext> serverSslContext) {
        this(root, 7776, serverSslContext);
    }

    public UndertowServer(HttpHandler root, int port, Supplier<SSLContext> serverSslContext) {
        this.rootHttpHandler = root;
        this.port = port;
        this.serverSslContext = serverSslContext;
    }

    @Override
    public Statement apply(Statement base, Description description) {
        return super.apply(base, description);
    }

    @Override
    protected void before() throws Throwable {
        // Stolen directly from Undertow ;-)
        xnio = Xnio.getInstance("nio", UndertowServer.class.getClassLoader());
        worker = xnio.createWorker(OptionMap.builder()
                .set(Options.WORKER_IO_THREADS, 8)
                .set(Options.CONNECTION_HIGH_WATER, 1000000)
                .set(Options.CONNECTION_LOW_WATER, 1000000)
                .set(Options.WORKER_TASK_CORE_THREADS, 30)
                .set(Options.WORKER_TASK_MAX_THREADS, 30)
                .set(Options.TCP_NODELAY, true)
                .set(Options.CORK, true)
                .getMap());

        serverOptions = OptionMap.builder()
                .set(Options.TCP_NODELAY, true)
                .set(Options.BACKLOG, 1000)
                .set(Options.REUSE_ADDRESSES, true)
                .set(Options.BALANCING_TOKENS, 1)
                .set(Options.BALANCING_CONNECTIONS, 2)
                .getMap();

        ByteBufferSlicePool pool = new ByteBufferSlicePool(BufferAllocator.BYTE_BUFFER_ALLOCATOR, 8192, 8192 * 8192);

        openListener = new HttpOpenListener(pool, OptionMap.create(UndertowOptions.BUFFER_PIPELINED_DATA, true,
                UndertowOptions.ENABLE_CONNECTOR_STATISTICS, true));
        openListener.setRootHandler((HttpServerExchange exchange) -> rootHttpHandler.handleRequest(exchange));
        acceptListener = ChannelListeners.openListenerAdapter(openListener);

        if (serverSslContext != null) {
            UndertowXnioSsl ssl = new UndertowXnioSsl(xnio, OptionMap.EMPTY, serverSslContext.get());
            server = ssl.createSslConnectionServer(worker, new InetSocketAddress("localhost", port), acceptListener, serverOptions);
        } else {
            server = worker.createStreamConnectionServer(new InetSocketAddress("localhost", port), acceptListener, serverOptions);
        }

        server.resumeAccepts();
    }

    @Override
    protected void after() {
        if (server == null) {
            return;
        }
        try {
            server.close();
            worker.shutdown();
            server = null;
            acceptListener = null;
            openListener = null;
            serverOptions = null;
            worker = null;
            xnio = null;
        } catch (IOException e) {
            throw new RuntimeException("Failed to destroy server", e);
        }
    }

    public URI getServerUri() throws URISyntaxException {
        return new URI("http", null, "localhost", port, null, null, null);
    }

    public void forceShutdown() {
        after();
    }
}<commit_msg>Resolves #39 OOM when running tests. Keeping UndertowServer simple.<commit_after>/*
 * JBoss, Home of Professional Open Source
 *
 * Copyright 2016 Red Hat, Inc. and/or its affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.wildfly.elytron.web.undertow.server.util;

import javax.net.ssl.SSLContext;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.function.Supplier;

import io.undertow.Undertow;
import io.undertow.server.HttpHandler;
import org.junit.rules.ExternalResource;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;

/**
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 */
public class UndertowServer extends ExternalResource {

    private Undertow server;
    private HttpHandler rootHttpHandler = null;
    private final Supplier<SSLContext> serverSslContext;
    private final int port;

    public UndertowServer(HttpHandler root) {
        this(root, (Supplier<SSLContext>) null);
    }

    public UndertowServer(HttpHandler root, int port) {
        this(root, port, null);
    }

    public UndertowServer(HttpHandler root, Supplier<SSLContext> serverSslContext) {
        this(root, 7776, serverSslContext);
    }

    public UndertowServer(HttpHandler root, int port, Supplier<SSLContext> serverSslContext) {
        this.rootHttpHandler = root;
        this.port = port;
        this.serverSslContext = serverSslContext;
    }

    @Override
    public Statement apply(Statement base, Description description) {
        return super.apply(base, description);
    }

    @Override
    protected void before() throws Throwable {
        Undertow.Builder builder = Undertow.builder().setBufferSize(512);

        if (serverSslContext != null) {
            builder.addHttpsListener(port,  "localhost", serverSslContext.get(), rootHttpHandler);
        } else {
            builder.addHttpListener(port, "localhost", rootHttpHandler);
        }

        server = builder.build();
        server.start();
    }

    @Override
    protected void after() {
        if (server == null) {
            return;
        }
        server.stop();
        server = null;
    }

    public URI getServerUri() throws URISyntaxException {
        return new URI("http", null, "localhost", port, null, null, null);
    }

    public void forceShutdown() {
        after();
    }
}
{'fraction_non_alphanumeric': 0.0939233180612491, 'fraction_numerical': 0.007957559681697613, 'mean_word_length': 3.074165029469548, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4268409', 'n_tokens_mistral': 2407, 'n_tokens_neox': 2263, 'n_words': 608}