Search in sources :

Example 11 with HttpRequestor

use of com.dropbox.core.http.HttpRequestor in project dropbox-sdk-java by dropbox.

the class DbxClientV2Test method testRetryDisabled.

@Test(expectedExceptions = RetryException.class)
public void testRetryDisabled() throws DbxException, IOException {
    HttpRequestor mockRequestor = mock(HttpRequestor.class);
    DbxRequestConfig config = createRequestConfig().withAutoRetryDisabled().withHttpRequestor(mockRequestor).build();
    DbxClientV2 client = new DbxClientV2(config, "fakeAccessToken");
    // 503 every time
    HttpRequestor.Uploader mockUploader = mockUploader();
    when(mockUploader.finish()).thenReturn(createEmptyResponse(503));
    when(mockRequestor.startPost(anyString(), anyHeaders())).thenReturn(mockUploader);
    try {
        client.users().getCurrentAccount();
    } finally {
        // should only have been called once since we disabled retry
        verify(mockRequestor, times(1)).startPost(anyString(), anyHeaders());
    }
}
Also used : HttpRequestor(com.dropbox.core.http.HttpRequestor) DbxRequestConfig(com.dropbox.core.DbxRequestConfig) Test(org.testng.annotations.Test)

Example 12 with HttpRequestor

use of com.dropbox.core.http.HttpRequestor in project dropbox-sdk-java by dropbox.

the class DbxClientV2Test method testRetrySuccessWithBackoff.

@Test
public void testRetrySuccessWithBackoff() throws Exception {
    HttpRequestor mockRequestor = mock(HttpRequestor.class);
    DbxRequestConfig config = createRequestConfig().withAutoRetryEnabled(3).withHttpRequestor(mockRequestor).build();
    DbxClientV2 client = new DbxClientV2(config, "fakeAccessToken");
    FileMetadata expected = constructFileMetadate();
    // 503 twice, then return result
    HttpRequestor.Uploader mockUploader = mockUploader();
    when(mockUploader.finish()).thenReturn(// no backoff
    createEmptyResponse(503)).thenReturn(// backoff 1 sec
    createRateLimitResponse(1)).thenReturn(// backoff 2 sec
    createRateLimitResponse(2)).thenReturn(createSuccessResponse(serialize(expected)));
    when(mockRequestor.startPost(anyString(), anyHeaders())).thenReturn(mockUploader);
    long start = System.currentTimeMillis();
    Metadata actual = client.files().getMetadata(expected.getId());
    long end = System.currentTimeMillis();
    // no way easy way to properly test this, but request should
    // have taken AT LEAST 3 seconds due to backoff.
    assertTrue((end - start) >= 3000L, "duration: " + (end - start) + " millis");
    // should have been called 4 times: initial call + 3 retries
    verify(mockRequestor, times(4)).startPost(anyString(), anyHeaders());
    assertEquals(actual.getName(), expected.getName());
    assertTrue(actual instanceof FileMetadata, actual.getClass().toString());
}
Also used : HttpRequestor(com.dropbox.core.http.HttpRequestor) DbxRequestConfig(com.dropbox.core.DbxRequestConfig) FileMetadata(com.dropbox.core.v2.files.FileMetadata) FileMetadata(com.dropbox.core.v2.files.FileMetadata) Metadata(com.dropbox.core.v2.files.Metadata) Test(org.testng.annotations.Test)

Aggregations

HttpRequestor (com.dropbox.core.http.HttpRequestor)12 Test (org.testng.annotations.Test)12 DbxRequestConfig (com.dropbox.core.DbxRequestConfig)11 FileMetadata (com.dropbox.core.v2.files.FileMetadata)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 Metadata (com.dropbox.core.v2.files.Metadata)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 Downloader (com.dropbox.core.v1.DbxClientV1.Downloader)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1