Skip to content

Commit 9f9699c

Browse files
committed
remove SimpleWaiter class and use CountDownLatch in tests
1 parent 12a046b commit 9f9699c

File tree

3 files changed

+29
-81
lines changed

3 files changed

+29
-81
lines changed

src/main/java/com/microsoft/graph/concurrency/SimpleWaiter.java

Lines changed: 0 additions & 65 deletions
This file was deleted.

src/test/java/com/microsoft/graph/concurrency/DefaultExecutorsTests.java

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
import static org.junit.Assert.*;
44

5-
import org.junit.After;
5+
import org.junit.Assert;
66
import org.junit.Before;
77
import org.junit.Test;
88

99
import com.microsoft.graph.core.ClientException;
1010
import com.microsoft.graph.logger.MockLogger;
1111

12+
import java.util.concurrent.CountDownLatch;
13+
import java.util.concurrent.TimeUnit;
1214
import java.util.concurrent.atomic.AtomicBoolean;
1315
import java.util.concurrent.atomic.AtomicLong;
1416
import 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
}

src/test/java/com/microsoft/graph/concurrency/SynchronousExecutorTests.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
11
package com.microsoft.graph.concurrency;
22

3-
import static org.junit.Assert.*;
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertTrue;
45

6+
import java.util.concurrent.CountDownLatch;
7+
import java.util.concurrent.TimeUnit;
58
import java.util.concurrent.atomic.AtomicBoolean;
69

7-
import org.junit.*;
10+
import org.junit.Test;
811

912
public class SynchronousExecutorTests {
1013

11-
@Ignore
1214
@Test
1315
public void testExecute() throws Exception {
1416
final AtomicBoolean success = new AtomicBoolean(false);
15-
final SimpleWaiter simpleWaiter = new SimpleWaiter();
17+
final CountDownLatch latch = new CountDownLatch(1);
1618
SynchronousExecutor synchronousExecutor = new SynchronousExecutor();
1719
synchronousExecutor.execute(new Runnable() {
1820
@Override
1921
public void run() {
2022
success.set(true);
21-
simpleWaiter.signal();
23+
latch.countDown();
2224
}
2325
});
24-
simpleWaiter.waitForSignal();
25-
Thread.sleep(2000);
26+
// use a big enough wait to handle a big gc
27+
assertTrue(latch.await(20, TimeUnit.SECONDS));
2628
assertTrue(success.get());
2729
assertEquals(0, synchronousExecutor.getActiveCount());
2830
}

0 commit comments

Comments
 (0)