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();
}
}
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();
}
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();
}
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();
}
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();
}
Aggregations