@Test
    public void testReconnect() throws Exception {
        ServerConfig serverConfig1 = new ServerConfig()
            .setStopTimeout(0)
            .setPort(22221)
            .setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT)
            .setQueues(100).setCoreThreads(5).setMaxThreads(5);
        ProviderConfig<HelloService> providerConfig = new ProviderConfig<HelloService>()
            .setInterfaceId(HelloService.class.getName())
            .setRef(new HelloServiceImpl())
            .setServer(serverConfig1)
            .setRepeatedExportLimit(-1)
            .setRegister(false);
        providerConfig.export();

        final ConsumerConfig<HelloService> consumerConfig = new ConsumerConfig<HelloService>()
            .setInterfaceId(HelloService.class.getName())
            .setDirectUrl("bolt://127.0.0.1:22221")
            .setConnectionHolder("all")
            .setRegister(false)
            .setLazy(true)
            .setReconnectPeriod(2000)
            .setTimeout(3000);
        HelloService helloService = consumerConfig.refer();
        Assert.assertNotNull(helloService.sayHello("xxx", 11));

        // Mock server down, and RPC will throw exception(no available provider)
        providerConfig.unExport();
        ServerFactory.destroyAll();

        BoltClientTransport clientTransport = (BoltClientTransport) consumerConfig.getConsumerBootstrap().getCluster()
            .getConnectionHolder()
            .getAvailableClientTransport(ProviderHelper.toProviderInfo("bolt://127.0.0.1:22221"));

        clientTransport.disconnect();

        TestUtils.delayGet(new Callable<Boolean>() {
            @Override
            public Boolean call() throws Exception {
                return consumerConfig.getConsumerBootstrap().getCluster().getConnectionHolder().isAvailableEmpty();
            }
        }, true, 100, 30);

        try {
            helloService.sayHello("xxx", 11);
            Assert.fail();
        } catch (Exception e) {
            Assert.assertTrue(e.getMessage().contains(LogCodes.ERROR_TARGET_URL_INVALID));
        }

        // Mock server restart
        serverConfig1 = new ServerConfig()
            .setStopTimeout(0)
            .setPort(22221)
            .setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT)
            .setQueues(100).setCoreThreads(5).setMaxThreads(5);
        providerConfig.setServer(Arrays.asList(serverConfig1)).export();
        // The consumer will reconnect to provider automatically
        TestUtils.delayGet(new Callable<Boolean>() {
            @Override
            public Boolean call() throws Exception {
                return consumerConfig.getConsumerBootstrap().getCluster().getConnectionHolder().isAvailableEmpty();
            }
        }, false, 100, 30);
        // RPC return success
        Assert.assertNotNull(helloService.sayHello("xxx", 11));
    }
@author <a href="mailto:zhanggeng.zg@antfin.com">GengZhang</a>
test/test-integration/src/test/java/com/alipay/sofa/rpc/test/client/ReconnectTest.java