NamedConnectionPoolTest.java 5.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 *
 * Copyright 2013 Netflix, Inc.
 *
 * Licensed 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
 *
 * http://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 com.netflix.http4;

20
import static org.junit.Assert.*;
21 22 23 24 25 26 27
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.util.EntityUtils;
A
Allen Wang 已提交
28 29
import org.apache.log4j.Level;
import org.apache.log4j.LogManager;
30 31
import org.junit.Test;

32
import com.netflix.client.ClientFactory;
33
import com.netflix.client.config.CommonClientConfigKey;
34
import com.netflix.client.http.HttpRequest;
35
import com.netflix.config.ConfigurationManager;
36
import com.netflix.niws.client.http.RestClient;
37

38 39 40
public class NamedConnectionPoolTest {
    @Test
    public void testConnectionPoolCounters() throws Exception {
41
        // LogManager.getRootLogger().setLevel((Level)Level.DEBUG);
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
        NFHttpClient client = NFHttpClientFactory.getNamedNFHttpClient("google-NamedConnectionPoolTest");
        assertTrue(client.getConnectionManager() instanceof MonitoredConnectionManager);
        MonitoredConnectionManager connectionPoolManager = (MonitoredConnectionManager) client.getConnectionManager(); 
        connectionPoolManager.setDefaultMaxPerRoute(100);
        connectionPoolManager.setMaxTotal(200);
        assertTrue(connectionPoolManager.getConnectionPool() instanceof NamedConnectionPool);
        NamedConnectionPool connectionPool = (NamedConnectionPool) connectionPoolManager.getConnectionPool(); 
        System.out.println("Entries created: " + connectionPool.getCreatedEntryCount());
        System.out.println("Requests count: " + connectionPool.getRequestsCount());
        System.out.println("Free entries: " + connectionPool.getFreeEntryCount());
        System.out.println("Deleted :" + connectionPool.getDeleteCount());
        System.out.println("Released: " + connectionPool.getReleaseCount());
        for (int i = 0; i < 10; i++) {
            HttpUriRequest request = new HttpGet("http://www.google.com/");
            HttpResponse response = client.execute(request);
            EntityUtils.consume(response.getEntity());
            assertEquals(200, response.getStatusLine().getStatusCode());
            Thread.sleep(500);
        }
        System.out.println("Entries created: " + connectionPool.getCreatedEntryCount());
        System.out.println("Requests count: " + connectionPool.getRequestsCount());
        System.out.println("Free entries: " + connectionPool.getFreeEntryCount());
        System.out.println("Deleted :" + connectionPool.getDeleteCount());
        System.out.println("Released: " + connectionPool.getReleaseCount());
A
Allen Wang 已提交
66 67 68
        assertTrue(connectionPool.getCreatedEntryCount() >= 1);
        assertTrue(connectionPool.getRequestsCount() >= 10);
        assertTrue(connectionPool.getFreeEntryCount() >= 9);
69
        assertEquals(0, connectionPool.getDeleteCount());
A
Allen Wang 已提交
70 71
        assertEquals(connectionPool.getReleaseCount(), connectionPool.getRequestsCount());
        assertEquals(connectionPool.getRequestsCount(), connectionPool.getCreatedEntryCount() + connectionPool.getFreeEntryCount());
72 73 74 75
        ConfigurationManager.getConfigInstance().setProperty("google-NamedConnectionPoolTest.ribbon." + CommonClientConfigKey.MaxTotalHttpConnections.key(), "50");
        ConfigurationManager.getConfigInstance().setProperty("google-NamedConnectionPoolTest.ribbon." + CommonClientConfigKey.MaxHttpConnectionsPerHost.key(), "10");
        assertEquals(50, connectionPoolManager.getMaxTotal());
        assertEquals(10, connectionPoolManager.getDefaultMaxPerRoute());
76 77
    }

78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
    @Test
    public void testConnectionPoolCleaner() throws Exception {
        // LogManager.getRootLogger().setLevel((Level)Level.DEBUG);
        ConfigurationManager.getConfigInstance().setProperty("ConnectionPoolCleanerTest.ribbon." + CommonClientConfigKey.ConnIdleEvictTimeMilliSeconds, "100");
        ConfigurationManager.getConfigInstance().setProperty("ConnectionPoolCleanerTest.ribbon." + CommonClientConfigKey.ConnectionCleanerRepeatInterval, "500");
        RestClient client = (RestClient) ClientFactory.getNamedClient("ConnectionPoolCleanerTest");
        NFHttpClient httpclient = NFHttpClientFactory.getNamedNFHttpClient("ConnectionPoolCleanerTest");
        assertNotNull(httpclient);
        com.netflix.client.http.HttpResponse response = null;
        try {
            response = client.execute(HttpRequest.newBuilder().uri("http://www.google.com/").build());
        } finally {
            if (response != null) {
                response.close();
            }
        }
        MonitoredConnectionManager connectionPoolManager = (MonitoredConnectionManager) httpclient.getConnectionManager();
        Thread.sleep(2000);
        assertEquals(0, connectionPoolManager.getConnectionsInPool());
        client.shutdown();
    }
99
}