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;
}
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);
}
}
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);
}
}
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;
}
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();
}
Aggregations