use of java.util.concurrent.TimeUnit in project rest.li by linkedin.
the class RestClientTest method testRestLiRemoteInvocationException.
@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "sendRequestOptions")
public void testRestLiRemoteInvocationException(SendRequestOption option, TimeoutOption timeoutOption, ProtocolVersionOption versionOption, ProtocolVersion protocolVersion, String errorResponseHeaderName) throws ExecutionException, TimeoutException, InterruptedException, RestLiDecodingException {
final int HTTP_CODE = 404;
final String ERR_MSG = "WHOOPS!";
RestClient client = mockClient(HTTP_CODE, ERR_MSG, protocolVersion);
Request<EmptyRecord> request = mockRequest(EmptyRecord.class, versionOption);
RequestBuilder<Request<EmptyRecord>> requestBuilder = mockRequestBuilder(request);
FutureCallback<Response<EmptyRecord>> callback = new FutureCallback<Response<EmptyRecord>>();
try {
sendRequest(option, client, request, requestBuilder, callback);
Long l = timeoutOption._l;
TimeUnit timeUnit = timeoutOption._timeUnit;
Response<EmptyRecord> response = l == null ? callback.get() : callback.get(l, timeUnit);
Assert.fail("Should have thrown");
} catch (ExecutionException e) {
Throwable cause = e.getCause();
Assert.assertTrue(cause instanceof RemoteInvocationException, "Expected RemoteInvocationException not " + cause.getClass().getName());
RemoteInvocationException rlre = (RemoteInvocationException) cause;
Assert.assertTrue(rlre.getMessage().startsWith("Received error " + HTTP_CODE + " from server"));
Throwable rlCause = rlre.getCause();
Assert.assertTrue(rlCause instanceof RestException, "Excepted RestException not " + rlCause.getClass().getName());
RestException rle = (RestException) rlCause;
Assert.assertEquals(ERR_MSG, rle.getResponse().getEntity().asString("UTF-8"));
Assert.assertEquals(HTTP_CODE, rle.getResponse().getStatus());
}
}
use of java.util.concurrent.TimeUnit in project rest.li by linkedin.
the class RestClientTest method getOkResponse.
private <T extends RecordTemplate> Response<T> getOkResponse(GetResponseOption option, ResponseFuture<T> future, TimeoutOption timeoutOption) throws ExecutionException, InterruptedException, TimeoutException, RemoteInvocationException {
Response<T> result = null;
T entity;
Long l = timeoutOption._l;
TimeUnit timeUnit = timeoutOption._timeUnit;
switch(option) {
case GET:
result = l == null ? future.get() : future.get(l, timeUnit);
break;
case GET_RESPONSE:
case GET_RESPONSE_EXPLICIT_NO_THROW:
case GET_RESPONSE_EXPLICIT_THROW:
result = l == null ? future.getResponse() : future.getResponse(l, timeUnit);
break;
case GET_RESPONSE_ENTITY:
case GET_RESPONSE_ENTITY_EXPLICIT_NO_THROW:
case GET_RESPONSE_ENTITY_EXPLICIT_THROW:
entity = l == null ? future.getResponseEntity() : future.getResponseEntity(l, timeUnit);
result = future.getResponse();
Assert.assertSame(entity, result.getEntity());
break;
default:
throw new IllegalStateException();
}
return result;
}
use of java.util.concurrent.TimeUnit in project rest.li by linkedin.
the class RestClientTest method getErrorResponse.
private <T extends RecordTemplate> RestLiResponseException getErrorResponse(GetResponseOption option, ResponseFuture<T> future, TimeoutOption timeoutOption) throws InterruptedException, TimeoutException, RemoteInvocationException {
Response<T> response = null;
T entity;
RestLiResponseException result = null;
Long l = timeoutOption._l;
TimeUnit timeUnit = timeoutOption._timeUnit;
switch(option) {
case GET:
try {
response = l == null ? future.get() : future.get(l, timeUnit);
Assert.fail("Should have thrown");
} catch (ExecutionException e) {
Throwable cause = e.getCause();
Assert.assertTrue(cause instanceof RestException, "Expected RestLiResponseException not " + cause.getClass().getName());
result = (RestLiResponseException) cause;
}
break;
case GET_RESPONSE:
case GET_RESPONSE_EXPLICIT_THROW:
try {
response = l == null ? future.getResponse() : future.getResponse(l, timeUnit);
Assert.fail("Should have thrown");
} catch (RestLiResponseException e) {
result = e;
}
break;
case GET_RESPONSE_EXPLICIT_NO_THROW:
response = l == null ? future.getResponse() : future.getResponse(l, timeUnit);
result = response.getError();
break;
case GET_RESPONSE_ENTITY:
case GET_RESPONSE_ENTITY_EXPLICIT_THROW:
try {
entity = l == null ? future.getResponseEntity() : future.getResponseEntity(l, timeUnit);
Assert.fail("Should have thrown");
} catch (RestLiResponseException e) {
result = e;
}
break;
case GET_RESPONSE_ENTITY_EXPLICIT_NO_THROW:
entity = l == null ? future.getResponseEntity() : future.getResponseEntity(l, timeUnit);
break;
default:
throw new IllegalStateException();
}
return result;
}
use of java.util.concurrent.TimeUnit in project rest.li by linkedin.
the class RestClientTest method testRestLiResponseExceptionCallback.
@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "sendRequestOptions")
public void testRestLiResponseExceptionCallback(SendRequestOption option, TimeoutOption timeoutOption, ProtocolVersionOption versionOption, ProtocolVersion protocolVersion, String errorResponseHeaderName) throws ExecutionException, TimeoutException, InterruptedException, RestLiDecodingException {
final String ERR_KEY = "someErr";
final String ERR_VALUE = "WHOOPS!";
final String ERR_MSG = "whoops2";
final int HTTP_CODE = 400;
final int APP_CODE = 666;
RestClient client = mockClient(ERR_KEY, ERR_VALUE, ERR_MSG, HTTP_CODE, APP_CODE, protocolVersion, errorResponseHeaderName);
Request<EmptyRecord> request = mockRequest(EmptyRecord.class, versionOption);
RequestBuilder<Request<EmptyRecord>> requestBuilder = mockRequestBuilder(request);
FutureCallback<Response<EmptyRecord>> callback = new FutureCallback<Response<EmptyRecord>>();
try {
sendRequest(option, client, request, requestBuilder, callback);
Long l = timeoutOption._l;
TimeUnit timeUnit = timeoutOption._timeUnit;
Response<EmptyRecord> response = l == null ? callback.get() : callback.get(l, timeUnit);
Assert.fail("Should have thrown");
} catch (ExecutionException e) {
// New
Throwable cause = e.getCause();
Assert.assertTrue(cause instanceof RestLiResponseException, "Expected RestLiResponseException not " + cause.getClass().getName());
RestLiResponseException rlre = (RestLiResponseException) cause;
Assert.assertEquals(HTTP_CODE, rlre.getStatus());
Assert.assertEquals(ERR_VALUE, rlre.getErrorDetails().get(ERR_KEY));
Assert.assertEquals(APP_CODE, rlre.getServiceErrorCode());
Assert.assertEquals(ERR_MSG, rlre.getServiceErrorMessage());
// Old
Assert.assertTrue(cause instanceof RestException, "Expected RestException not " + cause.getClass().getName());
RestException re = (RestException) cause;
RestResponse r = re.getResponse();
ErrorResponse er = new EntityResponseDecoder<ErrorResponse>(ErrorResponse.class).decodeResponse(r).getEntity();
Assert.assertEquals(HTTP_CODE, r.getStatus());
Assert.assertEquals(ERR_VALUE, er.getErrorDetails().data().getString(ERR_KEY));
Assert.assertEquals(APP_CODE, er.getServiceErrorCode().intValue());
Assert.assertEquals(ERR_MSG, er.getMessage());
}
}
use of java.util.concurrent.TimeUnit in project killbill by killbill.
the class TenantCacheInvalidation method start.
public void start() {
final TimeUnit pendingRateUnit = tenantConfig.getTenantBroadcastServiceRunningRate().getUnit();
final long pendingPeriod = tenantConfig.getTenantBroadcastServiceRunningRate().getPeriod();
tenantExecutor.scheduleAtFixedRate(new TenantCacheInvalidationRunnable(this, broadcastDao, tenantDao), pendingPeriod, pendingPeriod, pendingRateUnit);
}
Aggregations