diff --git a/core/src/main/kotlin/com/kakao/actionbase/core/codec/ByteArrayBufferPool.kt b/core/src/main/kotlin/com/kakao/actionbase/core/codec/ByteArrayBufferPool.kt index 969625338..8a63527dc 100644 --- a/core/src/main/kotlin/com/kakao/actionbase/core/codec/ByteArrayBufferPool.kt +++ b/core/src/main/kotlin/com/kakao/actionbase/core/codec/ByteArrayBufferPool.kt @@ -8,6 +8,7 @@ import org.slf4j.LoggerFactory class ByteArrayBufferPool( private val pool: ConcurrentLinkedQueue, + private val bufferSize: Int = Constants.Codec.DEFAULT_BUFFER_SIZE, ) { companion object { private val logger = LoggerFactory.getLogger(ByteArrayBufferPool::class.java) @@ -31,7 +32,7 @@ class ByteArrayBufferPool( pool.add(ByteArrayBuffer(bufferSize)) } - return ByteArrayBufferPool(pool) + return ByteArrayBufferPool(pool, bufferSize) } @JvmStatic @@ -58,7 +59,7 @@ class ByteArrayBufferPool( return value } - fun acquire(): ByteArrayBuffer = pool.poll() + fun acquire(): ByteArrayBuffer = pool.poll() ?: ByteArrayBuffer(bufferSize) fun release(buffer: ByteArrayBuffer) { pool.add(buffer) diff --git a/core/src/test/kotlin/com/kakao/actionbase/core/codec/ByteArrayBufferPoolTest.kt b/core/src/test/kotlin/com/kakao/actionbase/core/codec/ByteArrayBufferPoolTest.kt new file mode 100644 index 000000000..1b06e1c67 --- /dev/null +++ b/core/src/test/kotlin/com/kakao/actionbase/core/codec/ByteArrayBufferPoolTest.kt @@ -0,0 +1,45 @@ +package com.kakao.actionbase.core.codec + +import kotlin.test.assertContentEquals +import kotlin.test.assertEquals +import kotlin.test.assertNotSame + +import org.junit.jupiter.api.Test + +class ByteArrayBufferPoolTest { + companion object { + private const val BUFFER_SIZE = 64 + } + + @Test + fun `use should encode with a new buffer when the pool is exhausted`() { + val pool = ByteArrayBufferPool.create(poolSize = 1, bufferSize = BUFFER_SIZE) + + val outer = + pool.use { pooled -> + pooled.putValue(1) + val inner = + pool.use { allocated -> + assertNotSame(pooled, allocated) + allocated.putValue(2).toByteArray() + } + assertContentEquals(unpooled(2), inner) + pooled.toByteArray() + } + + assertContentEquals(unpooled(1), outer) + } + + @Test + fun `acquire should allocate a buffer of the pool's buffer size when the pool is exhausted`() { + val pool = ByteArrayBufferPool.create(poolSize = 1, bufferSize = BUFFER_SIZE) + val pooled = pool.acquire() + + val allocated = pool.acquire() + + assertNotSame(pooled, allocated) + assertEquals(BUFFER_SIZE, allocated.length) + } + + private fun unpooled(value: Int): ByteArray = ByteArray(BUFFER_SIZE).buffer().putValue(value).toByteArray() +}