Skip to content

Commit

Permalink
Merge branch '2.16' into 2.17
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jan 15, 2024
2 parents 83bc73d + e5292e1 commit 6c8f3e9
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.io.IOException;
import java.io.OutputStream;

// Tests for [core#1064] wrt custom `BufferRecycler`
public class BufferRecyclerPoolTest extends BaseTest
{
public void testNoOp() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package com.fasterxml.jackson.core.util;

import java.io.StringWriter;

import com.fasterxml.jackson.core.*;

// Basic testing for [core#1064] wrt usage by `JsonParser` / `JsonGenerator`
public class JsonBufferRecyclersTest extends BaseTest
{
// // Parsers with RecyclerPools:

public void testParserWithThreadLocalPool() throws Exception {
_testParser(JsonRecyclerPools.threadLocalPool());
}

public void testParserWithNopLocalPool() throws Exception {
_testParser(JsonRecyclerPools.nonRecyclingPool());
}

public void testParserWithDequeuPool() throws Exception {
_testParser(JsonRecyclerPools.newConcurrentDequePool());
_testParser(JsonRecyclerPools.sharedConcurrentDequePool());
}

public void testParserWithLockFreePool() throws Exception {
_testParser(JsonRecyclerPools.newLockFreePool());
_testParser(JsonRecyclerPools.sharedLockFreePool());
}

public void testParserWithBoundedPool() throws Exception {
_testParser(JsonRecyclerPools.newBoundedPool(5));
_testParser(JsonRecyclerPools.sharedBoundedPool());
}

private void _testParser(RecyclerPool<BufferRecycler> pool) throws Exception
{
JsonFactory jsonF = JsonFactory.builder()
.recyclerPool(pool)
.build();

JsonParser p = jsonF.createParser(a2q("{'a':123,'b':'foobar'}"));

assertToken(JsonToken.START_OBJECT, p.nextToken());
assertToken(JsonToken.FIELD_NAME, p.nextToken());
assertEquals("a", p.currentName());
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
assertEquals(123, p.getIntValue());
assertToken(JsonToken.FIELD_NAME, p.nextToken());
assertEquals("b", p.currentName());
assertToken(JsonToken.VALUE_STRING, p.nextToken());
assertEquals("foobar", p.getText());
assertToken(JsonToken.END_OBJECT, p.nextToken());

p.close();
}

// // Generators with RecyclerPools:

public void testGeneratorWithThreadLocalPool() throws Exception {
_testGenerator(JsonRecyclerPools.threadLocalPool());
}

public void testGeneratorWithNopLocalPool() throws Exception {
_testGenerator(JsonRecyclerPools.nonRecyclingPool());
}

public void testGeneratorWithDequeuPool() throws Exception {
_testGenerator(JsonRecyclerPools.newConcurrentDequePool());
_testGenerator(JsonRecyclerPools.sharedConcurrentDequePool());
}

public void testGeneratorWithLockFreePool() throws Exception {
_testGenerator(JsonRecyclerPools.newLockFreePool());
_testGenerator(JsonRecyclerPools.sharedLockFreePool());
}

public void testGeneratorWithBoundedPool() throws Exception {
_testGenerator(JsonRecyclerPools.newBoundedPool(5));
_testGenerator(JsonRecyclerPools.sharedBoundedPool());
}

private void _testGenerator(RecyclerPool<BufferRecycler> pool) throws Exception
{
JsonFactory jsonF = JsonFactory.builder()
.recyclerPool(pool)
.build();

StringWriter w = new StringWriter();
JsonGenerator g = jsonF.createGenerator(w);

g.writeStartObject();
g.writeNumberField("a", -42);
g.writeStringField("b", "barfoo");
g.writeEndObject();

g.close();

assertEquals(a2q("{'a':-42,'b':'barfoo'}"), w.toString());
}
}

0 comments on commit 6c8f3e9

Please sign in to comment.