-
-
Notifications
You must be signed in to change notification settings - Fork 15.8k
/
RecyclerTest.java
478 lines (421 loc) · 16 KB
/
RecyclerTest.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/*
* Copyright 2014 The Netty Project
*
* The Netty Project licenses this file to you 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:
*
* https://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 io.netty.util;
import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import org.junit.jupiter.api.function.Executable;
import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class RecyclerTest {
protected static Recycler<HandledObject> newRecycler(int maxCapacityPerThread) {
return newRecycler(maxCapacityPerThread, 8, maxCapacityPerThread >> 1);
}
protected static Recycler<HandledObject> newRecycler(int maxCapacityPerThread, int ratio, int chunkSize) {
return new Recycler<HandledObject>(maxCapacityPerThread, ratio, chunkSize) {
@Override
protected HandledObject newObject(
Recycler.Handle<HandledObject> handle) {
return new HandledObject(handle);
}
};
}
@NotNull
protected Thread newThread(Runnable runnable) {
return new Thread(runnable);
}
@Test
@Timeout(value = 5000, unit = TimeUnit.MILLISECONDS)
public void testThreadCanBeCollectedEvenIfHandledObjectIsReferenced() throws Exception {
final Recycler<HandledObject> recycler = newRecycler(1024);
final AtomicBoolean collected = new AtomicBoolean();
final AtomicReference<HandledObject> reference = new AtomicReference<HandledObject>();
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
HandledObject object = recycler.get();
// Store a reference to the HandledObject to ensure it is not collected when the run method finish.
reference.set(object);
}
}) {
@Override
protected void finalize() throws Throwable {
super.finalize();
collected.set(true);
}
};
assertFalse(collected.get());
thread.start();
thread.join();
// Null out so it can be collected.
thread = null;
// Loop until the Thread was collected. If we can not collect it the Test will fail due of a timeout.
while (!collected.get()) {
System.gc();
System.runFinalization();
Thread.sleep(50);
}
// Now call recycle after the Thread was collected to ensure this still works...
reference.getAndSet(null).recycle();
}
@Test
public void verySmallRecycer() {
newRecycler(2, 0, 1).get();
}
@Test
public void testMultipleRecycle() {
Recycler<HandledObject> recycler = newRecycler(1024);
final HandledObject object = recycler.get();
object.recycle();
assertThrows(IllegalStateException.class, new Executable() {
@Override
public void execute() {
object.recycle();
}
});
}
@Test
public void testMultipleRecycleAtDifferentThread() throws InterruptedException {
Recycler<HandledObject> recycler = newRecycler(1024);
final HandledObject object = recycler.get();
final AtomicReference<IllegalStateException> exceptionStore = new AtomicReference<IllegalStateException>();
final Thread thread1 = newThread(new Runnable() {
@Override
public void run() {
object.recycle();
}
});
thread1.start();
thread1.join();
final Thread thread2 = newThread(new Runnable() {
@Override
public void run() {
try {
object.recycle();
} catch (IllegalStateException e) {
exceptionStore.set(e);
}
}
});
thread2.start();
thread2.join();
HandledObject a = recycler.get();
HandledObject b = recycler.get();
assertNotSame(a, b);
IllegalStateException exception = exceptionStore.get();
assertNotNull(exception);
}
@Test
public void testMultipleRecycleAtDifferentThreadRacing() throws InterruptedException {
Recycler<HandledObject> recycler = newRecycler(1024);
final HandledObject object = recycler.get();
final AtomicReference<IllegalStateException> exceptionStore = new AtomicReference<IllegalStateException>();
final CountDownLatch countDownLatch = new CountDownLatch(2);
final Thread thread1 = newThread(new Runnable() {
@Override
public void run() {
try {
object.recycle();
} catch (IllegalStateException e) {
Exception x = exceptionStore.getAndSet(e);
if (x != null) {
e.addSuppressed(x);
}
} finally {
countDownLatch.countDown();
}
}
});
thread1.start();
final Thread thread2 = newThread(new Runnable() {
@Override
public void run() {
try {
object.recycle();
} catch (IllegalStateException e) {
Exception x = exceptionStore.getAndSet(e);
if (x != null) {
e.addSuppressed(x);
}
} finally {
countDownLatch.countDown();
}
}
});
thread2.start();
try {
countDownLatch.await();
HandledObject a = recycler.get();
HandledObject b = recycler.get();
assertNotSame(a, b);
IllegalStateException exception = exceptionStore.get();
if (exception != null) {
assertThat(exception).hasMessageContaining("recycled already");
assertEquals(0, exception.getSuppressed().length);
}
} finally {
thread1.join(1000);
thread2.join(1000);
}
}
@Test
public void testMultipleRecycleRacing() throws InterruptedException {
Recycler<HandledObject> recycler = newRecycler(1024);
final HandledObject object = recycler.get();
final AtomicReference<IllegalStateException> exceptionStore = new AtomicReference<IllegalStateException>();
final CountDownLatch countDownLatch = new CountDownLatch(1);
final Thread thread1 = newThread(new Runnable() {
@Override
public void run() {
try {
object.recycle();
} catch (IllegalStateException e) {
Exception x = exceptionStore.getAndSet(e);
if (x != null) {
e.addSuppressed(x);
}
} finally {
countDownLatch.countDown();
}
}
});
thread1.start();
try {
object.recycle();
} catch (IllegalStateException e) {
Exception x = exceptionStore.getAndSet(e);
if (x != null) {
e.addSuppressed(x);
}
}
try {
countDownLatch.await();
HandledObject a = recycler.get();
HandledObject b = recycler.get();
assertNotSame(a, b);
IllegalStateException exception = exceptionStore.get();
assertNotNull(exception); // Object got recycled twice, so at least one of the calls must throw.
} finally {
thread1.join(1000);
}
}
@Test
public void testRecycle() {
Recycler<HandledObject> recycler = newRecycler(1024);
HandledObject object = recycler.get();
object.recycle();
HandledObject object2 = recycler.get();
assertSame(object, object2);
object2.recycle();
}
@Test
public void testRecycleDisable() {
Recycler<HandledObject> recycler = newRecycler(-1);
HandledObject object = recycler.get();
object.recycle();
HandledObject object2 = recycler.get();
assertNotSame(object, object2);
object2.recycle();
}
@Test
public void testRecycleDisableDrop() {
Recycler<HandledObject> recycler = newRecycler(1024, 0, 16);
HandledObject object = recycler.get();
object.recycle();
HandledObject object2 = recycler.get();
assertSame(object, object2);
object2.recycle();
HandledObject object3 = recycler.get();
assertSame(object, object3);
object3.recycle();
}
/**
* Test to make sure bug #2848 never happens again
* https://github.com/netty/netty/issues/2848
*/
@Test
public void testMaxCapacity() {
testMaxCapacity(300);
Random rand = new Random();
for (int i = 0; i < 50; i++) {
testMaxCapacity(rand.nextInt(1000) + 256); // 256 - 1256
}
}
private static void testMaxCapacity(int maxCapacity) {
Recycler<HandledObject> recycler = newRecycler(maxCapacity);
HandledObject[] objects = new HandledObject[maxCapacity * 3];
for (int i = 0; i < objects.length; i++) {
objects[i] = recycler.get();
}
for (int i = 0; i < objects.length; i++) {
objects[i].recycle();
objects[i] = null;
}
assertTrue(maxCapacity >= recycler.threadLocalSize(),
"The threadLocalSize (" + recycler.threadLocalSize() + ") must be <= maxCapacity ("
+ maxCapacity + ") as we not pool all new handles internally");
}
@Test
public void testRecycleAtDifferentThread() throws Exception {
final Recycler<HandledObject> recycler = newRecycler(256, 2, 16);
final HandledObject o = recycler.get();
final HandledObject o2 = recycler.get();
final Thread thread = newThread(new Runnable() {
@Override
public void run() {
o.recycle();
o2.recycle();
}
});
thread.start();
thread.join();
assertSame(recycler.get(), o);
assertNotSame(recycler.get(), o2);
}
@Test
public void testRecycleAtTwoThreadsMulti() throws Exception {
final Recycler<HandledObject> recycler = newRecycler(256);
final HandledObject o = recycler.get();
ExecutorService single = Executors.newSingleThreadExecutor(new ThreadFactory() {
@Override
public Thread newThread(@NotNull Runnable r) {
return RecyclerTest.this.newThread(r);
}
});
final CountDownLatch latch1 = new CountDownLatch(1);
single.execute(new Runnable() {
@Override
public void run() {
o.recycle();
latch1.countDown();
}
});
assertTrue(latch1.await(100, TimeUnit.MILLISECONDS));
final HandledObject o2 = recycler.get();
// Always recycler the first object, that is Ok
assertSame(o2, o);
final CountDownLatch latch2 = new CountDownLatch(1);
single.execute(new Runnable() {
@Override
public void run() {
//The object should be recycled
o2.recycle();
latch2.countDown();
}
});
assertTrue(latch2.await(100, TimeUnit.MILLISECONDS));
// It should be the same object, right?
final HandledObject o3 = recycler.get();
assertSame(o3, o);
single.shutdown();
}
@Test
public void testMaxCapacityWithRecycleAtDifferentThread() throws Exception {
final int maxCapacity = 4;
final Recycler<HandledObject> recycler = newRecycler(maxCapacity, 4, 4);
// Borrow 2 * maxCapacity objects.
// Return the half from the same thread.
// Return the other half from the different thread.
final HandledObject[] array = new HandledObject[maxCapacity * 3];
for (int i = 0; i < array.length; i ++) {
array[i] = recycler.get();
}
for (int i = 0; i < maxCapacity; i ++) {
array[i].recycle();
}
final Thread thread = newThread(new Runnable() {
@Override
public void run() {
for (int i1 = maxCapacity; i1 < array.length; i1++) {
array[i1].recycle();
}
}
});
thread.start();
thread.join();
assertEquals(maxCapacity * 3 / 4, recycler.threadLocalSize());
for (int i = 0; i < array.length; i ++) {
recycler.get();
}
assertEquals(0, recycler.threadLocalSize());
}
@Test
public void testDiscardingExceedingElementsWithRecycleAtDifferentThread() throws Exception {
final int maxCapacity = 32;
final AtomicInteger instancesCount = new AtomicInteger(0);
final Recycler<HandledObject> recycler = new Recycler<HandledObject>(maxCapacity) {
@Override
protected HandledObject newObject(Recycler.Handle<HandledObject> handle) {
instancesCount.incrementAndGet();
return new HandledObject(handle);
}
};
// Borrow 2 * maxCapacity objects.
final HandledObject[] array = new HandledObject[maxCapacity * 2];
for (int i = 0; i < array.length; i++) {
array[i] = recycler.get();
}
assertEquals(array.length, instancesCount.get());
// Reset counter.
instancesCount.set(0);
// Recycle from other thread.
final Thread thread = newThread(new Runnable() {
@Override
public void run() {
for (HandledObject object: array) {
object.recycle();
}
}
});
thread.start();
thread.join();
assertEquals(0, instancesCount.get());
// Borrow 2 * maxCapacity objects. Half of them should come from
// the recycler queue, the other half should be freshly allocated.
for (int i = 0; i < array.length; i++) {
recycler.get();
}
// The implementation uses maxCapacity / 2 as limit per WeakOrderQueue
assertTrue(array.length - maxCapacity / 2 <= instancesCount.get(),
"The instances count (" + instancesCount.get() + ") must be <= array.length (" + array.length
+ ") - maxCapacity (" + maxCapacity + ") / 2 as we not pool all new handles" +
" internally");
}
static final class HandledObject {
Recycler.Handle<HandledObject> handle;
HandledObject(Recycler.Handle<HandledObject> handle) {
this.handle = handle;
}
void recycle() {
handle.recycle(this);
}
}
}