Search in sources :

Example 41 with TimeUnit

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());
    }
}
Also used : EmptyRecord(com.linkedin.restli.common.EmptyRecord) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestException(com.linkedin.r2.message.rest.RestException) RestResponse(com.linkedin.r2.message.rest.RestResponse) ErrorResponse(com.linkedin.restli.common.ErrorResponse) TimeUnit(java.util.concurrent.TimeUnit) RemoteInvocationException(com.linkedin.r2.RemoteInvocationException) ExecutionException(java.util.concurrent.ExecutionException) FutureCallback(com.linkedin.common.callback.FutureCallback) Test(org.testng.annotations.Test)

Example 42 with TimeUnit

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;
}
Also used : TimeUnit(java.util.concurrent.TimeUnit)

Example 43 with TimeUnit

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;
}
Also used : RestException(com.linkedin.r2.message.rest.RestException) TimeUnit(java.util.concurrent.TimeUnit) ExecutionException(java.util.concurrent.ExecutionException)

Example 44 with TimeUnit

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());
    }
}
Also used : EmptyRecord(com.linkedin.restli.common.EmptyRecord) RestResponse(com.linkedin.r2.message.rest.RestResponse) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestException(com.linkedin.r2.message.rest.RestException) ErrorResponse(com.linkedin.restli.common.ErrorResponse) RestResponse(com.linkedin.r2.message.rest.RestResponse) ErrorResponse(com.linkedin.restli.common.ErrorResponse) TimeUnit(java.util.concurrent.TimeUnit) ExecutionException(java.util.concurrent.ExecutionException) FutureCallback(com.linkedin.common.callback.FutureCallback) Test(org.testng.annotations.Test)

Example 45 with TimeUnit

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);
}
Also used : TimeUnit(java.util.concurrent.TimeUnit)

Aggregations

TimeUnit (java.util.concurrent.TimeUnit)190 Test (org.junit.Test)28 ExecutionException (java.util.concurrent.ExecutionException)16 IOException (java.io.IOException)11 TimeoutException (java.util.concurrent.TimeoutException)11 Future (java.util.concurrent.Future)10 HashMap (java.util.HashMap)7 TimeSpec (com.linkedin.thirdeye.api.TimeSpec)6 ArrayList (java.util.ArrayList)6 TimeValue (org.elasticsearch.common.unit.TimeValue)6 DataType (com.linkedin.pinot.common.data.FieldSpec.DataType)5 File (java.io.File)5 HashSet (java.util.HashSet)5 Matcher (java.util.regex.Matcher)5 WaitUntilGatewaySenderFlushedCoordinatorJUnitTest (org.apache.geode.internal.cache.wan.WaitUntilGatewaySenderFlushedCoordinatorJUnitTest)5 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)5 TimeGranularity (com.linkedin.thirdeye.api.TimeGranularity)4 GwtIncompatible (com.google.common.annotations.GwtIncompatible)3 RestException (com.linkedin.r2.message.rest.RestException)3 Map (java.util.Map)3