com.google.api.client.testing.util.MockSleeper

Here are the examples of the java api class com.google.api.client.testing.util.MockSleeper taken from open source projects.

1. RetryHttpInitializerWrapperTests#testSimpleRetry()

View license
public void testSimpleRetry() throws Exception {
    FailThenSuccessBackoffTransport fakeTransport = new FailThenSuccessBackoffTransport(HttpStatusCodes.STATUS_CODE_SERVER_ERROR, 3);
    MockGoogleCredential credential = RetryHttpInitializerWrapper.newMockCredentialBuilder().build();
    MockSleeper mockSleeper = new MockSleeper();
    RetryHttpInitializerWrapper retryHttpInitializerWrapper = new RetryHttpInitializerWrapper(credential, mockSleeper, TimeValue.timeValueSeconds(5));
    Compute client = new Compute.Builder(fakeTransport, new JacksonFactory(), null).setHttpRequestInitializer(retryHttpInitializerWrapper).setApplicationName("test").build();
    HttpRequest request = client.getRequestFactory().buildRequest("Get", new GenericUrl("http://elasticsearch.com"), null);
    HttpResponse response = request.execute();
    assertThat(mockSleeper.getCount(), equalTo(3));
    assertThat(response.getStatusCode(), equalTo(200));
}

2. RetryHttpInitializerWrapperTests#testIOExceptionRetry()

View license
public void testIOExceptionRetry() throws Exception {
    FailThenSuccessBackoffTransport fakeTransport = new FailThenSuccessBackoffTransport(HttpStatusCodes.STATUS_CODE_SERVER_ERROR, 1, true);
    MockGoogleCredential credential = RetryHttpInitializerWrapper.newMockCredentialBuilder().build();
    MockSleeper mockSleeper = new MockSleeper();
    RetryHttpInitializerWrapper retryHttpInitializerWrapper = new RetryHttpInitializerWrapper(credential, mockSleeper, TimeValue.timeValueMillis(500));
    Compute client = new Compute.Builder(fakeTransport, new JacksonFactory(), null).setHttpRequestInitializer(retryHttpInitializerWrapper).setApplicationName("test").build();
    HttpRequest request = client.getRequestFactory().buildRequest("Get", new GenericUrl("http://elasticsearch.com"), null);
    HttpResponse response = request.execute();
    assertThat(mockSleeper.getCount(), equalTo(1));
    assertThat(response.getStatusCode(), equalTo(200));
}

3. HttpBackOffIOExpcetionHandlerTest#subsetHandle()

View license
public void subsetHandle(long count, long millis, boolean retrySupported, BackOff backOff) throws IOException {
    // create the handler
    MockSleeper sleeper = new MockSleeper();
    HttpBackOffIOExceptionHandler handler = new HttpBackOffIOExceptionHandler(backOff).setSleeper(sleeper);
    while (handler.handleIOException(null, retrySupported)) {
        assertEquals(millis, sleeper.getLastMillis());
    }
    assertEquals(count, sleeper.getCount());
}

4. HttpBackOffUnsuccessfulResponseHandlerTest#subsetHandleResponse()

View license
private void subsetHandleResponse(int count, int millis, boolean retry, BackOff backOff, BackOffRequired backOffRequired) throws IOException {
    // create the handler
    MockSleeper sleeper = new MockSleeper();
    HttpBackOffUnsuccessfulResponseHandler handler = new HttpBackOffUnsuccessfulResponseHandler(backOff).setSleeper(sleeper).setBackOffRequired(backOffRequired);
    while (handler.handleResponse(null, null, retry)) {
        assertEquals(millis, sleeper.getLastMillis());
    }
    assertEquals(count, sleeper.getCount());
}

5. BackOffUtilsTest#subtestNext()

View license
public void subtestNext(long millis) throws Exception {
    MockSleeper sleeper = new MockSleeper();
    MockBackOff backOff = new MockBackOff().setBackOffMillis(millis);
    if (millis == BackOff.STOP) {
        backOff.setMaxTries(0);
    }
    int retries = 0;
    while (retries <= backOff.getMaxTries() + 1) {
        boolean next = BackOffUtils.next(sleeper, backOff);
        assertEquals(retries < backOff.getMaxTries(), next);
        if (next) {
            retries++;
        }
        assertEquals(retries, sleeper.getCount());
        assertEquals(retries, backOff.getNumberOfTries());
        if (!next) {
            break;
        }
        assertEquals(millis, sleeper.getLastMillis());
    }
}

6. RetryHttpInitializerWrapperTests#testRetryWaitTooLong()

View license
public void testRetryWaitTooLong() throws Exception {
    TimeValue maxWaitTime = TimeValue.timeValueMillis(10);
    int maxRetryTimes = 50;
    FailThenSuccessBackoffTransport fakeTransport = new FailThenSuccessBackoffTransport(HttpStatusCodes.STATUS_CODE_SERVER_ERROR, maxRetryTimes);
    JsonFactory jsonFactory = new JacksonFactory();
    MockGoogleCredential credential = RetryHttpInitializerWrapper.newMockCredentialBuilder().build();
    MockSleeper oneTimeSleeper = new MockSleeper() {

        @Override
        public void sleep(long millis) throws InterruptedException {
            Thread.sleep(maxWaitTime.getMillis());
            // important number, use this to get count
            super.sleep(0);
        }
    };
    RetryHttpInitializerWrapper retryHttpInitializerWrapper = new RetryHttpInitializerWrapper(credential, oneTimeSleeper, maxWaitTime);
    Compute client = new Compute.Builder(fakeTransport, jsonFactory, null).setHttpRequestInitializer(retryHttpInitializerWrapper).setApplicationName("test").build();
    HttpRequest request1 = client.getRequestFactory().buildRequest("Get", new GenericUrl("http://elasticsearch.com"), null);
    try {
        request1.execute();
        fail("Request should fail if wait too long");
    } catch (HttpResponseException e) {
        assertThat(e.getStatusCode(), equalTo(HttpStatusCodes.STATUS_CODE_SERVER_ERROR));
        assertThat(oneTimeSleeper.getCount(), lessThan(maxRetryTimes));
    }
}