Skip to content

Commit

Permalink
improve tests checking that pooled BufferRecycler is properly acquire…
Browse files Browse the repository at this point in the history
…d/released
  • Loading branch information
mariofusco committed Aug 30, 2023
1 parent 0f359fc commit 5394156
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
Expand Up @@ -245,6 +245,10 @@ public JacksonFeatureSet<StreamWriteCapability> getWriteCapabilities() {
return JSON_WRITE_CAPABILITIES;
}

public IOContext _getIoContext() {
return _ioContext;
}

/*
/**********************************************************
/* Shared helper methods
Expand Down
@@ -1,8 +1,10 @@
package com.fasterxml.jackson.core.write;
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;
Expand All @@ -11,36 +13,49 @@
public class BufferRecyclerPoolTest extends BaseTest
{
public void testNoOp() {
checkBufferRecyclerPoolImpl(BufferRecyclerPool.NonRecyclingPool.shared());
// no-op pool doesn't actually pool anything, so avoid checking it
checkBufferRecyclerPoolImpl(BufferRecyclerPool.NonRecyclingPool.shared(), false);
}

public void testThreadLocal() {
checkBufferRecyclerPoolImpl(BufferRecyclerPool.ThreadLocalPool.shared());
checkBufferRecyclerPoolImpl(BufferRecyclerPool.ThreadLocalPool.shared(), true);
}

public void testLockFree() {
checkBufferRecyclerPoolImpl(BufferRecyclerPool.LockFreePool.nonShared());
checkBufferRecyclerPoolImpl(BufferRecyclerPool.LockFreePool.shared(), true);
}

public void testConcurrentDequeue() {
checkBufferRecyclerPoolImpl(BufferRecyclerPool.ConcurrentDequePool.nonShared());
checkBufferRecyclerPoolImpl(BufferRecyclerPool.ConcurrentDequePool.shared(), true);
}

private void checkBufferRecyclerPoolImpl(BufferRecyclerPool pool) {
private void checkBufferRecyclerPoolImpl(BufferRecyclerPool pool, boolean checkPooledResource) {
JsonFactory jsonFactory = JsonFactory.builder()
.bufferRecyclerPool(pool)
.build();
assertEquals(6, write("test", jsonFactory));
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 int write(Object value, JsonFactory jsonFactory) {
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);
}
return out.size();
assertEquals(expectedSize, out.size);
return bufferRecycler;
}

public class NopOutputStream extends OutputStream {
Expand Down

0 comments on commit 5394156

Please sign in to comment.