Home / File/ OioEventLoopTest.java — netty Source File

OioEventLoopTest.java — netty Source File

Architecture documentation for OioEventLoopTest.java, a java file in the netty codebase.

File java Buffer Search 1 classes

Entity Profile

Relationship Graph

Source Code

/*
 * Copyright 2013 The Netty Project
 *
 * The Netty Project licenses this file to you 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:
 *
 *   https://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 io.netty.channel.oio;

import io.netty.bootstrap.Bootstrap;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelException;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.socket.oio.OioServerSocketChannel;
import io.netty.channel.socket.oio.OioSocketChannel;
import io.netty.util.NetUtil;
import org.junit.jupiter.api.Test;

import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.concurrent.CountDownLatch;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;

public class OioEventLoopTest {
    @Test
    public void testTooManyServerChannels() throws Exception {
        EventLoopGroup g = new OioEventLoopGroup(1);
        ServerBootstrap b = new ServerBootstrap();
        b.channel(OioServerSocketChannel.class);
        b.group(g);
        b.childHandler(new ChannelInboundHandlerAdapter());
        ChannelFuture f1 = b.bind(0);
        f1.sync();

        ChannelFuture f2 = b.bind(0);
        f2.await();

        assertInstanceOf(ChannelException.class, f2.cause());
        assertThat(f2.cause().getMessage().toLowerCase()).contains("too many channels");

        final CountDownLatch notified = new CountDownLatch(1);
        f2.addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                notified.countDown();
            }
        });

        notified.await();
        g.shutdownGracefully();
    }

    @Test
    public void testTooManyClientChannels() throws Exception {
        EventLoopGroup g = new OioEventLoopGroup(1);
        ServerBootstrap sb = new ServerBootstrap();
        sb.channel(OioServerSocketChannel.class);
        sb.group(g);
        sb.childHandler(new ChannelInboundHandlerAdapter());
        ChannelFuture f1 = sb.bind(0);
        f1.sync();

        Bootstrap cb = new Bootstrap();
        cb.channel(OioSocketChannel.class);
        cb.group(g);
        cb.handler(new ChannelInboundHandlerAdapter());
        ChannelFuture f2 = cb.connect(NetUtil.LOCALHOST, ((InetSocketAddress) f1.channel().localAddress()).getPort());
        f2.await();

        assertInstanceOf(ChannelException.class, f2.cause());
        assertThat(f2.cause().getMessage().toLowerCase()).contains("too many channels");

        final CountDownLatch notified = new CountDownLatch(1);
        f2.addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                notified.countDown();
            }
        });

        notified.await();
        g.shutdownGracefully();
    }

    @Test
    public void testTooManyAcceptedChannels() throws Exception {
        EventLoopGroup g = new OioEventLoopGroup(1);
        ServerBootstrap sb = new ServerBootstrap();
        sb.channel(OioServerSocketChannel.class);
        sb.group(g);
        sb.childHandler(new ChannelInboundHandlerAdapter());
        ChannelFuture f1 = sb.bind(0);
        f1.sync();

        Socket s = new Socket(NetUtil.LOCALHOST, ((InetSocketAddress) f1.channel().localAddress()).getPort());
        assertEquals(-1, s.getInputStream().read());
        s.close();

        g.shutdownGracefully();
    }
}

Domain

Subdomains

Frequently Asked Questions

What does OioEventLoopTest.java do?
OioEventLoopTest.java is a source file in the netty codebase, written in java. It belongs to the Buffer domain, Search subdomain.
Where is OioEventLoopTest.java in the architecture?
OioEventLoopTest.java is located at transport/src/test/java/io/netty/channel/oio/OioEventLoopTest.java (domain: Buffer, subdomain: Search, directory: transport/src/test/java/io/netty/channel/oio).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free