22
33import static org .junit .Assert .*;
44
5- import org .junit .After ;
5+ import org .junit .Assert ;
66import org .junit .Before ;
77import org .junit .Test ;
88
99import com .microsoft .graph .core .ClientException ;
1010import com .microsoft .graph .logger .MockLogger ;
1111
12+ import java .util .concurrent .CountDownLatch ;
13+ import java .util .concurrent .TimeUnit ;
1214import java .util .concurrent .atomic .AtomicBoolean ;
1315import java .util .concurrent .atomic .AtomicLong ;
1416import java .util .concurrent .atomic .AtomicReference ;
@@ -46,7 +48,7 @@ public void run() {
4648 }
4749 });
4850
49- callback ._completionWaiter . waitForSignal () ;
51+ callback .waitForCompletion (); ;
5052 assertTrue (callback ._successCalled .get ());
5153 assertEquals (expectedResult , callback ._successResult .get ());
5254 assertEquals (1 ,mLogger .getLogMessages ().size ());
@@ -61,7 +63,7 @@ public void testPerformOnForegroundWithResult() {
6163
6264 defaultExecutors .performOnForeground (expectedResult ,callback );
6365
64- callback ._completionWaiter . waitForSignal ();
66+ callback .waitForCompletion ();
6567 assertTrue (callback ._successCalled .get ());
6668 assertFalse (callback ._failureCalled .get ());
6769 assertEquals (expectedResult , callback ._successResult .get ());
@@ -78,7 +80,7 @@ public void testPerformOnForegroundWithProgress() throws Exception {
7880
7981 defaultExecutors .performOnForeground (expectedCurrentValue , expectedMaxValue , callback );
8082
81- callback ._completionWaiter . waitForSignal () ;
83+ callback .waitForCompletion (); ;
8284 assertFalse (callback ._successCalled .get ());
8385 assertFalse (callback ._failureCalled .get ());
8486 assertTrue (callback ._progressCalled .get ());
@@ -97,7 +99,7 @@ public void testPerformOnForegroundWithClientException() {
9799 defaultExecutors .performOnForeground (new ClientException (expectedExceptionMessage ,null ),
98100 callback );
99101
100- callback ._completionWaiter . waitForSignal ();
102+ callback .waitForCompletion ();
101103 assertFalse (callback ._successCalled .get ());
102104 assertTrue (callback ._failureCalled .get ());
103105 assertEquals (expectedExceptionMessage , callback ._exceptionResult .get ().getMessage ());
@@ -106,7 +108,7 @@ public void testPerformOnForegroundWithClientException() {
106108 }
107109
108110 private class ExecutorTestCallback <T > implements IProgressCallback <T > {
109- SimpleWaiter _completionWaiter = new SimpleWaiter ( );
111+ CountDownLatch latch = new CountDownLatch ( 1 );
110112
111113 AtomicBoolean _successCalled = new AtomicBoolean (false );
112114 AtomicReference <T > _successResult = new AtomicReference <>();
@@ -122,22 +124,31 @@ private class ExecutorTestCallback<T> implements IProgressCallback<T> {
122124 public void success (final T result ) {
123125 _successCalled .set (true );
124126 _successResult .set (result );
125- _completionWaiter . signal ();
127+ latch . countDown ();
126128 }
127129
128130 @ Override
129131 public void failure (final ClientException ex ) {
130132 _failureCalled .set (true );
131133 _exceptionResult .set (ex );
132- _completionWaiter . signal ();
134+ latch . countDown ();
133135 }
134136
135137 @ Override
136138 public void progress (final long current , final long max ) {
137139 _progressCalled .set (true );
138140 _progressResultCurrent .set (current );
139141 _progressResultMax .set (max );
140- _completionWaiter .signal ();
142+ latch .countDown ();
143+ }
144+
145+ void waitForCompletion () {
146+ try {
147+ // use a big enough wait to handle a big gc
148+ Assert .assertTrue (latch .await (20 , TimeUnit .SECONDS ));
149+ } catch (InterruptedException e ) {
150+ throw new RuntimeException (e );
151+ }
141152 }
142153 }
143154}
0 commit comments