/*
 * Copyright (c) 2015 Uber Technologies, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package com.uber.tchannel.api;

import com.uber.tchannel.api.TChannel.Builder;
import io.netty.util.concurrent.MultithreadEventExecutorGroup;
import org.junit.Test;

import java.net.InetAddress;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class TChannelBuilderTest {

    @Test
    public void testSetServerHost() throws Exception {

        TChannel tchannel = new TChannel.Builder("some-service")
                .setServerHost(InetAddress.getLoopbackAddress())
                .build();
        tchannel.listen();
        assertEquals("localhost", tchannel.getHost().getCanonicalHostName());
        tchannel.shutdown();

    }

    @Test
    public void testServerListeningHostValidity() throws Exception {

        // InetAddress constructed using hostname will not return IP address
        // with getHostName call
        TChannel tchannel = new TChannel.Builder("some-service")
            .setServerHost(InetAddress.getByName("localhost"))
            .build();
        tchannel.listen();

        // The regular expression used here doesn't cover all invalid cases, but it's
        // consistent with the one in javascript code
        assertTrue((tchannel.getListeningHost().matches("^\\d+\\.\\d+\\.\\d+\\.\\d+$")));
    }

    @Test
    public void testGroupsThreadCounts() throws Exception {

        Builder builder = new Builder("some-service")
            .setBossGroupThreads(3)
            .setChildGroupThreads(5);

        builder.build();

        assertEquals(3, ((MultithreadEventExecutorGroup) builder.getBossGroup()).executorCount());
        assertEquals(5, ((MultithreadEventExecutorGroup) builder.getChildGroup()).executorCount());
    }
}

@@ -1201,16 +1201,63 @@
 uilder;%0A
+import io.netty.channel.nio.NioEventLoopGroup;%0A
 import i
@@ -1410,16 +1410,59 @@
 Equals;%0A
+import static org.junit.Assert.assertSame;%0A
 import s
@@ -2932,11 +2932,586 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void testGroupsThreadCountsIgnoredForExplicitGroups() throws Exception %7B%0A%0A        NioEventLoopGroup bossGroup = new NioEventLoopGroup(1);%0A        NioEventLoopGroup childGroup = new NioEventLoopGroup(1);%0A%0A        Builder builder = new Builder(%22some-service%22)%0A            .setBossGroup(bossGroup)%0A            .setChildGroup(childGroup)%0A            .setBossGroupThreads(3)%0A            .setChildGroupThreads(5);%0A%0A        builder.build();%0A%0A        assertSame(bossGroup, builder.getBossGroup());%0A        assertSame(childGroup, builder.getChildGroup());%0A    %7D
 %0A%7D%0A

Add test