Search in sources :

Example 1 with CancellationToken

use of com.azure.android.core.util.CancellationToken in project azure-sdk-for-android by Azure.

the class CFBackedPageAsyncStream method forEach.

@Override
public CancellationToken forEach(AsyncStreamHandler<PagedResponse<T>> handler) {
    final CancellationToken token = new CancellationToken();
    handler.onInit(token);
    if (token.isCancellationRequested()) {
        handler.onError(new CancellationException());
        return token;
    }
    final CompletableFuture<Void> completableFuture = this.enumeratePages(startPageId, token, handler);
    token.registerOnCancel(() -> {
        completableFuture.cancel(true);
    });
    completableFuture.whenCompleteAsync((ignored, throwable) -> {
        if (throwable != null) {
            if (throwable instanceof CompletionException && throwable.getCause() != null) {
                // unwrap CF's CompletionException.
                handler.onError(throwable.getCause());
            } else {
                handler.onError(throwable);
            }
        } else {
            handler.onComplete();
        }
    });
    return token;
}
Also used : CancellationToken(com.azure.android.core.util.CancellationToken) CancellationException(java.util.concurrent.CancellationException) CompletionException(java9.util.concurrent.CompletionException)

Example 2 with CancellationToken

use of com.azure.android.core.util.CancellationToken in project azure-sdk-for-android by Azure.

the class PagedAsyncStreamTests method canStopPageEnumeration.

@Test
public void canStopPageEnumeration() {
    final CFStringPageRetriever pageRetriever = new CFStringPageRetriever(3, 5);
    // 
    final PagedAsyncStream<String> asyncStream = new PagedAsyncStream<>(pageId -> {
        final CFBackedPageAsyncStream backingStream = new CFBackedPageAsyncStream<>(pageRetriever, continuationToken -> continuationToken != null, pageId, logger);
        return backingStream;
    }, this.logger);
    CountDownLatch latch = new CountDownLatch(1);
    Throwable[] error = new Throwable[1];
    asyncStream.byPage().forEach(new AsyncStreamHandler<PagedResponse<String>>() {

        private CancellationToken token;

        @Override
        public void onInit(CancellationToken cancellationToken) {
            this.token = cancellationToken;
        }

        @Override
        public void onNext(PagedResponse<String> response) {
            Assertions.assertNotNull(response);
            if (response.getContinuationToken().equalsIgnoreCase("3")) {
                token.cancel();
            }
        }

        @Override
        public void onError(Throwable throwable) {
            error[0] = throwable;
            latch.countDown();
        }

        @Override
        public void onComplete() {
            latch.countDown();
        }
    });
    awaitOnLatch(latch, "canStopPageEnumeration");
    Assertions.assertEquals(3, pageRetriever.getCallCount());
    Assertions.assertNotNull(error[0]);
    Assertions.assertTrue(error[0] instanceof RuntimeException);
    if (error[0].getCause() != null) {
        // To investigate: It seems when running test using "gradle from commandline"
        // getCause() returns null but not-null from an app or editor.
        Assertions.assertTrue(error[0].getCause() instanceof CancellationException);
    }
}
Also used : CancellationToken(com.azure.android.core.util.CancellationToken) CountDownLatch(java.util.concurrent.CountDownLatch) CancellationException(java.util.concurrent.CancellationException) Test(org.junit.jupiter.api.Test)

Example 3 with CancellationToken

use of com.azure.android.core.util.CancellationToken in project azure-sdk-for-android by Azure.

the class PagedAsyncStreamTests method canStopElementEnumeration.

@Test
public void canStopElementEnumeration() {
    final CFStringPageRetriever pageRetriever = new CFStringPageRetriever(3, 5);
    // 
    final PagedAsyncStream<String> asyncStream = new PagedAsyncStream<>(pageId -> {
        final CFBackedPageAsyncStream backingStream = new CFBackedPageAsyncStream<>(pageRetriever, continuationToken -> continuationToken != null, pageId, logger);
        return backingStream;
    }, this.logger);
    CountDownLatch latch = new CountDownLatch(1);
    Throwable[] error = new Throwable[1];
    asyncStream.forEach(new AsyncStreamHandler<String>() {

        private CancellationToken token;

        @Override
        public void onInit(CancellationToken cancellationToken) {
            this.token = cancellationToken;
        }

        @Override
        public void onNext(String element) {
            Assertions.assertNotNull(element);
            if (element.equalsIgnoreCase("8")) {
                // "8" is an element in page 3.
                token.cancel();
            }
        }

        @Override
        public void onError(Throwable throwable) {
            error[0] = throwable;
            latch.countDown();
        }

        @Override
        public void onComplete() {
            latch.countDown();
        }
    });
    awaitOnLatch(latch, "canStopElementEnumeration");
    Assertions.assertEquals(3, pageRetriever.getCallCount());
    Assertions.assertNotNull(error[0]);
    Assertions.assertTrue(error[0] instanceof RuntimeException);
    if (error[0].getCause() != null) {
        // To investigate: It seems when running test using "gradle from commandline"
        // getCause() returns null but not-null from an app or editor.
        Assertions.assertTrue(error[0].getCause() instanceof CancellationException);
    }
}
Also used : CancellationToken(com.azure.android.core.util.CancellationToken) CountDownLatch(java.util.concurrent.CountDownLatch) CancellationException(java.util.concurrent.CancellationException) Test(org.junit.jupiter.api.Test)

Example 4 with CancellationToken

use of com.azure.android.core.util.CancellationToken in project azure-sdk-for-android by Azure.

the class RestProxy method invoke.

@Override
public Object invoke(final Object restProxy, final Method swaggerMethod, final Object[] swaggerMethodArgs) {
    final SwaggerMethodParser methodParser = this.interfaceParser.getMethodParser(swaggerMethod, this.logger);
    final Callback<Response<?>> restCallback;
    restCallback = (Callback<Response<?>>) swaggerMethodArgs[methodParser.callbackArgIndex];
    Objects.requireNonNull(restCallback);
    final CancellationToken cancellationToken;
    if (methodParser.cancellationTokenArgIndex == -1) {
        cancellationToken = CancellationToken.NONE;
    } else {
        cancellationToken = (CancellationToken) swaggerMethodArgs[methodParser.cancellationTokenArgIndex];
    }
    final HttpRequest httpRequest;
    try {
        httpRequest = methodParser.mapToHttpRequest(swaggerMethodArgs);
    } catch (IOException e) {
        restCallback.onFailure(e);
        return null;
    } catch (HttpResponseException e) {
        restCallback.onFailure(e);
        return null;
    }
    this.httpPipeline.send(httpRequest, RequestContext.NONE, cancellationToken, new HttpPipelineCallback(methodParser, restCallback));
    return null;
}
Also used : HttpResponse(com.azure.android.core.http.HttpResponse) HttpRequest(com.azure.android.core.http.HttpRequest) CancellationToken(com.azure.android.core.util.CancellationToken) HttpResponseException(com.azure.android.core.http.exception.HttpResponseException) IOException(java.io.IOException)

Example 5 with CancellationToken

use of com.azure.android.core.util.CancellationToken in project azure-sdk-for-android by Azure.

the class ChatThreadAsyncClientTest method setupMockTest.

private void setupMockTest() {
    final NoOpHttpClient httpClient = new NoOpHttpClient() {

        @Override
        public void send(HttpRequest httpRequest, CancellationToken cancellationToken, HttpCallback httpCallback) {
            httpCallback.onSuccess(ChatResponseMocker.createReadReceiptsResponse(httpRequest));
        }
    };
    this.client = getChatClientBuilder(httpClient).buildAsyncClient();
    String mockThreadId = "19:4b72178530934b7790135dd9359205e0@thread.v2";
    this.chatThreadClient = client.getChatThreadClient(mockThreadId);
    this.threadId = chatThreadClient.getChatThreadId();
}
Also used : HttpRequest(com.azure.android.core.http.HttpRequest) NoOpHttpClient(com.azure.android.core.test.http.NoOpHttpClient) CancellationToken(com.azure.android.core.util.CancellationToken) HttpCallback(com.azure.android.core.http.HttpCallback)

Aggregations

CancellationToken (com.azure.android.core.util.CancellationToken)19 HttpRequest (com.azure.android.core.http.HttpRequest)15 CountDownLatch (java.util.concurrent.CountDownLatch)15 HttpCallback (com.azure.android.core.http.HttpCallback)14 HttpResponse (com.azure.android.core.http.HttpResponse)14 HttpPipeline (com.azure.android.core.http.HttpPipeline)13 HttpPipelineBuilder (com.azure.android.core.http.HttpPipelineBuilder)13 Test (org.junit.jupiter.api.Test)13 HttpHeaders (com.azure.android.core.http.HttpHeaders)4 CancellationException (java.util.concurrent.CancellationException)4 HttpCallDispatcher (com.azure.android.core.http.HttpCallDispatcher)2 HttpClient (com.azure.android.core.http.HttpClient)2 IOException (java.io.IOException)2 CompletionException (java9.util.concurrent.CompletionException)2 ResourceLock (org.junit.jupiter.api.parallel.ResourceLock)2 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 MethodSource (org.junit.jupiter.params.provider.MethodSource)2 HttpResponseException (com.azure.android.core.http.exception.HttpResponseException)1 NoOpHttpClient (com.azure.android.core.test.http.NoOpHttpClient)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1