forked from microsoftgraph/msgraph-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDefaultExecutorsTests.java
More file actions
157 lines (129 loc) · 5.79 KB
/
DefaultExecutorsTests.java
File metadata and controls
157 lines (129 loc) · 5.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package com.microsoft.graph.concurrency;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import com.microsoft.graph.core.ClientException;
import com.microsoft.graph.logger.MockLogger;
/**
* Test cases for {@see DefaultExecutors}
*/
public class DefaultExecutorsTests {
private DefaultExecutors defaultExecutors;
private MockLogger mLogger;
@Before
public void setUp() throws Exception {
mLogger = new MockLogger();
defaultExecutors = new DefaultExecutors(mLogger);
}
@Test
public void testNotNull() {
assertNotNull(mLogger);
assertNotNull(defaultExecutors);
}
@Test
public void testPerformOnBackground() {
final String expectedLogMessage = "Starting background task, current active count: 0";
final String expectedResult = "test perform on background success";
final ExecutorTestCallback<String> callback = new ExecutorTestCallback<>();
defaultExecutors.performOnBackground(new Runnable() {
@Override
public void run() {
callback.success(expectedResult);
}
});
callback.waitForCompletion();;
assertTrue(callback._successCalled.get());
assertEquals(expectedResult, callback._successResult.get());
assertEquals(1,mLogger.getLogMessages().size());
assertTrue(mLogger.hasMessage(expectedLogMessage));
}
@Test
public void testPerformOnForegroundWithResult() {
final String expectedResult = "result value";
final String expectedLogMessage = "Starting foreground task, current active count:0, with result result value";
final ExecutorTestCallback<String> callback = new ExecutorTestCallback<>();
defaultExecutors.performOnForeground(expectedResult,callback);
callback.waitForCompletion();
assertTrue(callback._successCalled.get());
assertFalse(callback._failureCalled.get());
assertEquals(expectedResult, callback._successResult.get());
assertEquals(1,mLogger.getLogMessages().size());
assertTrue(mLogger.hasMessage(expectedLogMessage));
}
@Test
public void testPerformOnForegroundWithProgress() throws Exception {
final String expectedLogMessage = "Starting foreground task, current active count:0, with progress 1, max progress2";
final ExecutorTestCallback<String> callback = new ExecutorTestCallback<>();
final int expectedCurrentValue = 1;
final int expectedMaxValue = 2;
defaultExecutors.performOnForeground(expectedCurrentValue, expectedMaxValue, callback);
callback.waitForCompletion();;
assertFalse(callback._successCalled.get());
assertFalse(callback._failureCalled.get());
assertTrue(callback._progressCalled.get());
assertEquals(expectedCurrentValue, callback._progressResultCurrent.get());
assertEquals(expectedMaxValue, callback._progressResultMax.get());
assertEquals(1,mLogger.getLogMessages().size());
assertTrue(mLogger.hasMessage(expectedLogMessage));
}
@Test
public void testPerformOnForegroundWithClientException() {
final String expectedExceptionMessage = "client exception message";
final String expectedLogMessage = "Starting foreground task, current active count:0, with exception com.microsoft.graph.core.ClientException: client exception message";
final ExecutorTestCallback<String> callback = new ExecutorTestCallback<>();
defaultExecutors.performOnForeground(new ClientException(expectedExceptionMessage,null),
callback);
callback.waitForCompletion();
assertFalse(callback._successCalled.get());
assertTrue(callback._failureCalled.get());
assertEquals(expectedExceptionMessage, callback._exceptionResult.get().getMessage());
assertEquals(1,mLogger.getLogMessages().size());
assertTrue(mLogger.hasMessage(expectedLogMessage));
}
private class ExecutorTestCallback<T> implements IProgressCallback<T> {
CountDownLatch latch = new CountDownLatch(1);
AtomicBoolean _successCalled = new AtomicBoolean(false);
AtomicReference<T> _successResult = new AtomicReference<>();
AtomicBoolean _failureCalled = new AtomicBoolean(false);
AtomicReference<ClientException> _exceptionResult = new AtomicReference<>();
AtomicBoolean _progressCalled = new AtomicBoolean(false);
AtomicLong _progressResultCurrent = new AtomicLong(-1);
AtomicLong _progressResultMax = new AtomicLong(-1);
@Override
public void success(final T result) {
_successCalled.set(true);
_successResult.set(result);
latch.countDown();
}
@Override
public void failure(final ClientException ex) {
_failureCalled.set(true);
_exceptionResult.set(ex);
latch.countDown();
}
@Override
public void progress(final long current, final long max) {
_progressCalled.set(true);
_progressResultCurrent.set(current);
_progressResultMax.set(max);
latch.countDown();
}
void waitForCompletion() {
try {
// use a big enough wait to handle a big gc
Assert.assertTrue(latch.await(20, TimeUnit.SECONDS));
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}