Search in sources :

Example 1 with ForestConnectionManager

use of com.dtflys.forest.backend.ForestConnectionManager in project forest by dromara.

the class TestPoolClient method testPool.

@Test
public void testPool() throws InterruptedException {
    int count = 100;
    for (int i = 0; i < count; i++) {
        server.enqueue(new MockResponse().setBody(EXPECTED).setHeadersDelay(1, TimeUnit.SECONDS));
    }
    HttpBackend backend = configuration.getBackend();
    ForestConnectionManager connectionManager = backend.getConnectionManager();
    ExecutorService executorService = Executors.newFixedThreadPool(count);
    CountDownLatch latch = new CountDownLatch(count);
    for (int i = 0; i < count; i++) {
        executorService.execute(() -> {
            poolClient.send();
            if (connectionManager instanceof OkHttp3ConnectionManager) {
                ConnectionPool pool = ((OkHttp3ConnectionManager) connectionManager).getOkHttpPool();
                System.out.println("connect count = " + pool.connectionCount());
            }
            latch.countDown();
        });
    }
    latch.await();
}
Also used : ConnectionPool(okhttp3.ConnectionPool) MockResponse(okhttp3.mockwebserver.MockResponse) ForestConnectionManager(com.dtflys.forest.backend.ForestConnectionManager) ExecutorService(java.util.concurrent.ExecutorService) HttpBackend(com.dtflys.forest.backend.HttpBackend) CountDownLatch(java.util.concurrent.CountDownLatch) OkHttp3ConnectionManager(com.dtflys.forest.backend.okhttp3.conn.OkHttp3ConnectionManager) Test(org.junit.Test) BaseClientTest(com.dtflys.test.http.BaseClientTest)

Aggregations

ForestConnectionManager (com.dtflys.forest.backend.ForestConnectionManager)1 HttpBackend (com.dtflys.forest.backend.HttpBackend)1 OkHttp3ConnectionManager (com.dtflys.forest.backend.okhttp3.conn.OkHttp3ConnectionManager)1 BaseClientTest (com.dtflys.test.http.BaseClientTest)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ExecutorService (java.util.concurrent.ExecutorService)1 ConnectionPool (okhttp3.ConnectionPool)1 MockResponse (okhttp3.mockwebserver.MockResponse)1 Test (org.junit.Test)1