66import com .azure .core .http .HttpHeaders ;
77import com .azure .core .http .policy .HttpLogOptions ;
88import com .azure .core .util .logging .ClientLogger ;
9+ import org .junit .jupiter .api .AfterAll ;
910import org .junit .jupiter .api .Assertions ;
11+ import org .junit .jupiter .api .BeforeAll ;
1012import org .junit .jupiter .api .Test ;
1113import org .junit .jupiter .params .ParameterizedTest ;
1214import org .junit .jupiter .params .provider .Arguments ;
2527import java .util .List ;
2628import java .util .Map ;
2729import java .util .UUID ;
30+ import java .util .concurrent .ExecutionException ;
31+ import java .util .concurrent .ExecutorService ;
32+ import java .util .concurrent .Executors ;
33+ import java .util .concurrent .Future ;
2834import java .util .concurrent .ThreadLocalRandom ;
35+ import java .util .concurrent .TimeUnit ;
36+ import java .util .concurrent .TimeoutException ;
37+ import java .util .concurrent .atomic .AtomicBoolean ;
2938import java .util .function .Function ;
3039import java .util .stream .Stream ;
3140
3645import static org .junit .jupiter .api .Assertions .assertNull ;
3746import static org .junit .jupiter .api .Assertions .assertThrows ;
3847import static org .junit .jupiter .api .Assertions .assertTrue ;
48+ import static org .junit .jupiter .api .Assertions .fail ;
3949
4050public class CoreUtilsTests {
4151 private static final byte [] BYTES = "Hello world!" .getBytes (StandardCharsets .UTF_8 );
@@ -49,6 +59,18 @@ public class CoreUtilsTests {
4959 private static final String TIMEOUT_PROPERTY_NAME = "TIMEOUT_PROPERTY_NAME" ;
5060 private static final ConfigurationSource EMPTY_SOURCE = new TestConfigurationSource ();
5161
62+ private static ExecutorService executorService ;
63+
64+ @ BeforeAll
65+ public static void setupClass () {
66+ executorService = Executors .newCachedThreadPool ();
67+ }
68+
69+ @ AfterAll
70+ public static void teardownClass () {
71+ executorService .shutdownNow ();
72+ }
73+
5274 @ Test
5375 public void findFirstOfTypeEmptyArgs () {
5476 assertNull (CoreUtils .findFirstOfType (null , Integer .class ));
@@ -484,7 +506,7 @@ public void randomUuidIsCorrectlyType4() {
484506 bytes [6 ] &= 0x0f ; /* clear version */
485507 bytes [6 ] |= 0x40 ; /* set to version 4 */
486508 bytes [8 ] &= 0x3f ; /* clear variant */
487- bytes [8 ] |= 0x80 ; /* set to IETF variant */
509+ bytes [8 ] |= ( byte ) 0x80 ; /* set to IETF variant */
488510 long msbForJava = 0 ;
489511 long lsbForJava = 0 ;
490512 for (int i = 0 ; i < 8 ; i ++) {
@@ -496,4 +518,48 @@ public void randomUuidIsCorrectlyType4() {
496518
497519 assertEquals (new UUID (msbForJava , lsbForJava ), CoreUtils .randomUuid (msb , lsb ));
498520 }
521+
522+ @ Test
523+ public void futureCompletesBeforeTimeout () {
524+ try {
525+ AtomicBoolean completed = new AtomicBoolean (false );
526+ Future <?> future = executorService .submit (() -> {
527+ Thread .sleep (10 );
528+ completed .set (true );
529+ return null ;
530+ });
531+
532+ future .get (1000 , TimeUnit .MILLISECONDS );
533+
534+ assertTrue (completed .get ());
535+ } catch (InterruptedException | ExecutionException | TimeoutException e ) {
536+ throw new RuntimeException (e );
537+ }
538+ }
539+
540+ @ Test
541+ public void futureTimesOutAndIsCancelled () {
542+ try {
543+ AtomicBoolean completed = new AtomicBoolean (false );
544+ Future <?> future = executorService .submit (() -> {
545+ Thread .sleep (1000 );
546+ completed .set (true );
547+ return null ;
548+ });
549+
550+ try {
551+ CoreUtils .getFutureWithCancellation (future , 100 , TimeUnit .MILLISECONDS );
552+ fail ("Expected future to timout and be cancelled." );
553+ } catch (TimeoutException e ) {
554+ // Expected.
555+ }
556+
557+ // Give time for the future to complete if cancellation didn't work.
558+ Thread .sleep (1000 );
559+
560+ assertFalse (completed .get ());
561+ } catch (InterruptedException | ExecutionException e ) {
562+ throw new RuntimeException (e );
563+ }
564+ }
499565}
0 commit comments