package com.wealoha.thrift;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.transport.TFramedTransport;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.wealoha.thrift.service.TestThriftService;
import com.wealoha.thrift.service.TestThriftService.Client;
import com.wealoha.thrift.service.TestThriftService.Iface;

/**
 * 
 * @author javamonk
 * @createTime 2014年7月4日 下午4:51:18
 */
public class TestThriftClientPool {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Test
    public void testEcho() throws InterruptedException {
        List<ServiceInfo> serverList = Arrays.asList( //
                new ServiceInfo("127.0.0.1", 9092), //
                new ServiceInfo("127.0.0.1", 9091), //
                new ServiceInfo("127.0.0.1", 9090));

        PoolConfig config = new PoolConfig();
        config.setFailover(true);
        config.setTimeout(1000);
        //        config.setMaxTotal(10);
        //        config.setBlockWhenExhausted(true);
        ThriftClientPool<TestThriftService.Client> pool = new ThriftClientPool<>(serverList,
                e -> new Client(new TBinaryProtocol(new TFramedTransport(e))), config);
        // pool.setServices(serverList);

        ExecutorService executorService = Executors.newFixedThreadPool(10);

        for (int i = 0; i < 100; i++) {
            int counter = i;
            executorService.submit(new Runnable() {

                @Override
                public void run() {
                    try {
                        try (ThriftClient<Client> client = pool.getClient()) {
                            Iface iFace = client.iFace();
                            String response = iFace.echo("Hello " + counter + "!");
                            logger.info("get response: {}", response);
                            client.finish();
                        }
                    } catch (Throwable e) {
                        logger.error("get client fail", e);
                    }
                }
            });

            executorService.submit(new Runnable() {

                @Override
                public void run() {
                    try {
                        Iface iFace = pool.iface();
                        String response = iFace.echo("Hello " + counter + "!");
                        logger.info("get response: {}", response);
                    } catch (Throwable e) {
                        logger.error("get client fail", e);
                    }
                }
            });
        }

        executorService.shutdown();
        executorService.awaitTermination(1, TimeUnit.MINUTES);
        TimeUnit.MINUTES.sleep(1);
    }
}

@@ -273,34 +273,274 @@
 ift.
-transport.TFramedTransport
+server.TThreadPoolServer;%0Aimport org.apache.thrift.server.TThreadPoolServer.Args;%0Aimport org.apache.thrift.transport.TFramedTransport;%0Aimport org.apache.thrift.transport.TServerSocket;%0Aimport org.apache.thrift.transport.TServerTransport;%0Aimport org.junit.BeforeClass
 ;%0Aim
@@ -789,16 +789,139 @@
 e.Iface;
+%0Aimport com.wealoha.thrift.service.TestThriftService.Processor;%0Aimport com.wealoha.thrift.service.TestThriftServiceHandler;
 %0A%0A/**%0A *
@@ -1030,16 +1030,23 @@
 private 
+static 
 Logger l
@@ -1081,20 +1081,952 @@
 ger(
-getClass());
+TestThriftClientPool.class);%0A%0A    @BeforeClass%0A    public static void setUp() %7B%0A        int port = 9090;%0A%0A        try %7B%0A            TServerTransport serverTransport = new TServerSocket(port);%0A%0A            Args processor = new TThreadPoolServer.Args(serverTransport)%0A                    .inputTransportFactory(new TFramedTransport.Factory())%0A                    .outputTransportFactory(new TFramedTransport.Factory())%0A                    .processor(new Processor%3C%3E(new TestThriftServiceHandler()));%0A            //            processor.maxWorkerThreads = 20;%0A            TThreadPoolServer server = new TThreadPoolServer(processor);%0A%0A            logger.info(%22Starting test server...%22);%0A            new Thread(new Runnable() %7B%0A%0A                @Override%0A                public void run() %7B%0A                    server.serve();%0A                %7D%0A            %7D).start();%0A        %7D catch (Exception e) %7B%0A            e.printStackTrace();%0A        %7D%0A    %7D
 %0A%0A  
@@ -4168,43 +4168,8 @@
 S);%0A
-        TimeUnit.MINUTES.sleep(1);%0A
     

fix testing from commandline