Search in sources :

Example 1 with HttpBackend

use of com.dtflys.forest.backend.HttpBackend 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)

Example 2 with HttpBackend

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

the class TestForestConfiguration method testBackend.

@Test
public void testBackend() {
    ForestConfiguration configuration = ForestConfiguration.createConfiguration();
    configuration.setBackendName("okhttp3");
    assertEquals("okhttp3", configuration.getBackend().getName());
    configuration.setBackend(null);
    configuration.setBackendName("httpclient");
    assertEquals("httpclient", configuration.getBackend().getName());
    HttpBackendSelector originSelector = new HttpBackendSelector();
    HttpBackendSelector selector = Mockito.spy(originSelector);
    configuration.setHttpBackendSelector(selector);
    Mockito.when(selector.findOkHttp3BackendInstance()).thenReturn(null);
    configuration.setBackendName(null);
    configuration.setBackend(null);
    Assert.assertEquals("httpclient", configuration.getBackend().getName());
    Mockito.when(selector.findHttpclientBackendInstance()).thenReturn(null);
    configuration.setBackendName(null);
    configuration.setBackend(null);
    boolean thrown = false;
    try {
        HttpBackend backend = configuration.getBackend();
        System.out.print(backend);
    } catch (ForestRuntimeException e) {
        thrown = true;
    }
    assertTrue(thrown);
}
Also used : HttpBackendSelector(com.dtflys.forest.backend.HttpBackendSelector) ForestConfiguration(com.dtflys.forest.config.ForestConfiguration) ForestRuntimeException(com.dtflys.forest.exceptions.ForestRuntimeException) HttpBackend(com.dtflys.forest.backend.HttpBackend) Test(org.junit.Test)

Example 3 with HttpBackend

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

the class ForestRequest method setBackend.

/**
 * 设置HTTP后端框架
 *
 * @param backendName Forest后端框架名称
 * @return {@link ForestRequest}对象实例
 */
public ForestRequest<T> setBackend(String backendName) {
    HttpBackend backend = configuration.getBackendSelector().select(backendName);
    if (backend != null) {
        backend.init(configuration);
        this.backend = backend;
    }
    return this;
}
Also used : HttpBackend(com.dtflys.forest.backend.HttpBackend)

Example 4 with HttpBackend

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

the class Test0 method testRetry.

@Test
public void testRetry() {
    ForestLogger logger = Mockito.mock(ForestLogger.class);
    config0.getLogHandler().setLogger(logger);
    try {
        beastshopClient.testRetry();
    } catch (ForestRuntimeException e) {
    }
    HttpBackend backend = config0.getBackend();
    String backendName = backend.getName();
    Mockito.verify(logger).info("[Forest] Request (" + backendName + "): \n" + "\t[Retry]: 1\n" + "\tGET https://www.thebeastshop.com/autopage/shops.htm HTTPS");
    Mockito.verify(logger).info("[Forest] Request (" + backendName + "): \n" + "\t[Retry]: 2\n" + "\tGET https://www.thebeastshop.com/autopage/shops.htm HTTPS");
    Mockito.verify(logger).info("[Forest] Request (" + backendName + "): \n" + "\t[Retry]: 3\n" + "\tGET https://www.thebeastshop.com/autopage/shops.htm HTTPS");
    Mockito.verify(logger).info("[Forest] Request (" + backendName + "): \n" + "\t[Retry]: 4\n" + "\tGET https://www.thebeastshop.com/autopage/shops.htm HTTPS");
    Mockito.verify(logger).info("[Forest] Request (" + backendName + "): \n" + "\t[Retry]: 5\n" + "\tGET https://www.thebeastshop.com/autopage/shops.htm HTTPS");
// Mockito.verify(logger).info("[Forest] [Network Error]: connect timed out");
}
Also used : ForestRuntimeException(com.dtflys.forest.exceptions.ForestRuntimeException) ForestLogger(com.dtflys.forest.logging.ForestLogger) HttpBackend(com.dtflys.forest.backend.HttpBackend) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

HttpBackend (com.dtflys.forest.backend.HttpBackend)4 Test (org.junit.Test)3 ForestRuntimeException (com.dtflys.forest.exceptions.ForestRuntimeException)2 ForestConnectionManager (com.dtflys.forest.backend.ForestConnectionManager)1 HttpBackendSelector (com.dtflys.forest.backend.HttpBackendSelector)1 OkHttp3ConnectionManager (com.dtflys.forest.backend.okhttp3.conn.OkHttp3ConnectionManager)1 ForestConfiguration (com.dtflys.forest.config.ForestConfiguration)1 ForestLogger (com.dtflys.forest.logging.ForestLogger)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 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)1