Search in sources :

Example 26 with HttpClientProxy

use of com.tvd12.ezyhttp.client.HttpClientProxy in project ezyhttp by youngmonkeys.

the class HttpClientProxy method close.

@Override
public void close() {
    this.active = false;
    this.requestQueue.clear();
    Map<Request, EzyFuture> undoneTasks = futures.clear();
    for (Request undoneRequest : undoneTasks.keySet()) {
        EzyFuture undoneTask = undoneTasks.get(undoneRequest);
        undoneTask.cancel("HttpClientProxy close, request to: " + undoneRequest.getURL() + " has cancelled");
    }
    if (executorService != null) {
        this.executorService.shutdownNow();
    }
}
Also used : Request(com.tvd12.ezyhttp.client.request.Request) DownloadRequest(com.tvd12.ezyhttp.client.request.DownloadRequest) EzyFuture(com.tvd12.ezyfox.concurrent.EzyFuture)

Example 27 with HttpClientProxy

use of com.tvd12.ezyhttp.client.HttpClientProxy in project ezyhttp by youngmonkeys.

the class HttpClientProxyTest method deleteJsonTest.

@Test
public void deleteJsonTest() throws Exception {
    // given
    HttpClientProxy sut = newClientProxy();
    DeleteRequest request = new DeleteRequest().setConnectTimeout(15000).setEntity(new TestRequest("Monkey")).setResponseType(TestResponse.class).setResponseType(StatusCodes.OK, TestResponse.class).setURL("http://127.0.0.1:18081/greet").setURL(new URL("http://127.0.0.1:18081/greet")).setURL(URI.create("http://127.0.0.1:18081/greet"));
    // when
    Throwable e = Asserts.assertThrows(() -> sut.call(request, 15000));
    // then
    Asserts.assertEqualsType(e, IllegalArgumentException.class);
    sut.close();
    sut.stop();
}
Also used : HttpClientProxy(com.tvd12.ezyhttp.client.HttpClientProxy) URL(java.net.URL) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest) BaseTest(com.tvd12.test.base.BaseTest)

Example 28 with HttpClientProxy

use of com.tvd12.ezyhttp.client.HttpClientProxy in project ezyhttp by youngmonkeys.

the class HttpClientProxyTest method handleRequestsNoFutureWhenException.

@Test
public void handleRequestsNoFutureWhenException() throws Exception {
    // given
    HttpClientProxy sut = newClientProxy();
    RequestQueue queue = FieldUtil.getFieldValue(sut, "requestQueue");
    Request request = mock(Request.class);
    // when
    queue.add(request);
    Thread.sleep(100);
    // then
    sut.close();
}
Also used : HelloRequest(com.tvd12.ezyhttp.client.test.request.HelloRequest) HttpClientProxy(com.tvd12.ezyhttp.client.HttpClientProxy) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest) BaseTest(com.tvd12.test.base.BaseTest)

Example 29 with HttpClientProxy

use of com.tvd12.ezyhttp.client.HttpClientProxy in project ezyhttp by youngmonkeys.

the class HttpClientProxyTest method handleRequestsNoFutureWhenResponse.

@Test
public void handleRequestsNoFutureWhenResponse() throws Exception {
    // given
    HttpClientProxy sut = newClientProxy();
    RequestQueue queue = FieldUtil.getFieldValue(sut, "requestQueue");
    GetRequest request = new GetRequest().setConnectTimeout(15000).setResponseType(TestResponse.class).setResponseType(StatusCodes.OK, TestResponse.class).setURL("http://127.0.0.1:18081/greet?who=Monkey");
    // when
    queue.add(request);
    Thread.sleep(100);
    // then
    sut.close();
}
Also used : HttpClientProxy(com.tvd12.ezyhttp.client.HttpClientProxy) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest) BaseTest(com.tvd12.test.base.BaseTest)

Example 30 with HttpClientProxy

use of com.tvd12.ezyhttp.client.HttpClientProxy in project ezyhttp by youngmonkeys.

the class HttpClientProxyTest method maxCapacity.

@Test
public void maxCapacity() {
    // given
    HttpClientProxy sut = HttpClientProxy.builder().autoStart(true).requestQueueCapacity(1).threadPoolSize(1).build();
    PostRequest request = new PostRequest().setConnectTimeout(15000).setEntity(String.class).setResponseType(TestResponse.class).setResponseType(StatusCodes.OK, TestResponse.class).setURL("http://127.0.0.1:18081/greet");
    // when
    AtomicReference<Throwable> ref = new AtomicReference<>();
    Thread[] threads = new Thread[4];
    for (int i = 0; i < threads.length; ++i) {
        threads[i] = new Thread(() -> {
            while (ref.get() == null) {
                try {
                    sut.call(request, 150000);
                } catch (Exception e) {
                    ref.set(e);
                }
            }
        });
    }
    for (Thread thread : threads) {
        thread.start();
    }
    while (ref.get() == null) {
        EzyThreads.sleep(3);
    }
    // then
    Asserts.assertThat(ref.get()).isEqualsType(RequestQueueFullException.class);
    sut.close();
    sut.stop();
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) HttpClientProxy(com.tvd12.ezyhttp.client.HttpClientProxy) DownloadCancelledException(com.tvd12.ezyhttp.client.exception.DownloadCancelledException) RequestQueueFullException(com.tvd12.ezyhttp.client.exception.RequestQueueFullException) UnknownHostException(java.net.UnknownHostException) BadRequestException(com.tvd12.ezyfox.exception.BadRequestException) ClientNotActiveException(com.tvd12.ezyhttp.client.exception.ClientNotActiveException) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest) BaseTest(com.tvd12.test.base.BaseTest)

Aggregations

HttpClientProxy (com.tvd12.ezyhttp.client.HttpClientProxy)31 BaseTest (com.tvd12.test.base.BaseTest)27 BeforeTest (org.testng.annotations.BeforeTest)27 Test (org.testng.annotations.Test)27 File (java.io.File)11 BadRequestException (com.tvd12.ezyfox.exception.BadRequestException)8 ClientNotActiveException (com.tvd12.ezyhttp.client.exception.ClientNotActiveException)8 DownloadCancelledException (com.tvd12.ezyhttp.client.exception.DownloadCancelledException)8 RequestQueueFullException (com.tvd12.ezyhttp.client.exception.RequestQueueFullException)8 UnknownHostException (java.net.UnknownHostException)8 DownloadCancellationToken (com.tvd12.ezyhttp.client.concurrent.DownloadCancellationToken)6 EzyWrap (com.tvd12.ezyfox.util.EzyWrap)5 FileOutputStream (java.io.FileOutputStream)5 OutputStream (java.io.OutputStream)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 URL (java.net.URL)4 HelloRequest (com.tvd12.ezyhttp.client.test.request.HelloRequest)3 ResponseEntity (com.tvd12.ezyhttp.core.response.ResponseEntity)3 DownloadRequest (com.tvd12.ezyhttp.client.request.DownloadRequest)2 Request (com.tvd12.ezyhttp.client.request.Request)2