-
-
Notifications
You must be signed in to change notification settings - Fork 762
/
BufferRecyclerPoolTest.java
81 lines (67 loc) · 2.83 KB
/
BufferRecyclerPoolTest.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
package com.fasterxml.jackson.core.io;
import com.fasterxml.jackson.core.BaseTest;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.json.JsonGeneratorImpl;
import com.fasterxml.jackson.core.util.BufferRecycler;
import com.fasterxml.jackson.core.util.BufferRecyclerPool;
import java.io.IOException;
import java.io.OutputStream;
public class BufferRecyclerPoolTest extends BaseTest
{
public void testNoOp() {
// no-op pool doesn't actually pool anything, so avoid checking it
checkBufferRecyclerPoolImpl(BufferRecyclerPool.NonRecyclingPool.shared(), false);
}
public void testThreadLocal() {
checkBufferRecyclerPoolImpl(BufferRecyclerPool.ThreadLocalPool.shared(), true);
}
public void testLockFree() {
checkBufferRecyclerPoolImpl(BufferRecyclerPool.LockFreePool.shared(), true);
}
public void testConcurrentDequeue() {
checkBufferRecyclerPoolImpl(BufferRecyclerPool.ConcurrentDequePool.shared(), true);
}
private void checkBufferRecyclerPoolImpl(BufferRecyclerPool pool, boolean checkPooledResource) {
JsonFactory jsonFactory = JsonFactory.builder()
.bufferRecyclerPool(pool)
.build();
BufferRecycler usedBufferRecycler = write("test", jsonFactory, 6);
if (checkPooledResource) {
// acquire the pooled BufferRecycler again and check if it is the same instance used before
BufferRecycler pooledBufferRecycler = pool.acquireBufferRecycler();
try {
assertSame(usedBufferRecycler, pooledBufferRecycler);
} finally {
pooledBufferRecycler.release();
}
}
}
protected final BufferRecycler write(Object value, JsonFactory jsonFactory, int expectedSize) {
BufferRecycler bufferRecycler;
NopOutputStream out = new NopOutputStream();
try (JsonGenerator gen = jsonFactory.createGenerator(out)) {
bufferRecycler = ((JsonGeneratorImpl) gen)._getIoContext()._bufferRecycler;
gen.writeObject(value);
} catch (IOException e) {
throw new RuntimeException(e);
}
assertEquals(expectedSize, out.size);
return bufferRecycler;
}
public class NopOutputStream extends OutputStream {
protected int size = 0;
public NopOutputStream() { }
@Override
public void write(int b) throws IOException { ++size; }
@Override
public void write(byte[] b) throws IOException { size += b.length; }
@Override
public void write(byte[] b, int offset, int len) throws IOException { size += len; }
public NopOutputStream reset() {
size = 0;
return this;
}
public int size() { return size; }
}
}