Search in sources :

Example 11 with RestClient

use of com.baidu.hugegraph.rest.RestClient in project hugegraph-common by hugegraph.

the class RestClientTest method testPutWithHeaders.

@Test
public void testPutWithHeaders() {
    RestClient client = new RestClientImpl("/test", 1000, 200);
    MultivaluedMap<String, Object> headers = new MultivaluedHashMap<>();
    headers.add("key1", "value1-1");
    headers.add("key1", "value1-2");
    headers.add("Content-Encoding", "gzip");
    RestResult restResult = client.put("path", "id1", "body", headers);
    Assert.assertEquals(200, restResult.status());
}
Also used : MultivaluedHashMap(jakarta.ws.rs.core.MultivaluedHashMap) RestResult(com.baidu.hugegraph.rest.RestResult) AbstractRestClient(com.baidu.hugegraph.rest.AbstractRestClient) RestClient(com.baidu.hugegraph.rest.RestClient) Test(org.junit.Test)

Example 12 with RestClient

use of com.baidu.hugegraph.rest.RestClient in project hugegraph-common by hugegraph.

the class RestClientTest method testGetWithException.

@Test
public void testGetWithException() {
    RestClient client = new RestClientImpl("/test", 1000, 400);
    Assert.assertThrows(ClientException.class, () -> {
        client.get("path", "id1");
    });
}
Also used : AbstractRestClient(com.baidu.hugegraph.rest.AbstractRestClient) RestClient(com.baidu.hugegraph.rest.RestClient) Test(org.junit.Test)

Example 13 with RestClient

use of com.baidu.hugegraph.rest.RestClient in project hugegraph-common by hugegraph.

the class RestClientTest method testDeleteWithId.

@Test
public void testDeleteWithId() {
    RestClient client = new RestClientImpl("/test", 1000, 204);
    RestResult restResult = client.delete("path", "id1");
    Assert.assertEquals(204, restResult.status());
}
Also used : RestResult(com.baidu.hugegraph.rest.RestResult) AbstractRestClient(com.baidu.hugegraph.rest.AbstractRestClient) RestClient(com.baidu.hugegraph.rest.RestClient) Test(org.junit.Test)

Example 14 with RestClient

use of com.baidu.hugegraph.rest.RestClient in project hugegraph-common by hugegraph.

the class RestClientTest method testPostWithTokenAndAllParams.

@Test
public void testPostWithTokenAndAllParams() {
    RestClient client = new RestClientImpl("/test", "token", 1000, 10, 5, 200);
    RestResult restResult = client.post("path", "body");
    Assert.assertEquals(200, restResult.status());
}
Also used : RestResult(com.baidu.hugegraph.rest.RestResult) AbstractRestClient(com.baidu.hugegraph.rest.AbstractRestClient) RestClient(com.baidu.hugegraph.rest.RestClient) Test(org.junit.Test)

Example 15 with RestClient

use of com.baidu.hugegraph.rest.RestClient in project hugegraph-common by hugegraph.

the class RestClientTest method testCleanExecutor.

@Test
public void testCleanExecutor() throws Exception {
    // Modify IDLE_TIME 100ms to speed test
    int newIdleTime = 100;
    int newCheckPeriod = newIdleTime + 20;
    RestClient client = new RestClientImpl("/test", 1000, newIdleTime, 10, 5, 200);
    PoolingHttpClientConnectionManager pool;
    pool = Whitebox.getInternalState(client, "pool");
    pool = Mockito.spy(pool);
    Whitebox.setInternalState(client, "pool", pool);
    HttpRoute route = new HttpRoute(HttpHost.create("http://127.0.0.1:8080"));
    // Create a connection manually, it will be put into leased list
    HttpClientConnection conn = pool.requestConnection(route, null).get(1L, TimeUnit.SECONDS);
    PoolStats stats = pool.getTotalStats();
    int usingConns = stats.getLeased() + stats.getPending();
    Assert.assertGte(1, usingConns);
    // Sleep more than two check periods for busy connection
    Thread.sleep(newCheckPeriod);
    Mockito.verify(pool, Mockito.never()).closeExpiredConnections();
    stats = pool.getTotalStats();
    usingConns = stats.getLeased() + stats.getPending();
    Assert.assertGte(1, usingConns);
    // The connection will be put into available list
    pool.releaseConnection(conn, null, 0, TimeUnit.SECONDS);
    stats = pool.getTotalStats();
    usingConns = stats.getLeased() + stats.getPending();
    Assert.assertEquals(0, usingConns);
    /*
         * Sleep more than two check periods for free connection,
         * ensure connection has been closed
         */
    Thread.sleep(newCheckPeriod);
    Mockito.verify(pool, Mockito.atLeastOnce()).closeExpiredConnections();
    Mockito.verify(pool, Mockito.atLeastOnce()).closeIdleConnections(newIdleTime, TimeUnit.MILLISECONDS);
}
Also used : HttpRoute(org.apache.http.conn.routing.HttpRoute) HttpClientConnection(org.apache.http.HttpClientConnection) AbstractRestClient(com.baidu.hugegraph.rest.AbstractRestClient) RestClient(com.baidu.hugegraph.rest.RestClient) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) PoolStats(org.apache.http.pool.PoolStats) Test(org.junit.Test)

Aggregations

AbstractRestClient (com.baidu.hugegraph.rest.AbstractRestClient)24 RestClient (com.baidu.hugegraph.rest.RestClient)24 Test (org.junit.Test)24 RestResult (com.baidu.hugegraph.rest.RestResult)19 MultivaluedHashMap (jakarta.ws.rs.core.MultivaluedHashMap)3 PoolingHttpClientConnectionManager (org.apache.http.impl.conn.PoolingHttpClientConnectionManager)2 HashMap (java.util.HashMap)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 HttpClientConnection (org.apache.http.HttpClientConnection)1 HttpRoute (org.apache.http.conn.routing.HttpRoute)1 PoolStats (org.apache.http.pool.PoolStats)1