6
6
import com .azure .core .http .HttpHeaders ;
7
7
import com .azure .core .http .policy .HttpLogOptions ;
8
8
import com .azure .core .util .logging .ClientLogger ;
9
+ import org .junit .jupiter .api .AfterAll ;
9
10
import org .junit .jupiter .api .Assertions ;
11
+ import org .junit .jupiter .api .BeforeAll ;
10
12
import org .junit .jupiter .api .Test ;
11
13
import org .junit .jupiter .params .ParameterizedTest ;
12
14
import org .junit .jupiter .params .provider .Arguments ;
25
27
import java .util .List ;
26
28
import java .util .Map ;
27
29
import 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 ;
28
34
import java .util .concurrent .ThreadLocalRandom ;
35
+ import java .util .concurrent .TimeUnit ;
36
+ import java .util .concurrent .TimeoutException ;
37
+ import java .util .concurrent .atomic .AtomicBoolean ;
29
38
import java .util .function .Function ;
30
39
import java .util .stream .Stream ;
31
40
36
45
import static org .junit .jupiter .api .Assertions .assertNull ;
37
46
import static org .junit .jupiter .api .Assertions .assertThrows ;
38
47
import static org .junit .jupiter .api .Assertions .assertTrue ;
48
+ import static org .junit .jupiter .api .Assertions .fail ;
39
49
40
50
public class CoreUtilsTests {
41
51
private static final byte [] BYTES = "Hello world!" .getBytes (StandardCharsets .UTF_8 );
@@ -49,6 +59,18 @@ public class CoreUtilsTests {
49
59
private static final String TIMEOUT_PROPERTY_NAME = "TIMEOUT_PROPERTY_NAME" ;
50
60
private static final ConfigurationSource EMPTY_SOURCE = new TestConfigurationSource ();
51
61
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
+
52
74
@ Test
53
75
public void findFirstOfTypeEmptyArgs () {
54
76
assertNull (CoreUtils .findFirstOfType (null , Integer .class ));
@@ -484,7 +506,7 @@ public void randomUuidIsCorrectlyType4() {
484
506
bytes [6 ] &= 0x0f ; /* clear version */
485
507
bytes [6 ] |= 0x40 ; /* set to version 4 */
486
508
bytes [8 ] &= 0x3f ; /* clear variant */
487
- bytes [8 ] |= 0x80 ; /* set to IETF variant */
509
+ bytes [8 ] |= ( byte ) 0x80 ; /* set to IETF variant */
488
510
long msbForJava = 0 ;
489
511
long lsbForJava = 0 ;
490
512
for (int i = 0 ; i < 8 ; i ++) {
@@ -496,4 +518,48 @@ public void randomUuidIsCorrectlyType4() {
496
518
497
519
assertEquals (new UUID (msbForJava , lsbForJava ), CoreUtils .randomUuid (msb , lsb ));
498
520
}
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
+ }
499
565
}
0 commit comments