Skip to content
This repository was archived by the owner on Sep 4, 2020. It is now read-only.

Commit 822c977

Browse files
committed
Updates to code to address Peter's feedback.
1 parent 328f8fb commit 822c977

3 files changed

Lines changed: 116 additions & 88 deletions

File tree

Lines changed: 111 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package com.microsoft.graph.concurrency;
22

33
import android.os.Looper;
4-
import android.os.SystemClock;
54
import android.test.AndroidTestCase;
6-
75
import com.microsoft.graph.core.ClientException;
86
import com.microsoft.graph.core.GraphErrorCodes;
97
import com.microsoft.graph.logger.MockLogger;
108
import java.util.concurrent.atomic.AtomicBoolean;
9+
import java.util.concurrent.atomic.AtomicLong;
10+
import java.util.concurrent.atomic.AtomicReference;
1111

1212
/**
1313
* Test cases for {@see DefaultExecutors}
@@ -30,119 +30,157 @@ public void testNotNull() {
3030
}
3131

3232
public void testPerformOnBackground() {
33-
String expectedLogMessage = "Starting background task, current active count: 0";
33+
final String expectedLogMessage = "Starting background task, current active count: 0";
3434
final AtomicBoolean success = new AtomicBoolean(false);
35+
final SimpleWaiter simpleWaiter = new SimpleWaiter();
36+
final AtomicReference<CallingState> callingState = new AtomicReference<>(CallingState.Unknown);
37+
3538
defaultExecutors.performOnBackground(new Runnable() {
3639
@Override
3740
public void run() {
3841
success.set(true);
42+
callingState.set(Looper.getMainLooper().isCurrentThread() ? CallingState.Foreground : CallingState.Background);
43+
simpleWaiter.signal();
3944
}
4045
});
41-
// sleep 1 second for background task
42-
SystemClock.sleep(1000);
43-
new SimpleWaiter().signal();
44-
assertFalse(Looper.getMainLooper().isCurrentThread());
46+
47+
simpleWaiter.waitForSignal();
48+
assertEquals(CallingState.Background, callingState.get());
4549
assertTrue(success.get());
4650
assertEquals(1,mLogger.getLogMessages().size());
4751
assertTrue(mLogger.hasMessage(expectedLogMessage));
4852
}
4953

5054
public void testPerformOnForegroundWithResult() {
51-
String expectedResult = "result value";
52-
String expectedLogMessage = "Starting foreground task, current active count:0, with result result value";
53-
MockCallback callback = new MockCallback();
55+
final String expectedResult = "result value";
56+
final String expectedLogMessage = "Starting foreground task, current active count:0, with result result value";
57+
final MockCallback<String> callback = new MockCallback<>();
58+
5459
defaultExecutors.performOnForeground(expectedResult,callback);
55-
SystemClock.sleep(1000);
56-
new SimpleWaiter().signal();
57-
assertFalse(Looper.getMainLooper().isCurrentThread());
58-
assertTrue(callback.getSuccess());
59-
assertFalse(callback.getFailure());
60-
assertEquals(expectedResult, callback.getResult());
60+
61+
callback._completionWaiter.waitForSignal();
62+
assertEquals(CallingState.Foreground,callback._callingState.get());
63+
assertTrue(callback._successCalled.get());
64+
assertFalse(callback._failureCalled.get());
65+
assertEquals(expectedResult, callback._successResult.get());
6166
assertEquals(1,mLogger.getLogMessages().size());
6267
assertTrue(mLogger.hasMessage(expectedLogMessage));
6368
}
6469

65-
public void testPerformOnForegroundWithProgress() {
66-
String expectedLogMessage = "Starting foreground task, current active count:0, with progress 1, max progress1";
67-
final AtomicBoolean success = new AtomicBoolean(false);
68-
final AtomicBoolean progress = new AtomicBoolean(false);
69-
final AtomicBoolean failure = new AtomicBoolean(false);
70-
IProgressCallback<String> callback = new IProgressCallback<String>() {
71-
@Override
72-
public void progress(long current, long max) {
73-
progress.set(true);
74-
}
75-
76-
@Override
77-
public void success(String s) {
78-
success.set(true);
79-
}
80-
81-
@Override
82-
public void failure(ClientException ex) {
83-
failure.set(true);
84-
}
85-
};
86-
defaultExecutors.performOnForeground(1,1,callback);
87-
SystemClock.sleep(1000);
88-
new SimpleWaiter().signal();
89-
assertFalse(Looper.getMainLooper().isCurrentThread());
90-
assertTrue(progress.get());
91-
assertFalse(success.get());
92-
assertFalse(failure.get());
70+
public void testPerformOnForegroundWithProgress() throws Exception {
71+
final String expectedLogMessage = "Starting foreground task, current active count:0, with progress 1, max progress2";
72+
final ExecutorTestCallback<String> callback = new ExecutorTestCallback<>();
73+
final int expectedCurrentValue = 1;
74+
final int expectedMaxValue = 2;
75+
76+
defaultExecutors.performOnForeground(expectedCurrentValue, expectedMaxValue, callback);
77+
78+
callback._completionWaiter.waitForSignal();
79+
assertEquals(CallingState.Foreground, callback._callingState.get());
80+
assertFalse(callback._successCalled.get());
81+
assertFalse(callback._failureCalled.get());
82+
assertTrue(callback._progressCalled.get());
83+
assertEquals(expectedCurrentValue, callback._progressResultCurrent.get());
84+
assertEquals(expectedMaxValue, callback._progressResultMax.get());
9385
assertEquals(1,mLogger.getLogMessages().size());
9486
assertTrue(mLogger.hasMessage(expectedLogMessage));
9587
}
9688

9789
public void testPerformOnForegroundWithClientException() {
98-
String expectedExceptionMessage = "client exception message";
99-
String expectedLogMessage = "Starting foreground task, current active count:0, with exception com.microsoft.graph.core.ClientException: client exception message";
100-
MockCallback callback = new MockCallback();
101-
defaultExecutors.performOnForeground(new ClientException(expectedExceptionMessage,null, GraphErrorCodes.InvalidAcceptType),callback);
102-
SystemClock.sleep(1000);
103-
new SimpleWaiter().signal();
104-
assertFalse(Looper.getMainLooper().isCurrentThread());
105-
assertFalse(callback.getSuccess());
106-
assertTrue(callback.getFailure());
107-
assertEquals(expectedExceptionMessage, callback.getException().getMessage());
108-
assertTrue(callback.getException().isError(GraphErrorCodes.InvalidAcceptType));
90+
final String expectedExceptionMessage = "client exception message";
91+
final String expectedLogMessage = "Starting foreground task, current active count:0, with exception com.microsoft.graph.core.ClientException: client exception message";
92+
final MockCallback<String> callback = new MockCallback<>();
93+
94+
defaultExecutors.performOnForeground(new ClientException(expectedExceptionMessage,null, GraphErrorCodes.InvalidAcceptType),
95+
callback);
96+
97+
callback._completionWaiter.waitForSignal();
98+
assertEquals(CallingState.Foreground, callback._callingState.get());
99+
assertFalse(callback._successCalled.get());
100+
assertTrue(callback._failureCalled.get());
101+
assertEquals(expectedExceptionMessage, callback._exceptionResult.get().getMessage());
102+
assertTrue(callback._exceptionResult.get().isError(GraphErrorCodes.InvalidAcceptType));
109103
assertEquals(1,mLogger.getLogMessages().size());
110104
assertTrue(mLogger.hasMessage(expectedLogMessage));
111105
}
112106

113-
private class MockCallback implements ICallback <String> {
107+
private class MockCallback<T> implements ICallback <T> {
114108

115-
Boolean success = false;
116-
Boolean failure = false;
117-
String result;
118-
ClientException exception;
109+
SimpleWaiter _completionWaiter = new SimpleWaiter();
110+
AtomicReference<CallingState> _callingState = new AtomicReference<>(CallingState.Unknown);
111+
112+
AtomicBoolean _successCalled = new AtomicBoolean(false);
113+
AtomicReference<T> _successResult = new AtomicReference<>();
114+
115+
AtomicBoolean _failureCalled = new AtomicBoolean(false);
116+
AtomicReference<ClientException> _exceptionResult = new AtomicReference<>();
119117

120118
@Override
121-
public void success(String string) {
122-
success = true;
123-
result = string;
119+
public void success(T result) {
120+
_successCalled.set(true);
121+
_successResult.set(result);
122+
_callingState.set(getCallingState());
123+
_completionWaiter.signal();
124124
}
125125

126126
@Override
127127
public void failure(ClientException ex) {
128-
failure = true;
129-
exception = ex;
128+
_failureCalled.set(true);
129+
_exceptionResult.set(ex);
130+
_callingState.set(getCallingState());
131+
_completionWaiter.signal();
132+
}
133+
134+
private CallingState getCallingState(){
135+
return Looper.getMainLooper().isCurrentThread() ? CallingState.Foreground : CallingState.Background;
130136
}
137+
}
138+
139+
private class ExecutorTestCallback<T> implements IProgressCallback<T> {
140+
SimpleWaiter _completionWaiter = new SimpleWaiter();
141+
AtomicReference<CallingState> _callingState = new AtomicReference<>(CallingState.Unknown);
142+
143+
AtomicBoolean _successCalled = new AtomicBoolean(false);
144+
AtomicReference<T> _successResult = new AtomicReference<>();
131145

132-
public Boolean getSuccess() {
133-
return success;
146+
AtomicBoolean _failureCalled = new AtomicBoolean(false);
147+
AtomicReference<ClientException> _exceptionResult = new AtomicReference<>();
148+
149+
AtomicBoolean _progressCalled = new AtomicBoolean(false);
150+
AtomicLong _progressResultCurrent = new AtomicLong(-1);
151+
AtomicLong _progressResultMax = new AtomicLong(-1);
152+
153+
@Override
154+
public void success(final T result) {
155+
_successCalled.set(true);
156+
_successResult.set(result);
157+
_callingState.set(getCallingState());
158+
_completionWaiter.signal();
134159
}
135160

136-
public Boolean getFailure() {
137-
return failure;
161+
@Override
162+
public void failure(final ClientException ex) {
163+
_failureCalled.set(true);
164+
_exceptionResult.set(ex);
165+
_callingState.set(getCallingState());
166+
_completionWaiter.signal();
138167
}
139168

140-
public String getResult() {
141-
return result;
169+
@Override
170+
public void progress(final long current, final long max) {
171+
_progressCalled.set(true);
172+
_progressResultCurrent.set(current);
173+
_progressResultMax.set(max);
174+
_callingState.set(getCallingState());
175+
_completionWaiter.signal();
142176
}
143177

144-
public ClientException getException() {
145-
return exception;
178+
private CallingState getCallingState(){
179+
return Looper.getMainLooper().isCurrentThread() ? CallingState.Foreground : CallingState.Background;
146180
}
147181
}
182+
183+
private enum CallingState {
184+
Foreground, Background, Unknown;
185+
}
148186
}

graphsdk/src/androidTest/java/com/microsoft/graph/concurrency/SynchronousExecutorTests.java

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

3-
import android.os.Looper;
4-
import android.os.SystemClock;
53
import android.test.AndroidTestCase;
64

75
import java.util.concurrent.atomic.AtomicBoolean;
@@ -13,17 +11,16 @@ public class SynchronousExecutorTests extends AndroidTestCase {
1311

1412
public void testExecute() {
1513
final AtomicBoolean success = new AtomicBoolean(false);
14+
final SimpleWaiter simpleWaiter = new SimpleWaiter();
1615
SynchronousExecutor synchronousExecutor = new SynchronousExecutor();
1716
synchronousExecutor.execute(new Runnable() {
1817
@Override
1918
public void run() {
2019
success.set(true);
20+
simpleWaiter.signal();
2121
}
2222
});
23-
SystemClock.sleep(1000);
24-
new SimpleWaiter().signal();
25-
assertFalse(Looper.getMainLooper().isCurrentThread());
26-
assertNotNull(synchronousExecutor);
23+
simpleWaiter.waitForSignal();
2724
assertTrue(success.get());
2825
assertEquals(0, synchronousExecutor.getActiveCount());
2926
}

graphsdk/src/androidTest/java/com/microsoft/graph/core/BaseClientTests.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,19 +67,12 @@ public void testValidateThrowException() {
6767
assertTrue(success);
6868
}
6969

70-
public void testValidateSuccess() {
71-
Boolean success = false;
70+
public void testValidateSuccess() throws Exception {
7271
baseClient.setAuthenticationProvider(mAuthenticationProvider);
7372
baseClient.setExecutors(mExecutors);
7473
baseClient.setHttpProvider(mHttpProvider);
7574
baseClient.setSerializer(mSerializer);
76-
try{
77-
baseClient.validate();
78-
success = true;
79-
}catch (NullPointerException nEx){
80-
success = false;
81-
}
82-
assertTrue(success);
75+
baseClient.validate();
8376
}
8477

8578
public void testEndPoint() {

0 commit comments

Comments
 (0)