forked from hazelcast/hazelcast
/
LoggingScheduledExecutorTest.java
279 lines (221 loc) · 8.95 KB
/
LoggingScheduledExecutorTest.java
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
* Copyright (c) 2008-2017, Hazelcast, Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hazelcast.util.executor;
import com.hazelcast.logging.AbstractLogger;
import com.hazelcast.logging.LogEvent;
import com.hazelcast.test.AssertTask;
import com.hazelcast.test.HazelcastParallelClassRunner;
import com.hazelcast.test.HazelcastTestSupport;
import com.hazelcast.test.annotation.ParallelTest;
import com.hazelcast.test.annotation.QuickTest;
import com.hazelcast.util.RootCauseMatcher;
import com.hazelcast.util.executor.LoggingScheduledExecutor.LoggingDelegatingFuture;
import org.junit.After;
import org.junit.Rule;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Callable;
import java.util.concurrent.CancellationException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Future;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import java.util.logging.Level;
import static java.util.concurrent.TimeUnit.SECONDS;
import static java.util.logging.Level.SEVERE;
import static junit.framework.TestCase.assertTrue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNull;
@RunWith(HazelcastParallelClassRunner.class)
@Category({QuickTest.class, ParallelTest.class})
public class LoggingScheduledExecutorTest extends HazelcastTestSupport {
private TestLogger logger = new TestLogger();
private TestThreadFactory factory = new TestThreadFactory();
private ScheduledExecutorService executor;
@Rule
public ExpectedException expectedException = ExpectedException.none();
@After
public void tearDown() throws Exception {
if (executor != null) {
executor.shutdownNow();
executor.awaitTermination(5, SECONDS);
}
}
@Test
public void no_remaining_task_after_cancel() throws Exception {
executor = new LoggingScheduledExecutor(logger, 1, factory);
for (int i = 0; i < 10; i++) {
Future<Integer> future = executor.submit(new Callable<Integer>() {
@Override
public Integer call() throws Exception {
TimeUnit.HOURS.sleep(1);
return null;
}
});
future.cancel(true);
}
BlockingQueue<Runnable> workQueue = ((LoggingScheduledExecutor) executor).getQueue();
assertEquals(0, workQueue.size());
}
@Test
public void testConstructor_withRejectedExecutionHandler() {
RejectedExecutionHandler handler = new RejectedExecutionHandler() {
@Override
public void rejectedExecution(Runnable runnable, ThreadPoolExecutor executor) {
}
};
executor = new LoggingScheduledExecutor(logger, 1, factory, handler);
}
@Test
public void logsExecutionException_withRunnable() {
executor = new LoggingScheduledExecutor(logger, 1, factory);
executor.submit(new FailedRunnable());
assertTrueEventually(new AssertTask() {
@Override
public void run() throws Exception {
assertInstanceOf(RuntimeException.class, logger.getThrowable());
String message = logger.getMessage();
assertTrue("Found message: '" + message + "'", message.contains("FailedRunnable"));
Level level = logger.getLevel();
assertEquals(SEVERE, level);
}
});
}
@Test
public void throwsExecutionException_withCallable() throws Exception {
executor = new LoggingScheduledExecutor(logger, 1, factory);
Future<Integer> future = executor.submit(new FailedCallable());
expectedException.expect(new RootCauseMatcher(RuntimeException.class));
future.get();
assertNull(logger.getThrowable());
}
@Test
public void throwsExecutionException_withCallable_withFutureGetTimeout() throws Exception {
executor = new LoggingScheduledExecutor(logger, 1, factory);
Future<Integer> future = executor.submit(new FailedCallable());
expectedException.expect(new RootCauseMatcher(RuntimeException.class));
future.get(1, SECONDS);
assertNull(logger.getThrowable());
}
@Test
public void testFuture_withCancellation() throws Exception {
final CountDownLatch blocker = new CountDownLatch(1);
executor = new LoggingScheduledExecutor(logger, 1, factory);
Future<Integer> future = executor.submit(new BlockingCallable(blocker));
assertFalse(future.isCancelled());
assertTrue(future.cancel(true));
expectedException.expect(CancellationException.class);
future.get();
}
@Test
public void testLoggingDelegatingFuture() {
executor = new LoggingScheduledExecutor(logger, 1, factory);
Runnable task = new FailedRunnable();
ScheduledFuture<?> scheduledFuture1 = executor.schedule(task, 0, SECONDS);
ScheduledFuture<?> scheduledFuture2 = executor.scheduleAtFixedRate(task, 0, 1, SECONDS);
assertInstanceOf(LoggingDelegatingFuture.class, scheduledFuture1);
assertInstanceOf(LoggingDelegatingFuture.class, scheduledFuture2);
LoggingDelegatingFuture future1 = (LoggingDelegatingFuture) scheduledFuture1;
LoggingDelegatingFuture future2 = (LoggingDelegatingFuture) scheduledFuture2;
assertFalse(future1.isPeriodic());
assertTrue(future2.isPeriodic());
assertEquals(future1, future1);
assertNotEquals(future1, future2);
assertNotEquals(future1, null);
assertEquals(future1.hashCode(), future1.hashCode());
assertNotEquals(future1.hashCode(), future2.hashCode());
}
private static class FailedRunnable implements Runnable {
@Override
public void run() {
throw new RuntimeException();
}
@Override
public String toString() {
return "FailedRunnable{}";
}
}
private static class FailedCallable implements Callable<Integer> {
@Override
public Integer call() throws Exception {
throw new RuntimeException();
}
@Override
public String toString() {
return "FailedCallable{}";
}
}
private static class BlockingCallable implements Callable<Integer> {
private final CountDownLatch blocker;
BlockingCallable(CountDownLatch blocker) {
this.blocker = blocker;
}
@Override
public Integer call() throws Exception {
blocker.await();
return 42;
}
}
private static class TestThreadFactory implements ThreadFactory {
@Override
public Thread newThread(Runnable r) {
return new Thread(r);
}
}
private static class TestLogger extends AbstractLogger {
private final AtomicReference<Throwable> throwableHolder = new AtomicReference<Throwable>();
private final AtomicReference<String> messageHolder = new AtomicReference<String>();
private final AtomicReference<Level> logLevelHolder = new AtomicReference<Level>();
@Override
public void log(Level level, String message) {
throw new UnsupportedOperationException();
}
@Override
public void log(Level level, String message, Throwable thrown) {
throwableHolder.set(thrown);
messageHolder.set(message);
logLevelHolder.set(level);
}
@Override
public void log(LogEvent logEvent) {
throw new UnsupportedOperationException();
}
@Override
public Level getLevel() {
return logLevelHolder.get();
}
@Override
public boolean isLoggable(Level level) {
return false;
}
public Throwable getThrowable() {
return throwableHolder.get();
}
public String getMessage() {
return messageHolder.get();
}
}
}