Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests to verify proper life-cycle of RecyclerPool implementations, from databind perspective #4324

Merged
merged 7 commits into from
Jan 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package com.fasterxml.jackson.databind.util;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.nio.charset.StandardCharsets;
import java.util.function.Predicate;

import com.fasterxml.jackson.annotation.JsonPropertyOrder;

Expand Down Expand Up @@ -54,6 +58,10 @@ public void testParserWithBoundedPool() throws Exception {
_testParser(JsonRecyclerPools.sharedBoundedPool());
}

public void testParserWithHybridPool() throws Exception {
_testParser(new HybridTestPool());
}

private void _testParser(RecyclerPool<BufferRecycler> pool) throws Exception
{
ObjectMapper mapper = JsonMapper.builder(
Expand Down Expand Up @@ -100,6 +108,10 @@ public void testGeneratorWithBoundedPool() throws Exception {
_testGenerator(JsonRecyclerPools.sharedBoundedPool());
}

public void testGeneratorWithHybridPool() throws Exception {
_testGenerator(new HybridTestPool());
}

private void _testGenerator(RecyclerPool<BufferRecycler> pool) throws Exception
{
ObjectMapper mapper = JsonMapper.builder(
Expand All @@ -115,4 +127,65 @@ private void _testGenerator(RecyclerPool<BufferRecycler> pool) throws Exception
assertEquals(EXP, new String(mapper.writeValueAsBytes(new Pojo4321(-42, "bogus")),
StandardCharsets.UTF_8));
}

static class HybridTestPool implements RecyclerPool<BufferRecycler>
{
private static final long serialVersionUID = 1L;

private static final Predicate<Thread> isVirtual = VirtualPredicate.findIsVirtualPredicate();

private final RecyclerPool<BufferRecycler> nativePool = JsonRecyclerPools.threadLocalPool();
private final RecyclerPool<BufferRecycler> virtualPool = JsonRecyclerPools.newLockFreePool();

@Override
public BufferRecycler acquirePooled() {
return isVirtual.test(Thread.currentThread()) ?
virtualPool.acquirePooled() :
nativePool.acquirePooled();
}

@Override
public void releasePooled(BufferRecycler pooled) {
if (isVirtual.test(Thread.currentThread())) {
virtualPool.releasePooled(pooled);
} else {
nativePool.releasePooled(pooled);
}
}

static class VirtualPredicate {
static final MethodHandle virtualMh = findVirtualMH();

static MethodHandle findVirtualMH() {
try {
return MethodHandles.publicLookup().findVirtual(Thread.class, "isVirtual",
MethodType.methodType(boolean.class));
} catch (Exception e) {
return null;
}
}

static Predicate<Thread> findIsVirtualPredicate() {
if (virtualMh != null) {
return new Predicate<Thread>() {
@Override
public boolean test(Thread thread) {
try {
return (boolean) virtualMh.invokeExact(thread);
} catch (Throwable e) {
throw new RuntimeException(e);
}
}
};
}

return new Predicate<Thread>() {
@Override
public boolean test(Thread thread) {
return false;
}
};
}
}
}
}