Skip to content

Commit bbad8ab

Browse files
authored
General Core changes before release (Azure#37055)
1 parent 0f0190a commit bbad8ab

File tree

9 files changed

+54
-25
lines changed

9 files changed

+54
-25
lines changed

sdk/core/azure-core/src/main/java/com/azure/core/implementation/ImplUtils.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,11 @@ public static RetryStrategy getRetryStrategyFromOptions(RetryOptions retryOption
384384
}
385385
}
386386

387+
@SuppressWarnings("unchecked")
388+
public static <E extends Throwable> void sneakyThrows(Throwable e) throws E {
389+
throw (E) e;
390+
}
391+
387392
private ImplUtils() {
388393
}
389394
}

sdk/core/azure-core/src/main/java/com/azure/core/implementation/http/rest/AsyncRestProxy.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,10 @@ public void updateRequest(RequestDataConfiguration requestDataConfiguration, Ser
287287
Object bodyContentObject = requestDataConfiguration.getBodyContent();
288288
SwaggerMethodParser methodParser = requestDataConfiguration.getMethodParser();
289289

290+
if (bodyContentObject == null) {
291+
return;
292+
}
293+
290294
// Attempt to use JsonSerializable or XmlSerializable in a separate block.
291295
if (supportsJsonSerializable(bodyContentObject.getClass())) {
292296
request.setBody(serializeJsonSerializableToBytes((JsonSerializable<?>) bodyContentObject));

sdk/core/azure-core/src/main/java/com/azure/core/implementation/http/rest/RestProxyBase.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -409,10 +409,5 @@ static boolean supportsXmlSerializable(Class<?> bodyContentClass) {
409409
static ByteBuffer serializeAsXmlSerializable(Object bodyContent) throws IOException {
410410
return ReflectionSerializable.serializeXmlSerializableToByteBuffer(bodyContent);
411411
}
412-
413-
@SuppressWarnings("unchecked")
414-
static <E extends Exception> void sneakyThrows(Exception e) throws E {
415-
throw (E) e;
416-
}
417412
}
418413

sdk/core/azure-core/src/main/java/com/azure/core/implementation/http/rest/SyncRestProxy.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.azure.core.http.rest.RequestOptions;
1111
import com.azure.core.http.rest.Response;
1212
import com.azure.core.http.rest.StreamResponse;
13+
import com.azure.core.implementation.ImplUtils;
1314
import com.azure.core.implementation.TypeUtil;
1415
import com.azure.core.implementation.serializer.HttpResponseDecoder;
1516
import com.azure.core.util.Base64Url;
@@ -82,7 +83,7 @@ public Object invoke(Object proxy, Method method, RequestOptions options, EnumSe
8283
errorOptions);
8384
} catch (Exception e) {
8485
tracer.end(null, e, context);
85-
sneakyThrows(e);
86+
ImplUtils.sneakyThrows(e);
8687
return null;
8788
}
8889
}
@@ -220,6 +221,10 @@ public void updateRequest(RequestDataConfiguration requestDataConfiguration,
220221
HttpRequest request = requestDataConfiguration.getHttpRequest();
221222
Object bodyContentObject = requestDataConfiguration.getBodyContent();
222223

224+
if (bodyContentObject == null) {
225+
return;
226+
}
227+
223228
// Attempt to use JsonSerializable or XmlSerializable in a separate block.
224229
if (supportsJsonSerializable(bodyContentObject.getClass())) {
225230
request.setBody(serializeJsonSerializableToBytes((JsonSerializable<?>) bodyContentObject));

sdk/core/azure-core/src/main/java/com/azure/core/util/CoreUtils.java

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -541,24 +541,50 @@ static UUID randomUuid(long msb, long lsb) {
541541
* Calls {@link Future#get(long, TimeUnit)} and returns the value if the {@code future} completes before the timeout
542542
* is triggered. If the timeout is triggered, the {@code future} is {@link Future#cancel(boolean) cancelled}
543543
* interrupting the execution of the task that the {@link Future} represented.
544+
* <p>
545+
* If the timeout is {@link Duration#isZero()} or is {@link Duration#isNegative()} then the timeout will be ignored
546+
* and an infinite timeout will be used.
544547
*
545548
* @param <T> The type of value returned by the {@code future}.
546549
* @param future The {@link Future} to get the value from.
547-
* @param timeout The timeout value.
548-
* @param unit The {@link TimeUnit} of the timeout value.
550+
* @param timeout The timeout value. If the timeout is {@link Duration#isZero()} or is {@link Duration#isNegative()}
551+
* then the timeout will be ignored and an infinite timeout will be used.
549552
* @return The value from the {@code future}.
553+
* @throws NullPointerException If {@code future} is null.
550554
* @throws CancellationException If the computation was cancelled.
551555
* @throws ExecutionException If the computation threw an exception.
552556
* @throws InterruptedException If the current thread was interrupted while waiting.
553557
* @throws TimeoutException If the wait timed out.
558+
* @throws RuntimeException If the {@code future} threw an exception during processing.
559+
* @throws Error If the {@code future} threw an {@link Error} during processing.
554560
*/
555-
public static <T> T getFutureWithCancellation(Future<T> future, long timeout, TimeUnit unit)
561+
public static <T> T getResultWithTimeout(Future<T> future, Duration timeout)
556562
throws InterruptedException, ExecutionException, TimeoutException {
563+
Objects.requireNonNull(future, "'future' cannot be null.");
564+
565+
if (!hasTimeout(timeout)) {
566+
return future.get();
567+
}
568+
557569
try {
558-
return future.get(timeout, unit);
570+
return future.get(timeout.toMillis(), TimeUnit.MILLISECONDS);
559571
} catch (TimeoutException e) {
560572
future.cancel(true);
561573
throw e;
574+
} catch (ExecutionException e) {
575+
Throwable cause = e.getCause();
576+
if (cause instanceof Error) {
577+
throw (Error) cause;
578+
} else if (cause instanceof RuntimeException) {
579+
throw (RuntimeException) cause;
580+
} else {
581+
ImplUtils.sneakyThrows(cause);
582+
throw e;
583+
}
562584
}
563585
}
586+
587+
private static boolean hasTimeout(Duration timeout) {
588+
return timeout != null && !timeout.isZero() && !timeout.isNegative();
589+
}
564590
}

sdk/core/azure-core/src/test/java/com/azure/core/util/CoreUtilsTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@ public void futureTimesOutAndIsCancelled() {
548548
});
549549

550550
try {
551-
CoreUtils.getFutureWithCancellation(future, 100, TimeUnit.MILLISECONDS);
551+
CoreUtils.getResultWithTimeout(future, Duration.ofMillis(100));
552552
fail("Expected future to timout and be cancelled.");
553553
} catch (TimeoutException e) {
554554
// Expected.

sdk/tables/azure-data-tables/src/main/java/com/azure/data/tables/TableClient.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,12 @@
6666
import java.util.Map;
6767
import java.util.concurrent.ExecutionException;
6868
import java.util.concurrent.ExecutorService;
69-
import java.util.concurrent.TimeUnit;
7069
import java.util.concurrent.TimeoutException;
7170
import java.util.function.BiConsumer;
7271
import java.util.function.Supplier;
7372
import java.util.stream.Collectors;
7473

75-
import static com.azure.core.util.CoreUtils.getFutureWithCancellation;
74+
import static com.azure.core.util.CoreUtils.getResultWithTimeout;
7675
import static com.azure.data.tables.implementation.TableUtils.callWithOptionalTimeout;
7776
import static com.azure.data.tables.implementation.TableUtils.hasTimeout;
7877
import static com.azure.data.tables.implementation.TableUtils.mapThrowableToTableServiceException;
@@ -343,8 +342,7 @@ public Response<Void> deleteTableWithResponse(Duration timeout, Context context)
343342

344343
try {
345344
return hasTimeout(timeout)
346-
? getFutureWithCancellation(THREAD_POOL.submit(callable::get), timeout.toMillis(), TimeUnit.MILLISECONDS)
347-
: callable.get();
345+
? getResultWithTimeout(THREAD_POOL.submit(callable::get), timeout) : callable.get();
348346
} catch (InterruptedException | ExecutionException | TimeoutException ex) {
349347
throw logger.logExceptionAsError(new RuntimeException(ex));
350348
} catch (RuntimeException ex) {
@@ -819,8 +817,7 @@ private Response<Void> deleteEntityWithResponse(String partitionKey, String rowK
819817

820818
try {
821819
return hasTimeout(timeout)
822-
? getFutureWithCancellation(THREAD_POOL.submit(callable::get), timeout.toMillis(), TimeUnit.MILLISECONDS)
823-
: callable.get();
820+
? getResultWithTimeout(THREAD_POOL.submit(callable::get), timeout) : callable.get();
824821
} catch (InterruptedException | ExecutionException | TimeoutException ex) {
825822
throw logger.logExceptionAsError(new RuntimeException(ex));
826823
} catch (RuntimeException ex) {
@@ -1542,8 +1539,7 @@ public Response<TableTransactionResult> submitTransactionWithResponse(List<Table
15421539

15431540
try {
15441541
return hasTimeout(timeout)
1545-
? getFutureWithCancellation(THREAD_POOL.submit(callable::get), timeout.toMillis(), TimeUnit.MILLISECONDS)
1546-
: callable.get();
1542+
? getResultWithTimeout(THREAD_POOL.submit(callable::get), timeout) : callable.get();
15471543
} catch (InterruptedException | ExecutionException | TimeoutException ex) {
15481544
throw logger.logExceptionAsError(new RuntimeException(ex));
15491545
} catch (RuntimeException ex) {

sdk/tables/azure-data-tables/src/main/java/com/azure/data/tables/TableServiceClient.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,11 @@
4343
import java.util.List;
4444
import java.util.concurrent.ExecutionException;
4545
import java.util.concurrent.ExecutorService;
46-
import java.util.concurrent.TimeUnit;
4746
import java.util.concurrent.TimeoutException;
4847
import java.util.function.Supplier;
4948
import java.util.stream.Collectors;
5049

51-
import static com.azure.core.util.CoreUtils.getFutureWithCancellation;
50+
import static com.azure.core.util.CoreUtils.getResultWithTimeout;
5251
import static com.azure.data.tables.implementation.TableUtils.callWithOptionalTimeout;
5352
import static com.azure.data.tables.implementation.TableUtils.hasTimeout;
5453

@@ -381,8 +380,7 @@ public Response<Void> deleteTableWithResponse(String tableName, Duration timeout
381380
Supplier<Response<Void>> callable = () -> deleteTableWithResponse(tableName, context);
382381
try {
383382
return hasTimeout(timeout)
384-
? getFutureWithCancellation(THREAD_POOL.submit(callable::get), timeout.toMillis(), TimeUnit.MILLISECONDS)
385-
: callable.get();
383+
? getResultWithTimeout(THREAD_POOL.submit(callable::get), timeout) : callable.get();
386384
} catch (InterruptedException | ExecutionException | TimeoutException e) {
387385
throw logger.logExceptionAsError(new RuntimeException(e));
388386
} catch (RuntimeException e) {

sdk/tables/azure-data-tables/src/main/java/com/azure/data/tables/implementation/TableUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
import java.util.function.Supplier;
5353
import java.util.stream.Collectors;
5454

55-
import static com.azure.core.util.CoreUtils.getFutureWithCancellation;
55+
import static com.azure.core.util.CoreUtils.getResultWithTimeout;
5656
import static com.azure.core.util.FluxUtil.monoError;
5757
import static com.azure.core.util.tracing.Tracer.AZ_TRACING_NAMESPACE_KEY;
5858
/**
@@ -616,7 +616,7 @@ public static <T> T callWithOptionalTimeout(Supplier<T> callable, ExecutorServic
616616
ClientLogger logger) {
617617
try {
618618
return hasTimeout(timeout)
619-
? getFutureWithCancellation(threadPool.submit(callable::get), timeout.toMillis(), TimeUnit.MILLISECONDS)
619+
? getResultWithTimeout(threadPool.submit(callable::get), timeout)
620620
: callable.get();
621621
} catch (InterruptedException | ExecutionException | TimeoutException ex) {
622622
throw logger.logExceptionAsError(new RuntimeException(ex));

0 commit comments

Comments
 (0)