Android Open Source - couchbase-lite-android Remote Request Test






From Project

Back to project page couchbase-lite-android.

License

The source code is released under:

Apache License

If you think the Android project couchbase-lite-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.couchbase.lite.support;
//from  w ww .java 2  s  .  c o m

import com.couchbase.lite.LiteTestCase;
import com.couchbase.lite.mockserver.MockCheckpointPut;
import com.couchbase.lite.mockserver.MockDispatcher;
import com.couchbase.lite.mockserver.MockHelper;
import com.couchbase.lite.mockserver.MockRevsDiff;
import com.couchbase.lite.mockserver.WrappedSmartMockResponse;
import com.couchbase.lite.util.Log;
import com.squareup.okhttp.mockwebserver.MockResponse;
import com.squareup.okhttp.mockwebserver.MockWebServer;
import com.squareup.okhttp.mockwebserver.RecordedRequest;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpResponseException;

import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class RemoteRequestTest extends LiteTestCase {

    /**
     * Make RemoteRequests will retry correctly.
     *
     * Return MAX_RETRIES - 1 503 transient errors, followed by a 404 non-transient error.
     * It should retry for the 503 errors and return after it gets the 404.
     */
    public void testRetryLastRequestSuccess() throws Exception {

        // lower retry to speed up test
        RemoteRequestRetry.RETRY_DELAY_MS = 5;

        PersistentCookieStore cookieStore = database.getPersistentCookieStore();
        CouchbaseLiteHttpClientFactory factory = new CouchbaseLiteHttpClientFactory(cookieStore);

        // create mockwebserver and custom dispatcher
        MockDispatcher dispatcher = new MockDispatcher();
        MockWebServer server = MockHelper.getMockWebServer(dispatcher);
        dispatcher.setServerType(MockDispatcher.ServerType.SYNC_GW);

        // respond with 503 error for the first MAX_RETRIES - 1 requests
        int num503Responses = RemoteRequestRetry.MAX_RETRIES - 1;
        for (int i=0; i<num503Responses; i++) {
            dispatcher.enqueueResponse(MockHelper.PATH_REGEX_CHECKPOINT, new MockResponse().setResponseCode(503));
        }
        // on last request, respond with 404 error
        MockCheckpointPut mockCheckpointPut = new MockCheckpointPut();
        dispatcher.enqueueResponse(MockHelper.PATH_REGEX_CHECKPOINT, mockCheckpointPut);

        server.play();

        String urlString = String.format("%s/%s", server.getUrl("/db"), "_local");
        URL url = new URL(urlString);

        Map<String, Object> requestBody = new HashMap<String, Object>();
        requestBody.put("foo", "bar");

        Map<String, Object> requestHeaders = new HashMap<String, Object>();

        final CountDownLatch received404Error = new CountDownLatch(1);

        RemoteRequestCompletionBlock completionBlock = new RemoteRequestCompletionBlock() {
            @Override
            public void onCompletion(HttpResponse httpResponse, Object result, Throwable e) {
                if (e instanceof HttpResponseException) {
                    HttpResponseException htre = (HttpResponseException) e;
                    if (htre.getStatusCode() == 404) {
                        received404Error.countDown();
                    }
                }
            }
        };

        ScheduledExecutorService requestExecutorService = Executors.newScheduledThreadPool(5);
        ScheduledExecutorService workExecutorService = Executors.newSingleThreadScheduledExecutor();

        // ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor(4);
        RemoteRequestRetry request = new RemoteRequestRetry(
                RemoteRequestRetry.RemoteRequestType.REMOTE_REQUEST,
                requestExecutorService,
                workExecutorService,
                factory,
                "GET",
                url,
                requestBody,
                database,
                requestHeaders,
                completionBlock
        );

        // wait for the future to return
        Future future = request.submit();
        future.get(300, TimeUnit.SECONDS);

        // at this point, the completionBlock should have already been called back
        // with a 404 error, which will decrement countdown latch.
        boolean success = received404Error.await(1, TimeUnit.SECONDS);
        assertTrue(success);

        // make sure that we saw MAX_RETRIES requests sent to server
        for (int i=0; i<RemoteRequestRetry.MAX_RETRIES; i++) {
            RecordedRequest recordedRequest = dispatcher.takeRequest(MockHelper.PATH_REGEX_CHECKPOINT);
            assertNotNull(recordedRequest);
        }

    }


    public void testRetryAllRequestsFail() throws Exception {

        // lower retry to speed up test
        RemoteRequestRetry.RETRY_DELAY_MS = 5;

        PersistentCookieStore cookieStore = database.getPersistentCookieStore();
        CouchbaseLiteHttpClientFactory factory = new CouchbaseLiteHttpClientFactory(cookieStore);

        // create mockwebserver and custom dispatcher
        MockDispatcher dispatcher = new MockDispatcher();
        MockWebServer server = MockHelper.getMockWebServer(dispatcher);
        dispatcher.setServerType(MockDispatcher.ServerType.SYNC_GW);

        // respond with 503 error for all requests
        int num503Responses = RemoteRequestRetry.MAX_RETRIES + 1;
        for (int i=0; i<num503Responses; i++) {
            dispatcher.enqueueResponse(MockHelper.PATH_REGEX_CHECKPOINT, new MockResponse().setResponseCode(503));
        }

        server.play();

        String urlString = String.format("%s/%s", server.getUrl("/db"), "_local");
        URL url = new URL(urlString);

        Map<String, Object> requestBody = new HashMap<String, Object>();
        requestBody.put("foo", "bar");

        Map<String, Object> requestHeaders = new HashMap<String, Object>();

        final CountDownLatch received503Error = new CountDownLatch(1);

        RemoteRequestCompletionBlock completionBlock = new RemoteRequestCompletionBlock() {
            @Override
            public void onCompletion(HttpResponse httpResponse, Object result, Throwable e) {
                if (e instanceof HttpResponseException) {
                    HttpResponseException htre = (HttpResponseException) e;
                    if (htre.getStatusCode() == 503) {
                        received503Error.countDown();
                    }
                }
            }
        };

        ScheduledExecutorService requestExecutorService = Executors.newScheduledThreadPool(5);
        ScheduledExecutorService workExecutorService = Executors.newSingleThreadScheduledExecutor();

        // ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor(4);
        RemoteRequestRetry request = new RemoteRequestRetry(
                RemoteRequestRetry.RemoteRequestType.REMOTE_REQUEST,
                requestExecutorService,
                workExecutorService,
                factory,
                "GET",
                url,
                requestBody,
                database,
                requestHeaders,
                completionBlock
        );

        // wait for the future to return
        Future future = request.submit();

        future.get(300, TimeUnit.SECONDS);

        // at this point, the completionBlock should have already been called back
        // with a 404 error, which will decrement countdown latch.
        boolean success = received503Error.await(1, TimeUnit.SECONDS);
        assertTrue(success);

        // make sure that we saw MAX_RETRIES requests sent to server
        for (int i=0; i<RemoteRequestRetry.MAX_RETRIES; i++) {
            RecordedRequest recordedRequest = dispatcher.takeRequest(MockHelper.PATH_REGEX_CHECKPOINT);
            assertNotNull(recordedRequest);
        }

    }


    /**
     * Reproduce a severe issue where the pusher stops working because it's remoteRequestExecutor
     * is full of tasks which are all blocked trying to add more tasks to the queue.
     *
     *
     * Failing on Jenkins, looking into it.
     * 
     * @throws Exception
     */
    public void failingTestRetryQueueDeadlock() throws Exception {

        // lower retry to speed up test
        RemoteRequestRetry.RETRY_DELAY_MS = 5;

        PersistentCookieStore cookieStore = database.getPersistentCookieStore();
        CouchbaseLiteHttpClientFactory factory = new CouchbaseLiteHttpClientFactory(cookieStore);

        // create mockwebserver and custom dispatcher
        MockDispatcher dispatcher = new MockDispatcher();
        MockWebServer server = MockHelper.getMockWebServer(dispatcher);
        dispatcher.setServerType(MockDispatcher.ServerType.SYNC_GW);

        // respond with 503 error for all requests
        MockResponse response = new MockResponse().setResponseCode(503);
        WrappedSmartMockResponse wrapped = new WrappedSmartMockResponse(response);
        wrapped.setDelayMs(5);
        wrapped.setSticky(true);
        dispatcher.enqueueResponse(MockHelper.PATH_REGEX_CHECKPOINT, wrapped);

        server.play();

        String urlString = String.format("%s/%s", server.getUrl("/db"), "_local");
        URL url = new URL(urlString);

        Map<String, Object> requestBody = new HashMap<String, Object>();
        requestBody.put("foo", "bar");

        Map<String, Object> requestHeaders = new HashMap<String, Object>();

        int threadPoolSize = 5;
        int numRequests = 10;

        final CountDownLatch received503Error = new CountDownLatch(numRequests);

        RemoteRequestCompletionBlock completionBlock = new RemoteRequestCompletionBlock() {
            @Override
            public void onCompletion(HttpResponse httpResponse, Object result, Throwable e) {
                if (e instanceof HttpResponseException) {
                    HttpResponseException htre = (HttpResponseException) e;
                    if (htre.getStatusCode() == 503) {
                        received503Error.countDown();
                    }
                }
            }
        };

        ScheduledExecutorService requestExecutorService = Executors.newScheduledThreadPool(5);
        ScheduledExecutorService workExecutorService = Executors.newSingleThreadScheduledExecutor();

        List<Future> requestFutures = new ArrayList<Future>();

        for (int i=0; i<numRequests; i++) {

            // ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor(4);
            RemoteRequestRetry request = new RemoteRequestRetry(
                    RemoteRequestRetry.RemoteRequestType.REMOTE_REQUEST,
                    requestExecutorService,
                    workExecutorService,
                    factory,
                    "GET",
                    url,
                    requestBody,
                    database,
                    requestHeaders,
                    completionBlock
            );

            Future future = request.submit();
            requestFutures.add(future);

        }

        for (Future future : requestFutures) {
            future.get();
        }

        boolean success = received503Error.await(120, TimeUnit.SECONDS);
        assertTrue(success);






    }




}




Java Source Code List

com.couchbase.lite.ApiTest.java
com.couchbase.lite.AttachmentsTest.java
com.couchbase.lite.AuthTest.java
com.couchbase.lite.Base64Test.java
com.couchbase.lite.BlobStoreWriterTest.java
com.couchbase.lite.CRUDOperationsTest.java
com.couchbase.lite.CacheTest.java
com.couchbase.lite.ChangesTest.java
com.couchbase.lite.CollationTest.java
com.couchbase.lite.DatabaseTest.java
com.couchbase.lite.DocumentTest.java
com.couchbase.lite.LitePerfTestCase.java
com.couchbase.lite.LiteTestCase.java
com.couchbase.lite.LiteTestContext.java
com.couchbase.lite.LocalDocsTest.java
com.couchbase.lite.ManagerTest.java
com.couchbase.lite.MiscTest.java
com.couchbase.lite.MultipartReaderTest.java
com.couchbase.lite.RevTreeTest.java
com.couchbase.lite.RevisionsTest.java
com.couchbase.lite.RouterTest.java
com.couchbase.lite.SequenceMapTest.java
com.couchbase.lite.ValidationsTest.java
com.couchbase.lite.ViewsTest.java
com.couchbase.lite.android.AndroidContext.java
com.couchbase.lite.android.AndroidLogger.java
com.couchbase.lite.android.AndroidNetworkReachabilityManager.java
com.couchbase.lite.android.AndroidSQLiteStorageEngineFactory.java
com.couchbase.lite.android.AndroidSQLiteStorageEngine.java
com.couchbase.lite.mockserver.MockBulkDocs.java
com.couchbase.lite.mockserver.MockChangesFeedNoResponse.java
com.couchbase.lite.mockserver.MockChangesFeed.java
com.couchbase.lite.mockserver.MockCheckpointGet.java
com.couchbase.lite.mockserver.MockCheckpointPut.java
com.couchbase.lite.mockserver.MockDispatcher.java
com.couchbase.lite.mockserver.MockDocumentBulkGet.java
com.couchbase.lite.mockserver.MockDocumentGet.java
com.couchbase.lite.mockserver.MockDocumentPut.java
com.couchbase.lite.mockserver.MockFacebookAuthPost.java
com.couchbase.lite.mockserver.MockHelper.java
com.couchbase.lite.mockserver.MockPreloadedPullTarget.java
com.couchbase.lite.mockserver.MockRevsDiff.java
com.couchbase.lite.mockserver.MockSessionGet.java
com.couchbase.lite.mockserver.SmartMockResponse.java
com.couchbase.lite.mockserver.WrappedSmartMockResponse.java
com.couchbase.lite.performance2.Test01_CreateDocs.java
com.couchbase.lite.performance2.Test02_CreateDocsUnoptimizedWay.java
com.couchbase.lite.performance2.Test03_CreateDocsWithAttachments.java
com.couchbase.lite.performance2.Test06_PullReplication.java
com.couchbase.lite.performance2.Test07_PushReplication.java
com.couchbase.lite.performance2.Test08_DocRevisions.java
com.couchbase.lite.performance2.Test09_LoadDB.java
com.couchbase.lite.performance2.Test10_DeleteDB.java
com.couchbase.lite.performance2.Test11_DeleteDocs.java
com.couchbase.lite.performance2.Test12_IndexView.java
com.couchbase.lite.performance2.Test13_QueryView.java
com.couchbase.lite.performance2.Test14_ReduceView.java
com.couchbase.lite.performance2.Test28_KeySizes.java
com.couchbase.lite.performance2.Test29_AllDocQuery.java
com.couchbase.lite.performance2.Test30_LiveQuery.java
com.couchbase.lite.performance2.Test31_CompactDB.java
com.couchbase.lite.performance.Test10_DeleteDB.java
com.couchbase.lite.performance.Test11_DeleteDocs.java
com.couchbase.lite.performance.Test12_IndexView.java
com.couchbase.lite.performance.Test13_QueryView.java
com.couchbase.lite.performance.Test14_ReduceView.java
com.couchbase.lite.performance.Test16_ParallelPushReplication.java
com.couchbase.lite.performance.Test1_CreateDocs.java
com.couchbase.lite.performance.Test2_CreateDocsUnoptimizedWay.java
com.couchbase.lite.performance.Test3_CreateDocsWithAttachments.java
com.couchbase.lite.performance.Test6_PushReplication.java
com.couchbase.lite.performance.Test7_PullReplication.java
com.couchbase.lite.performance.Test8_DocRevisions.java
com.couchbase.lite.performance.Test9_LoadDB.java
com.couchbase.lite.replicator.BulkDownloaderTest.java
com.couchbase.lite.replicator.ChangeTrackerTest.java
com.couchbase.lite.replicator.CustomizableMockHttpClient.java
com.couchbase.lite.replicator.ReplicationTest.java
com.couchbase.lite.replicator.ResponderChain.java
com.couchbase.lite.support.BatcherTest.java
com.couchbase.lite.support.JsonDocumentTest.java
com.couchbase.lite.support.PersistentCookieStoreTest.java
com.couchbase.lite.support.RemoteRequestTest.java
com.couchbase.touchdb.RevCollator.java
com.couchbase.touchdb.TDCollateJSON.java