Search in sources :

Example 1 with MockGoogleCredential

use of com.google.api.client.googleapis.testing.auth.oauth2.MockGoogleCredential in project elasticsearch by elastic.

the class RetryHttpInitializerWrapperTests method testIOExceptionRetry.

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));
}
Also used : LowLevelHttpRequest(com.google.api.client.http.LowLevelHttpRequest) HttpRequest(com.google.api.client.http.HttpRequest) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest) Compute(com.google.api.services.compute.Compute) MockGoogleCredential(com.google.api.client.googleapis.testing.auth.oauth2.MockGoogleCredential) MockLowLevelHttpResponse(com.google.api.client.testing.http.MockLowLevelHttpResponse) HttpResponse(com.google.api.client.http.HttpResponse) LowLevelHttpResponse(com.google.api.client.http.LowLevelHttpResponse) GenericUrl(com.google.api.client.http.GenericUrl) JacksonFactory(com.google.api.client.json.jackson2.JacksonFactory) MockSleeper(com.google.api.client.testing.util.MockSleeper)

Example 2 with MockGoogleCredential

use of com.google.api.client.googleapis.testing.auth.oauth2.MockGoogleCredential in project elasticsearch by elastic.

the class RetryHttpInitializerWrapperTests method testRetryWaitTooLong.

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));
        // should only retry once.
        assertThat(oneTimeSleeper.getCount(), lessThan(maxRetryTimes));
    }
}
Also used : LowLevelHttpRequest(com.google.api.client.http.LowLevelHttpRequest) HttpRequest(com.google.api.client.http.HttpRequest) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest) JsonFactory(com.google.api.client.json.JsonFactory) MockGoogleCredential(com.google.api.client.googleapis.testing.auth.oauth2.MockGoogleCredential) HttpResponseException(com.google.api.client.http.HttpResponseException) GenericUrl(com.google.api.client.http.GenericUrl) JacksonFactory(com.google.api.client.json.jackson2.JacksonFactory) Compute(com.google.api.services.compute.Compute) TimeValue(org.elasticsearch.common.unit.TimeValue) MockSleeper(com.google.api.client.testing.util.MockSleeper)

Example 3 with MockGoogleCredential

use of com.google.api.client.googleapis.testing.auth.oauth2.MockGoogleCredential in project elasticsearch by elastic.

the class RetryHttpInitializerWrapperTests method testSimpleRetry.

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));
}
Also used : LowLevelHttpRequest(com.google.api.client.http.LowLevelHttpRequest) HttpRequest(com.google.api.client.http.HttpRequest) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest) Compute(com.google.api.services.compute.Compute) MockGoogleCredential(com.google.api.client.googleapis.testing.auth.oauth2.MockGoogleCredential) MockLowLevelHttpResponse(com.google.api.client.testing.http.MockLowLevelHttpResponse) HttpResponse(com.google.api.client.http.HttpResponse) LowLevelHttpResponse(com.google.api.client.http.LowLevelHttpResponse) GenericUrl(com.google.api.client.http.GenericUrl) JacksonFactory(com.google.api.client.json.jackson2.JacksonFactory) MockSleeper(com.google.api.client.testing.util.MockSleeper)

Example 4 with MockGoogleCredential

use of com.google.api.client.googleapis.testing.auth.oauth2.MockGoogleCredential in project druid by druid-io.

the class GcpModuleTest method testSimpleModuleLoads.

@Test
public void testSimpleModuleLoads() {
    final Injector injector = Guice.createInjector(Modules.override(new GcpModule()).with(new GcpMockModule() {

        @Override
        public void configure(Binder binder) {
            binder.bindScope(LazySingleton.class, Scopes.SINGLETON);
        }
    }));
    Assert.assertTrue(injector.getInstance(HttpRequestInitializer.class) instanceof MockGoogleCredential);
    Assert.assertTrue(injector.getInstance(HttpTransport.class) instanceof MockHttpTransport);
}
Also used : Binder(com.google.inject.Binder) MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport) Injector(com.google.inject.Injector) MockGoogleCredential(com.google.api.client.googleapis.testing.auth.oauth2.MockGoogleCredential) Test(org.junit.Test)

Aggregations

MockGoogleCredential (com.google.api.client.googleapis.testing.auth.oauth2.MockGoogleCredential)4 GenericUrl (com.google.api.client.http.GenericUrl)3 HttpRequest (com.google.api.client.http.HttpRequest)3 LowLevelHttpRequest (com.google.api.client.http.LowLevelHttpRequest)3 JacksonFactory (com.google.api.client.json.jackson2.JacksonFactory)3 MockLowLevelHttpRequest (com.google.api.client.testing.http.MockLowLevelHttpRequest)3 MockSleeper (com.google.api.client.testing.util.MockSleeper)3 Compute (com.google.api.services.compute.Compute)3 HttpResponse (com.google.api.client.http.HttpResponse)2 LowLevelHttpResponse (com.google.api.client.http.LowLevelHttpResponse)2 MockLowLevelHttpResponse (com.google.api.client.testing.http.MockLowLevelHttpResponse)2 HttpResponseException (com.google.api.client.http.HttpResponseException)1 JsonFactory (com.google.api.client.json.JsonFactory)1 MockHttpTransport (com.google.api.client.testing.http.MockHttpTransport)1 Binder (com.google.inject.Binder)1 Injector (com.google.inject.Injector)1 TimeValue (org.elasticsearch.common.unit.TimeValue)1 Test (org.junit.Test)1