<commit_before>/*
 * JBoss, Home of Professional Open Source
 * Copyright 2008, JBoss Inc., and individual contributors as indicated
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

package org.jboss.xnio.core.nio.test;

import junit.framework.TestCase;
import org.jboss.xnio.spi.Provider;
import org.jboss.xnio.spi.TcpServerService;
import org.jboss.xnio.spi.Lifecycle;
import org.jboss.xnio.spi.TcpConnectorService;
import org.jboss.xnio.core.nio.NioProvider;
import org.jboss.xnio.channels.ConnectedStreamChannel;
import org.jboss.xnio.channels.ChannelOption;
import org.jboss.xnio.IoFuture;
import org.jboss.xnio.IoHandler;
import org.jboss.xnio.IoHandlerFactory;
import org.jboss.xnio.IoUtils;
import org.jboss.xnio.test.support.LoggingHelper;
import static org.jboss.xnio.Buffers.flip;
import static org.jboss.xnio.IoUtils.safeClose;
import static org.jboss.xnio.IoUtils.nullHandler;
import java.net.SocketAddress;
import java.net.InetSocketAddress;
import java.net.Inet4Address;
import java.nio.ByteBuffer;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

/**
 *
 */
public final class NioTcpTestCase extends TestCase {
    static {
        LoggingHelper.init();
    }

    private static final int SERVER_PORT = 12345;

    private static final void stop(Lifecycle lifecycle) {
        try {
            lifecycle.stop();
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }

    private void doConnectionTest(final Runnable body, final IoHandler<? super ConnectedStreamChannel<SocketAddress>> clientHandler, final IoHandler<? super ConnectedStreamChannel<SocketAddress>> serverHandler) throws Exception {
        synchronized (this) {
            final Provider provider = new NioProvider();
            provider.start();
            try {
                final TcpServerService tcpServerService = provider.createTcpServer();
                tcpServerService.setReuseAddress(true);
                tcpServerService.setBindAddresses(new SocketAddress[] { new InetSocketAddress(Inet4Address.getByAddress(new byte[] { 127, 0, 0, 1 }), SERVER_PORT)});
                tcpServerService.setHandlerFactory(new IoHandlerFactory<ConnectedStreamChannel<SocketAddress>>() {
                    public IoHandler<? super ConnectedStreamChannel<SocketAddress>> createHandler() {
                        return serverHandler;
                    }
                });
                tcpServerService.start();
                try {
                    final TcpConnectorService connectorService = provider.createTcpConnector();
                    connectorService.setConnectTimeout(10);
                    connectorService.start();
                    try {
                        final IoFuture<ConnectedStreamChannel<SocketAddress>> ioFuture = connectorService.connectTo(new InetSocketAddress(Inet4Address.getByAddress(new byte[] { 127, 0, 0, 1 }), SERVER_PORT), clientHandler);
                        final ConnectedStreamChannel<SocketAddress> connectedStreamChannel = ioFuture.get();
                        try {
                            body.run();
                            connectedStreamChannel.close();
                        } finally {
                            safeClose(connectedStreamChannel);
                        }
                    } finally {<commit_msg>Fix test case problem<commit_after>/*
 * JBoss, Home of Professional Open Source
 * Copyright 2008, JBoss Inc., and individual contributors as indicated
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

package org.jboss.xnio.core.nio.test;

import junit.framework.TestCase;
import org.jboss.xnio.spi.Provider;
import org.jboss.xnio.spi.TcpServerService;
import org.jboss.xnio.spi.Lifecycle;
import org.jboss.xnio.spi.TcpConnectorService;
import org.jboss.xnio.core.nio.NioProvider;
import org.jboss.xnio.channels.ConnectedStreamChannel;
import org.jboss.xnio.channels.ChannelOption;
import org.jboss.xnio.channels.TcpChannel;
import org.jboss.xnio.IoFuture;
import org.jboss.xnio.IoHandler;
import org.jboss.xnio.IoHandlerFactory;
import org.jboss.xnio.IoUtils;
import org.jboss.xnio.test.support.LoggingHelper;
import static org.jboss.xnio.Buffers.flip;
import static org.jboss.xnio.IoUtils.safeClose;
import static org.jboss.xnio.IoUtils.nullHandler;
import java.net.SocketAddress;
import java.net.InetSocketAddress;
import java.net.Inet4Address;
import java.nio.ByteBuffer;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

/**
 *
 */
public final class NioTcpTestCase extends TestCase {
    static {
        LoggingHelper.init();
    }

    private static final int SERVER_PORT = 12345;

    private static final void stop(Lifecycle lifecycle) {
        try {
            lifecycle.stop();
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }

    private void doConnectionTest(final Runnable body, final IoHandler<? super TcpChannel> clientHandler, final IoHandler<? super TcpChannel> serverHandler) throws Exception {
        synchronized (this) {
            final Provider provider = new NioProvider();
            provider.start();
            try {
                final TcpServerService tcpServerService = provider.createTcpServer();
                tcpServerService.setReuseAddress(true);
                tcpServerService.setBindAddresses(new SocketAddress[] { new InetSocketAddress(Inet4Address.getByAddress(new byte[] { 127, 0, 0, 1 }), SERVER_PORT)});
                tcpServerService.setHandlerFactory(new IoHandlerFactory<TcpChannel>() {
                    public IoHandler<? super TcpChannel> createHandler() {
                        return serverHandler;
                    }
                });
                tcpServerService.start();
                try {
                    final TcpConnectorService connectorService = provider.createTcpConnector();
                    connectorService.setConnectTimeout(10);
                    connectorService.start();
                    try {
                        final IoFuture<TcpChannel> ioFuture = connectorService.connectTo(new InetSocketAddress(Inet4Address.getByAddress(new byte[] { 127, 0, 0, 1 }), SERVER_PORT), clientHandler);
                        final TcpChannel channel = ioFuture.get();
                        try {
                            body.run();
                            channel.close();
                        } finally {
                            safeClose(channel);
                        }
                    } finally {
{'fraction_non_alphanumeric': 0.08303886925795052, 'fraction_numerical': 0.009187279151943463, 'mean_word_length': 2.733948988566403, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, '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': '1919922', 'n_tokens_mistral': 2327, 'n_tokens_neox': 2183, 'n_words': 637}